From 6ad154a62b81e2012c87f7b0ab134b376ed3b1af Mon Sep 17 00:00:00 2001 From: real-zony Date: Sat, 24 Sep 2022 19:13:57 +0800 Subject: [PATCH] feat: Add update check logic. --- .../Infrastructure/Updater/DefaultUpdater.cs | 62 +++++++++++++++++++ .../Updater/JsonModel/NewVersionItem.cs | 8 +++ .../Updater/JsonModel/NewVersionItemType.cs | 7 +++ .../Updater/JsonModel/NewVersionResponse.cs | 15 +++++ src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj | 62 ++++++++++--------- 5 files changed, 125 insertions(+), 29 deletions(-) create mode 100644 src/ZonyLrcTools.Cli/Infrastructure/Updater/DefaultUpdater.cs create mode 100644 src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionItem.cs create mode 100644 src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionItemType.cs create mode 100644 src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionResponse.cs diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Updater/DefaultUpdater.cs b/src/ZonyLrcTools.Cli/Infrastructure/Updater/DefaultUpdater.cs new file mode 100644 index 0000000..dd90baf --- /dev/null +++ b/src/ZonyLrcTools.Cli/Infrastructure/Updater/DefaultUpdater.cs @@ -0,0 +1,62 @@ +using System; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using ZonyLrcTools.Cli.Infrastructure.DependencyInject; +using ZonyLrcTools.Cli.Infrastructure.Network; +using ZonyLrcTools.Cli.Infrastructure.Updater.JsonModel; + +namespace ZonyLrcTools.Cli.Infrastructure.Updater; + +public class DefaultUpdater : ISingletonDependency +{ + public const string UpdateUrl = "https://api.zony.me/lrc-tools/update"; + + private readonly IWarpHttpClient _warpHttpClient; + private readonly ILogger _logger; + + public DefaultUpdater(IWarpHttpClient warpHttpClient, + ILogger logger) + { + _warpHttpClient = warpHttpClient; + _logger = logger; + } + + public async Task CheckUpdateAsync() + { + var response = await _warpHttpClient.GetAsync(UpdateUrl); + if (response == null) + { + return; + } + + var currentVersion = Assembly.GetExecutingAssembly().GetName().Version; + if (response.NewVersion <= currentVersion) + { + return; + } + + var importantItem = response.Items.FirstOrDefault(x => x.ItemType == NewVersionItemType.Important); + if (importantItem != null) + { + _logger.LogWarning($"发现了新版本,请点击下面的链接进行更新:{importantItem.Url}"); + _logger.LogWarning($"最新版本号:{response.NewVersion},当前版本号: ${currentVersion}"); + _logger.LogWarning($"更新内容:{response.NewVersionDescription}"); + + if (OperatingSystem.IsWindows()) + { + Process.Start("explorer.exe", importantItem.Url); + } + else if (OperatingSystem.IsMacOS()) + { + Process.Start("open", importantItem.Url); + } + else if (OperatingSystem.IsLinux()) + { + Process.Start("xdg-open", importantItem.Url); + } + } + } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionItem.cs b/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionItem.cs new file mode 100644 index 0000000..f183b0e --- /dev/null +++ b/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionItem.cs @@ -0,0 +1,8 @@ +namespace ZonyLrcTools.Cli.Infrastructure.Updater.JsonModel; + +public class NewVersionItem +{ + public string Url { get; set; } + + public NewVersionItemType ItemType { get; set; } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionItemType.cs b/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionItemType.cs new file mode 100644 index 0000000..b68b36a --- /dev/null +++ b/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionItemType.cs @@ -0,0 +1,7 @@ +namespace ZonyLrcTools.Cli.Infrastructure.Updater.JsonModel; + +public enum NewVersionItemType +{ + Important, + NormalBinaryFile +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionResponse.cs b/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionResponse.cs new file mode 100644 index 0000000..b5d0b9e --- /dev/null +++ b/src/ZonyLrcTools.Cli/Infrastructure/Updater/JsonModel/NewVersionResponse.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace ZonyLrcTools.Cli.Infrastructure.Updater.JsonModel; + +public class NewVersionResponse +{ + public Version NewVersion { get; set; } + + public string NewVersionDescription { get; set; } + + public List Items { get; set; } + + public DateTime UpdateTime { get; set; } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj b/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj index 5e377a0..2d97079 100644 --- a/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj +++ b/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj @@ -3,41 +3,45 @@ net6.0 Exe + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 + 0.0.0.1 - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - Always - - - - Always - - - - Always - + + + + Always + + + + Always + + + + Always +