diff --git a/src/Cli/Cli.csproj b/src/Cli/Cli.csproj index c3cf79cb..add2fb59 100644 --- a/src/Cli/Cli.csproj +++ b/src/Cli/Cli.csproj @@ -16,6 +16,7 @@ + @@ -32,6 +33,8 @@ + + diff --git a/src/Cli/Program.cs b/src/Cli/Program.cs index d319f847..0c84898a 100644 --- a/src/Cli/Program.cs +++ b/src/Cli/Program.cs @@ -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; @@ -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; @@ -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 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(); + 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 []; +} \ No newline at end of file