Skip to content

Commit

Permalink
Notify users when there is a new version of the CLI (#242)
Browse files Browse the repository at this point in the history
Do this only when not running unattended.
  • Loading branch information
kzu authored Jun 18, 2024
1 parent 63788f9 commit 2996a1b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Cli/Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NuGet.Protocol" Version="6.10.0" />
<PackageReference Include="NuGetizer" Version="1.2.2" PrivateAssets="all" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageReference Include="Devlooped.JQ" Version="1.7.1.1" />
Expand All @@ -32,6 +33,8 @@
<ProjectProperty Include="ToolCommandName" />
<ProjectProperty Include="BuildDate" />
<ProjectProperty Include="BuildRef" />
<ProjectProperty Include="PackageId" />
<ProjectProperty Include="PackageVersion" />
</ItemGroup>

</Project>
51 changes: 49 additions & 2 deletions src/Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using Devlooped.Sponsors;
using DotNetConfig;
using Microsoft.Extensions.DependencyInjection;
using NuGet.Configuration;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using Spectre.Console;
using Spectre.Console.Cli;

Expand Down Expand Up @@ -40,7 +43,7 @@
// Force run welcome if --welcome is passed or no tos was accepted yet
// preserve all other args just in case the welcome command adds more in the future.
var result = await app.RunAsync(args.TakeWhile(x => x == ToSSettings.ToSOption).Prepend("welcome").ToArray());

if (result != 0)
return result;

Expand All @@ -67,4 +70,48 @@
}
#endif

return app.Run(args);
var updates = Task.Run(() => CheckUpdates(args));
var exit = app.Run(args);

if (await updates is { Length: > 0 } messages)
{
foreach (var message in messages)
AnsiConsole.MarkupLine(message);
}

return exit;

static async Task<string[]> CheckUpdates(string[] args)
{
if (args.Contains("-u") && !args.Contains("--unattended"))
return [];

var providers = Repository.Provider.GetCoreV3();
var repository = new SourceRepository(new PackageSource("https://api.nuget.org/v3/index.json"), providers);
var resource = await repository.GetResourceAsync<PackageMetadataResource>();
var localVersion = new NuGetVersion(ThisAssembly.Project.Version);
var metadata = await resource.GetMetadataAsync(ThisAssembly.Project.PackageId, true, false,
new SourceCacheContext
{
NoCache = true,
RefreshMemoryCache = true,
},
NuGet.Common.NullLogger.Instance, CancellationToken.None);

var update = metadata
.Select(x => x.Identity)
.Where(x => x.Version > localVersion)
.OrderByDescending(x => x.Version)
.Select(x => x.Version)
.FirstOrDefault();

if (update != null)
{
return [
$"There is a new version of [yellow]{ThisAssembly.Project.PackageId}[/]: [dim]v{localVersion.ToNormalizedString()}[/] -> [lime]v{update.ToNormalizedString()}[/]",
$"Update with: [yellow]dotnet[/] tool update -g {ThisAssembly.Project.PackageId}"
];
}

return [];
}

0 comments on commit 2996a1b

Please sign in to comment.