diff --git a/Source/v2/Meadow.CLI/VersionChecker.cs b/Source/v2/Meadow.CLI/VersionChecker.cs new file mode 100644 index 00000000..f73013a5 --- /dev/null +++ b/Source/v2/Meadow.CLI/VersionChecker.cs @@ -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()? + .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")); + } +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Meadow.CLI.csproj b/Source/v2/Meadow.Cli/Meadow.CLI.csproj index e9799f13..ceff66e7 100644 --- a/Source/v2/Meadow.Cli/Meadow.CLI.csproj +++ b/Source/v2/Meadow.Cli/Meadow.CLI.csproj @@ -10,7 +10,7 @@ Wilderness Labs, Inc Wilderness Labs, Inc true - 2.0.50.0 + 2.0.51.0 AnyCPU http://developer.wildernesslabs.co/Meadow/Meadow.CLI/ https://github.com/WildernessLabs/Meadow.CLI diff --git a/Source/v2/Meadow.Cli/Program.cs b/Source/v2/Meadow.Cli/Program.cs index e11e275d..c9d54b2d 100644 --- a/Source/v2/Meadow.Cli/Program.cs +++ b/Source/v2/Meadow.Cli/Program.cs @@ -99,6 +99,8 @@ public static async Task Main(string[] _) MeadowTelemetry.Current.Dispose(); } + VersionChecker.CheckForUpdates(Log.Logger, serviceProvider.GetService()).Wait(); + return returnCode; } diff --git a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs index 6bdc44f9..a674c67e 100644 --- a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs +++ b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs @@ -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"; } \ No newline at end of file