Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to check for new versions of Meadow.CLI once a day #580

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}
Loading