Skip to content

Commit

Permalink
Merge pull request #547 from melithine/check_version
Browse files Browse the repository at this point in the history
Add support to notify users when they are using an outdated version.
  • Loading branch information
sim0n00ps authored Sep 3, 2024
2 parents 5fa218b + 08b97e1 commit f1b9388
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions OF DL/OF DL.csproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="HtmlAgilityPack" Version="1.11.49" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="7.0.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Octokit" Version="13.0.1" />
<PackageReference Include="protobuf-net" Version="3.2.26" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
Expand Down
44 changes: 39 additions & 5 deletions OF DL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using OF_DL.Enumerations;
using OF_DL.Enumurations;
using OF_DL.Helpers;
using Octokit;
using Serilog;
using Serilog.Core;
using Serilog.Events;
Expand Down Expand Up @@ -49,11 +50,6 @@ public async static Task Main(string[] args)

AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red));

var version = Assembly.GetEntryAssembly()?.GetName().Version;
if (version != null)
{
AnsiConsole.Markup("[green]Version: " + $"{version.Major}.{version.Minor}.{version.Build}\n[/]");
}
//I dont like it... but I needed to move config here, otherwise the logging level gets changed too late after we missed a whole bunch of important info
if (File.Exists("config.json"))
{
Expand Down Expand Up @@ -139,6 +135,44 @@ public async static Task Main(string[] args)
}
}

try
{
Version localVersion = Assembly.GetEntryAssembly()?.GetName().Version; //Only tested with numeric values.
//Get all releases from GitHub
//Source: https://octokitnet.readthedocs.io/en/latest/getting-started/
GitHubClient client = new GitHubClient(new ProductHeaderValue("SomeName"));
IReadOnlyList<Release> releases = await client.Repository.Release.GetAll("sim0n00ps", "OF-DL");

//Setup the versions
Version latestGitHubVersion = new Version(releases[0].TagName.Replace("OFDLV", ""));

//Compare the Versions
//Source: https://stackoverflow.com/questions/7568147/compare-version-numbers-without-using-split-function
int versionComparison = localVersion.CompareTo(latestGitHubVersion);
if (versionComparison < 0)
{
//The version on GitHub is more up to date than this local release.
AnsiConsole.Markup("[red]You are running OF-DL version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}\n[/]");
AnsiConsole.Markup("[red]Please update to the current release on GitHub, " + $"{latestGitHubVersion.Major}.{latestGitHubVersion.Minor}.{latestGitHubVersion.Build}: {releases[0].HtmlUrl}\n[/]");
Log.Debug("Detected outdated client running version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}");
Log.Debug("Latest GitHub release version " + $"{latestGitHubVersion.Major}.{latestGitHubVersion.Minor}.{latestGitHubVersion.Build}");
}
else
{
//This local version is greater than the release version on GitHub.
AnsiConsole.Markup("[green]You are running OF-DL version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}\n[/]");
AnsiConsole.Markup("[green]Latest GitHub Release version: " + $"{latestGitHubVersion.Major}.{latestGitHubVersion.Minor}.{latestGitHubVersion.Build}\n[/]");
Log.Debug("Detected client running version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}");
Log.Debug("Latest GitHub release version " + $"{latestGitHubVersion.Major}.{latestGitHubVersion.Minor}.{latestGitHubVersion.Build}");
}
}
catch (Exception e)
{
AnsiConsole.Markup("[red]Error checking latest release on GitHub:\n[/]");
Console.WriteLine(e);
Log.Error("Error checking latest release on GitHub.", e.Message);
}

if (File.Exists("auth.json"))
{
AnsiConsole.Markup("[green]auth.json located successfully!\n[/]");
Expand Down

0 comments on commit f1b9388

Please sign in to comment.