From d03defc890a4795fabb1db9564e4fc1851a45d02 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Fri, 10 Feb 2023 13:11:16 +0100 Subject: [PATCH 1/3] feat: Check for latest version of Uno.Check # Conflicts: # UnoCheck/CheckCommand.cs --- UnoCheck/ToolInfo.cs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/UnoCheck/ToolInfo.cs b/UnoCheck/ToolInfo.cs index 4795206a..e389cde2 100644 --- a/UnoCheck/ToolInfo.cs +++ b/UnoCheck/ToolInfo.cs @@ -1,10 +1,14 @@ -using NuGet.Versioning; +using NuGet.Common; +using NuGet.Protocol; +using NuGet.Protocol.Core.Types; +using NuGet.Versioning; using Spectre.Console; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace DotNetCheck @@ -38,8 +42,8 @@ public class ToolInfo return await Manifest.Manifest.FromFileOrUrl(f); } - public static string CurrentVersion - => NuGetVersion.Parse(FileVersionInfo.GetVersionInfo(typeof(ToolInfo).Assembly.Location).FileVersion).ToString(); + public static NuGetVersion CurrentVersion + => NuGetVersion.Parse(FileVersionInfo.GetVersionInfo(typeof(ToolInfo).Assembly.Location).FileVersion); public static bool Validate(Manifest.Manifest manifest) { @@ -80,6 +84,27 @@ public static bool Validate(Manifest.Manifest manifest) return true; } + public static async Task GetLatestVersion(bool isPrerelease) + { + // package name and current version + string packageName = "Uno.Check"; + + // create a source repository + var source = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); + + // check for a newer version + var packageMetadataResource = await source.GetResourceAsync(); + var packageMetadata = await packageMetadataResource.GetMetadataAsync( + packageName, + includePrerelease: isPrerelease, + includeUnlisted: false, + new SourceCacheContext(), + NullLogger.Instance, + CancellationToken.None); + var latestVersion = packageMetadata.Select(p => p.Identity.Version).Max(); + return latestVersion; + } + public static void ExitPrompt(bool nonInteractive) { if (!nonInteractive) From ebff48e20862f184ca9721ae28a96e12641f6437 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Fri, 10 Feb 2023 13:12:52 +0100 Subject: [PATCH 2/3] feat: Check for latest uno-check version --- UnoCheck/CheckCommand.cs | 56 +++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/UnoCheck/CheckCommand.cs b/UnoCheck/CheckCommand.cs index cc2a1b58..679ce94b 100644 --- a/UnoCheck/CheckCommand.cs +++ b/UnoCheck/CheckCommand.cs @@ -1,5 +1,6 @@ using DotNetCheck.Checkups; using DotNetCheck.Models; +using NuGet.Configuration; using NuGet.Versioning; using Spectre.Console; using Spectre.Console.Cli; @@ -37,6 +38,11 @@ public override async Task ExecuteAsync(CommandContext context, CheckSettin AnsiConsole.MarkupLine("If problems are detected, it will offer the option to try and fix them for you, or suggest a way to fix them yourself."); AnsiConsole.Write(new Rule()); + if (await NeedsToolUpdateAsync(settings)) + { + return 1; + } + if (!Util.IsAdmin() && Util.IsWindows) { var suTxt = Util.IsWindows ? "Administrator" : "Superuser (su)"; @@ -154,7 +160,7 @@ public override async Task ExecuteAsync(CommandContext context, CheckSettin continue; } - checkup.OnStatusUpdated += checkupStatusUpdated; + checkup.OnStatusUpdated += CheckupStatusUpdated; AnsiConsole.WriteLine(); AnsiConsole.MarkupLine($"[bold]{Icon.Checking} " + checkup.Title + " Checkup[/]..."); @@ -221,7 +227,7 @@ public override async Task ExecuteAsync(CommandContext context, CheckSettin { try { - remedy.OnStatusUpdated += remedyStatusUpdated; + remedy.OnStatusUpdated += RemedyStatusUpdated; AnsiConsole.MarkupLine($"{Icon.Thinking} Attempting to fix: " + checkup.Title); @@ -242,7 +248,7 @@ public override async Task ExecuteAsync(CommandContext context, CheckSettin } finally { - remedy.OnStatusUpdated -= remedyStatusUpdated; + remedy.OnStatusUpdated -= RemedyStatusUpdated; } } @@ -252,7 +258,7 @@ public override async Task ExecuteAsync(CommandContext context, CheckSettin } } - checkup.OnStatusUpdated -= checkupStatusUpdated; + checkup.OnStatusUpdated -= CheckupStatusUpdated; } AnsiConsole.Write(new Rule()); @@ -304,7 +310,45 @@ public override async Task ExecuteAsync(CommandContext context, CheckSettin return exitCode; } - void checkupStatusUpdated(object sender, CheckupStatusEventArgs e) + private async Task NeedsToolUpdateAsync(CheckSettings settings) + { + if (settings.Manifest is not null && !settings.CI) + { + return false; + } + + var currentVersion = ToolInfo.CurrentVersion; + NuGetVersion latestVersion = null; + try + { + latestVersion = await ToolInfo.GetLatestVersion(currentVersion.IsPrerelease); + } + catch + { + AnsiConsole.MarkupLine($"[bold yellow]{Icon.Warning} Could not check for latest version of uno-check on NuGet.org. The currently installed version may be out of date.[/]"); + AnsiConsole.WriteLine(); + AnsiConsole.Write(new Rule()); + } + + if (latestVersion is not null && currentVersion < latestVersion) + { + AnsiConsole.MarkupLine($"[bold yellow]{Icon.Warning} Your uno-check version is not up to date. The latest version is {latestVersion}. You can use the following command to update:[/]"); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine($"dotnet tool update --global Uno.Check --version {latestVersion}"); + AnsiConsole.WriteLine(); + + AnsiConsole.Write(new Rule()); + } + + if (latestVersion is null || currentVersion < latestVersion) + { + return !AnsiConsole.Confirm("Would you still like to continue with the currently installed version?", false); + } + + return false; + } + + private void CheckupStatusUpdated(object sender, CheckupStatusEventArgs e) { var msg = ""; if (e.Status == Models.Status.Error) @@ -319,7 +363,7 @@ void checkupStatusUpdated(object sender, CheckupStatusEventArgs e) AnsiConsole.MarkupLine(" " + msg); } - void remedyStatusUpdated(object sender, RemedyStatusEventArgs e) + private void RemedyStatusUpdated(object sender, RemedyStatusEventArgs e) { AnsiConsole.MarkupLine(" " + e.Message); } From 1305340323a821e9f8715d4419bc9ebc5c013481 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Fri, 10 Feb 2023 13:26:11 +0100 Subject: [PATCH 3/3] chore: Make the logic clearer --- UnoCheck/CheckCommand.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/UnoCheck/CheckCommand.cs b/UnoCheck/CheckCommand.cs index 679ce94b..309e0d87 100644 --- a/UnoCheck/CheckCommand.cs +++ b/UnoCheck/CheckCommand.cs @@ -317,6 +317,7 @@ private async Task NeedsToolUpdateAsync(CheckSettings settings) return false; } + bool needsToUpdate = false; var currentVersion = ToolInfo.CurrentVersion; NuGetVersion latestVersion = null; try @@ -342,10 +343,11 @@ private async Task NeedsToolUpdateAsync(CheckSettings settings) if (latestVersion is null || currentVersion < latestVersion) { - return !AnsiConsole.Confirm("Would you still like to continue with the currently installed version?", false); - } + var shouldContinue = AnsiConsole.Confirm("Would you still like to continue with the currently installed version?", false); + needsToUpdate = !shouldContinue; + } - return false; + return needsToUpdate; } private void CheckupStatusUpdated(object sender, CheckupStatusEventArgs e)