Skip to content

Commit

Permalink
Add logic to check for new versions of Meadow.CLI once a day
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Jun 16, 2024
1 parent fcf57f4 commit 0e3e256
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 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"));
}
}
5 changes: 5 additions & 0 deletions Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public async ValueTask ExecuteAsync(IConsole console)
{
return GetType().GetCustomAttribute<CommandAttribute>(true)?.Name;
}

private void CheckForCLIUpdates()
{

}
}
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

0 comments on commit 0e3e256

Please sign in to comment.