Skip to content

Commit

Permalink
Merge pull request #580 from WildernessLabs/version_check
Browse files Browse the repository at this point in the history
Add logic to check for new versions of Meadow.CLI once a day
  • Loading branch information
ctacke authored Jun 17, 2024
2 parents fcf57f4 + 11bb697 commit b1f21c6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
70 changes: 70 additions & 0 deletions Source/v2/Meadow.CLI/VersionChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Newtonsoft.Json.Linq;
using Serilog;
using System.Reflection;

namespace Meadow.CLI;

public static class VersionChecker
{
private static readonly string PackageId = "WildernessLabs.Meadow.CLI";
private static readonly string NugetApiUrl = $"https://api.nuget.org/v3-flatcontainer/{PackageId.ToLower()}/index.json";
private static readonly TimeSpan CheckFrequency = TimeSpan.FromDays(1);
private static readonly string LastCheckKey = "last_check_time";

public static async Task CheckForUpdates(ILogger? logger, ISettingsManager settingsManager)
{
if (DateTime.UtcNow - GetLastCheckTime(settingsManager) < CheckFrequency)
{
return;
}

string currentVersion = GetCurrentVersion();

using var httpClient = new HttpClient();

try
{
var response = await httpClient.GetStringAsync(NugetApiUrl);

var json = JObject.Parse(response);
var latestPublishedVersion = $"{json["versions"].Last()}";

if (latestPublishedVersion != null &&
Version.TryParse(currentVersion, out Version? currentVersionParsed) &&
Version.TryParse(latestPublishedVersion, out Version? latestVersionParsed) &&
latestVersionParsed > currentVersionParsed)
{

logger?.Information($"\r\nMeadow.CLI {latestVersionParsed} is avaliable - run 'dotnet tool update {PackageId} -g' to update");
}

SetLastCheckTime(settingsManager, DateTime.UtcNow);
}
catch (Exception ex)
{
logger?.Debug($"Error checking for updates: {ex.Message}");
}
}

private static string GetCurrentVersion()
{
return Assembly.GetEntryAssembly()?
.GetCustomAttribute<AssemblyFileVersionAttribute>()?
.Version ?? "2.0.0";
}

private static DateTime GetLastCheckTime(ISettingsManager settingsManager)
{
var lastCheckString = settingsManager.GetSetting(LastCheckKey);
if (DateTime.TryParse(lastCheckString, out DateTime lastCheck))
{
return lastCheck;
}
return DateTime.MinValue;
}

private static void SetLastCheckTime(ISettingsManager settingsManager, DateTime dateTime)
{
settingsManager.SaveSetting(LastCheckKey, dateTime.ToString("o"));
}
}
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Meadow.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Authors>Wilderness Labs, Inc</Authors>
<Company>Wilderness Labs, Inc</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.0.50.0</PackageVersion>
<PackageVersion>2.0.51.0</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
2 changes: 2 additions & 0 deletions Source/v2/Meadow.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public static async Task<int> Main(string[] _)
MeadowTelemetry.Current.Dispose();
}

VersionChecker.CheckForUpdates(Log.Logger, serviceProvider.GetService<ISettingsManager>()).Wait();

return returnCode;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Meadow.CLI;

public static class Constants
{
public const string CLI_VERSION = "2.0.50.0";
public const string CLI_VERSION = "2.0.51.0";
}

0 comments on commit b1f21c6

Please sign in to comment.