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

feat: Check for latest version of Uno.Check #124

Merged
merged 3 commits into from
Feb 10, 2023
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
58 changes: 52 additions & 6 deletions UnoCheck/CheckCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DotNetCheck.Checkups;
using DotNetCheck.Models;
using NuGet.Configuration;
using NuGet.Versioning;
using Spectre.Console;
using Spectre.Console.Cli;
Expand Down Expand Up @@ -37,6 +38,11 @@ public override async Task<int> 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)";
Expand Down Expand Up @@ -154,7 +160,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, CheckSettin
continue;
}

checkup.OnStatusUpdated += checkupStatusUpdated;
checkup.OnStatusUpdated += CheckupStatusUpdated;

AnsiConsole.WriteLine();
AnsiConsole.MarkupLine($"[bold]{Icon.Checking} " + checkup.Title + " Checkup[/]...");
Expand Down Expand Up @@ -221,7 +227,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, CheckSettin
{
try
{
remedy.OnStatusUpdated += remedyStatusUpdated;
remedy.OnStatusUpdated += RemedyStatusUpdated;

AnsiConsole.MarkupLine($"{Icon.Thinking} Attempting to fix: " + checkup.Title);

Expand All @@ -242,7 +248,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, CheckSettin
}
finally
{
remedy.OnStatusUpdated -= remedyStatusUpdated;
remedy.OnStatusUpdated -= RemedyStatusUpdated;
}
}

Expand All @@ -252,7 +258,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, CheckSettin
}
}

checkup.OnStatusUpdated -= checkupStatusUpdated;
checkup.OnStatusUpdated -= CheckupStatusUpdated;
}

AnsiConsole.Write(new Rule());
Expand Down Expand Up @@ -304,7 +310,47 @@ public override async Task<int> ExecuteAsync(CommandContext context, CheckSettin
return exitCode;
}

void checkupStatusUpdated(object sender, CheckupStatusEventArgs e)
private async Task<bool> NeedsToolUpdateAsync(CheckSettings settings)
{
if (settings.Manifest is not null && !settings.CI)
{
return false;
}

bool needsToUpdate = 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)
{
var shouldContinue = AnsiConsole.Confirm("Would you still like to continue with the currently installed version?", false);
needsToUpdate = !shouldContinue;
}

return needsToUpdate;
}

private void CheckupStatusUpdated(object sender, CheckupStatusEventArgs e)
{
var msg = "";
if (e.Status == Models.Status.Error)
Expand All @@ -319,7 +365,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);
}
Expand Down
31 changes: 28 additions & 3 deletions UnoCheck/ToolInfo.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -80,6 +84,27 @@ public static bool Validate(Manifest.Manifest manifest)
return true;
}

public static async Task<NuGetVersion> 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<PackageMetadataResource>();
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)
Expand Down