|
| 1 | +using DotNetConfig; |
| 2 | +using NuGet.Configuration; |
| 3 | +using NuGet.Protocol.Core.Types; |
| 4 | +using NuGet.Versioning; |
| 5 | +using Spectre.Console; |
| 6 | +using Spectre.Console.Cli; |
| 7 | + |
| 8 | +namespace Devlooped.Extensions.AI; |
| 9 | + |
| 10 | +static class AppExtensions |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Runs the app and update check in paralell, and renders any update messages if available |
| 14 | + /// after the command app completes. |
| 15 | + /// </summary> |
| 16 | + public static async Task<int> RunWithUpdatesAsync(this ICommandApp app, string[] args) |
| 17 | + { |
| 18 | + var updates = Task.Run(() => GetUpdatesAsync(args)); |
| 19 | + var exit = await app.RunAsync(args); |
| 20 | + |
| 21 | + if (await updates is { Length: > 0 } messages) |
| 22 | + { |
| 23 | + foreach (var message in messages) |
| 24 | + AnsiConsole.MarkupLine(message); |
| 25 | + } |
| 26 | + |
| 27 | + return exit; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Checks for updates to the tool and shows them if available. |
| 32 | + /// </summary> |
| 33 | + public static async Task ShowUpdatesAsync(this ICommandApp app, string[] args) |
| 34 | + { |
| 35 | + if (await GetUpdatesAsync(args, true) is { Length: > 0 } messages) |
| 36 | + { |
| 37 | + foreach (var message in messages) |
| 38 | + AnsiConsole.MarkupLine(message); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Shows the app version, build date and release link. |
| 44 | + /// </summary> |
| 45 | + /// <param name="app"></param> |
| 46 | + public static void ShowVersion(this ICommandApp app) |
| 47 | + { |
| 48 | + AnsiConsole.MarkupLine($"{ThisAssembly.Project.ToolCommandName} version [lime]{ThisAssembly.Project.Version}[/] ({ThisAssembly.Project.BuildDate})"); |
| 49 | + AnsiConsole.MarkupLine($"[link]{ThisAssembly.Git.Url}/releases/tag/{ThisAssembly.Project.BuildRef}[/]"); |
| 50 | + } |
| 51 | + |
| 52 | + static async Task<string[]> GetUpdatesAsync(string[] args, bool forced = false) |
| 53 | + { |
| 54 | + if (args.Contains("-u") || args.Contains("--unattended")) |
| 55 | + return []; |
| 56 | + |
| 57 | + var config = Config.Build(ConfigLevel.Global).GetSection(ThisAssembly.Project.ToolCommandName); |
| 58 | + |
| 59 | + // Check once a day max |
| 60 | + if (!forced) |
| 61 | + { |
| 62 | + var lastCheck = config.GetDateTime("checked") ?? DateTime.UtcNow.AddDays(-2); |
| 63 | + // if it's been > 24 hours since the last check, we'll check again |
| 64 | + if (lastCheck > DateTime.UtcNow.AddDays(-1)) |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + // We check from a different feed in this case. |
| 69 | + var civersion = ThisAssembly.Project.VersionPrefix.StartsWith("42.42."); |
| 70 | + |
| 71 | + var providers = Repository.Provider.GetCoreV3(); |
| 72 | + var repository = new SourceRepository(new PackageSource( |
| 73 | + // use CI feed rather than production feed depending on which version we're using |
| 74 | + civersion && !string.IsNullOrEmpty(ThisAssembly.Project.SLEET_FEED_URL) ? |
| 75 | + ThisAssembly.Project.SLEET_FEED_URL : |
| 76 | + "https://api.nuget.org/v3/index.json"), providers); |
| 77 | + var resource = await repository.GetResourceAsync<PackageMetadataResource>(); |
| 78 | + var localVersion = new NuGetVersion(ThisAssembly.Project.Version); |
| 79 | + // Only update to stable versions, not pre-releases |
| 80 | + var metadata = await resource.GetMetadataAsync(ThisAssembly.Project.PackageId, includePrerelease: false, false, |
| 81 | + new SourceCacheContext |
| 82 | + { |
| 83 | + NoCache = true, |
| 84 | + RefreshMemoryCache = true, |
| 85 | + }, |
| 86 | + NuGet.Common.NullLogger.Instance, CancellationToken.None); |
| 87 | + |
| 88 | + var update = metadata |
| 89 | + .Select(x => x.Identity) |
| 90 | + .Where(x => x.Version > localVersion) |
| 91 | + .OrderByDescending(x => x.Version) |
| 92 | + .Select(x => x.Version) |
| 93 | + .FirstOrDefault(); |
| 94 | + |
| 95 | + config.SetDateTime("checked", DateTime.UtcNow); |
| 96 | + |
| 97 | + if (update != null) |
| 98 | + { |
| 99 | + return [ |
| 100 | + $"There is a new version of [yellow]{ThisAssembly.Project.PackageId}[/]: [dim]v{localVersion.ToNormalizedString()}[/] -> [lime]v{update.ToNormalizedString()}[/]", |
| 101 | + $"Update with: [yellow]dotnet[/] tool update -g {ThisAssembly.Project.PackageId}" + |
| 102 | + (civersion ? $" --source {ThisAssembly.Project.SLEET_FEED_URL}" : ""), |
| 103 | + ]; |
| 104 | + } |
| 105 | + |
| 106 | + return []; |
| 107 | + } |
| 108 | +} |
0 commit comments