Skip to content

Commit

Permalink
Add GitHub release info at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Dec 20, 2024
1 parent e0c54b9 commit c45aaa8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
9 changes: 9 additions & 0 deletions Src/GBX.NET.Tool.CLI/GitHubJsonContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using GBX.NET.Tool.CLI;
using System.Text.Json.Serialization;

namespace NationsConverterWeb;

[JsonSerializable(typeof(UpdateInfo))]
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
internal sealed partial class GitHubJsonContext : JsonSerializerContext;
16 changes: 5 additions & 11 deletions Src/GBX.NET.Tool.CLI/ToolConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,6 @@ public async Task RunAsync(ExecuteLogic executeLogic, CancellationToken cancella
logger.LogTrace("Intro finished.");
}

// Check for updates here if received. If not, check at the end of the tool execution
var updateCheckCompleted = updateChecker is null
|| await updateChecker.TryCompareVersionAsync();

logger.LogDebug("Update check completed: {UpdateCheckCompleted}", updateCheckCompleted);

AnsiConsole.WriteLine();

if (toolSettings.InputArguments.Count == 0)
Expand All @@ -165,9 +159,9 @@ public async Task RunAsync(ExecuteLogic executeLogic, CancellationToken cancella
AnsiConsole.WriteLine(options.IntroText);
}

if (!updateCheckCompleted && updateChecker is not null)
if (updateChecker is not null)
{
await updateChecker.CompareVersionAsync();
await updateChecker.CompareVersionAsync(cancellationToken);
}

AnsiConsole.WriteLine();
Expand All @@ -194,10 +188,10 @@ public async Task RunAsync(ExecuteLogic executeLogic, CancellationToken cancella

logger.LogInformation("Completed!");

// Check again for updates if not done before
if (!updateCheckCompleted && updateChecker is not null)
// Check for updates if not done before
if (updateChecker is not null)
{
await updateChecker.CompareVersionAsync();
await updateChecker.CompareVersionAsync(cancellationToken);
}
}

Expand Down
30 changes: 20 additions & 10 deletions Src/GBX.NET.Tool.CLI/ToolUpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Spectre.Console;
using NationsConverterWeb;
using Spectre.Console;
using System.Net.Http.Json;

namespace GBX.NET.Tool.CLI;

Expand All @@ -22,19 +24,19 @@ public ToolUpdateChecker(Task<HttpResponseMessage> updateInfoResponseTask)
return new ToolUpdateChecker(responseTask);
}

public async ValueTask<bool> TryCompareVersionAsync()
public async ValueTask<bool> TryCompareVersionAsync(CancellationToken cancellationToken)
{
if (!updateInfoResponseTask.IsCompleted)
{
return false;
}

await CompareVersionAsync();
await CompareVersionAsync(cancellationToken);

return true;
}

public async Task CompareVersionAsync()
public async Task CompareVersionAsync(CancellationToken cancellationToken)
{
AnsiConsole.WriteLine();

Expand All @@ -45,13 +47,21 @@ public async Task CompareVersionAsync()
AnsiConsole.Write(new Rule("Check for updates / auto-updater").LeftJustified().RuleStyle("yellow"));
AnsiConsole.WriteLine();

//var updateInfo = await updateInfoResponse.Content.ReadFromJsonAsync<UpdateInfo>(cancellationToken);
try
{
var updateInfo = await updateInfoResponse.Content.ReadFromJsonAsync(GitHubJsonContext.Default.UpdateInfo, cancellationToken);

//if (updateInfo is not null)
//{
AnsiConsole.MarkupLine($"[yellow]New version available:[/] [green]tag[/]");
AnsiConsole.MarkupLine($"[yellow]Release notes:[/] [green]url[/]");
//}
if (updateInfo is not null)
{
AnsiConsole.MarkupLine($"[yellow]Latest version available:[/] [green]{updateInfo.TagName?.TrimStart('v')}[/]");
AnsiConsole.MarkupLine($"[yellow]Release notes:[/] [green]{updateInfo.HtmlUrl}[/]");
}
}
catch (Exception ex)
{
AnsiConsole.MarkupLine("[red]Failed to parse update information.[/]");
AnsiConsole.WriteException(ex);
}

AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule().RuleStyle("yellow"));
Expand Down
8 changes: 8 additions & 0 deletions Src/GBX.NET.Tool.CLI/UpdateInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace GBX.NET.Tool.CLI;

internal sealed class UpdateInfo
{
public string? TagName { get; set; }
public string? HtmlUrl { get; set; }
public bool IsPrerelease { get; set; }
}

0 comments on commit c45aaa8

Please sign in to comment.