From 97856bf2815d1f43fd76287bc2295ca8ec7c62f4 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sun, 20 Jun 2021 17:24:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E5=8D=87=E7=BA=A7=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Upgrade/FastGithub.Upgrade.csproj | 12 +++ FastGithub.Upgrade/ProductionVersion.cs | 91 ++++++++++++++++ .../UpgradeHostedService.cs | 101 +++++++----------- .../UpgradeServiceCollectionExtensions.cs | 22 ++++ FastGithub.sln | 6 ++ FastGithub/FastGithub.csproj | 1 + FastGithub/Program.cs | 5 +- 7 files changed, 171 insertions(+), 67 deletions(-) create mode 100644 FastGithub.Upgrade/FastGithub.Upgrade.csproj create mode 100644 FastGithub.Upgrade/ProductionVersion.cs rename FastGithub/VersionHostedService.cs => FastGithub.Upgrade/UpgradeHostedService.cs (51%) create mode 100644 FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs diff --git a/FastGithub.Upgrade/FastGithub.Upgrade.csproj b/FastGithub.Upgrade/FastGithub.Upgrade.csproj new file mode 100644 index 0000000..d44a067 --- /dev/null +++ b/FastGithub.Upgrade/FastGithub.Upgrade.csproj @@ -0,0 +1,12 @@ + + + + net5.0 + enable + + + + + + + diff --git a/FastGithub.Upgrade/ProductionVersion.cs b/FastGithub.Upgrade/ProductionVersion.cs new file mode 100644 index 0000000..c5904b8 --- /dev/null +++ b/FastGithub.Upgrade/ProductionVersion.cs @@ -0,0 +1,91 @@ +using System; +using System.Text.RegularExpressions; + +namespace FastGithub.Upgrade +{ + /// + /// 表示产品版本 + /// + sealed class ProductionVersion : IComparable + { + /// + /// 版本 + /// + public Version Version { get; } + + /// + /// 子版本 + /// + public string SubVersion { get; } + + /// + /// 产品版本 + /// + /// + /// + public ProductionVersion(Version version, string subVersion) + { + this.Version = version; + this.SubVersion = subVersion; + } + + /// + /// 比较版本 + /// + /// + /// + public int CompareTo(ProductionVersion? other) + { + var x = this; + var y = other; + + if (y == null) + { + return 1; + } + + var value = x.Version.CompareTo(y.Version); + if (value == 0) + { + value = CompareSubVerson(x.SubVersion, y.SubVersion); + } + return value; + + static int CompareSubVerson(string subX, string subY) + { + if (subX.Length == 0 && subY.Length == 0) + { + return 0; + } + if (subX.Length == 0) + { + return 1; + } + if (subY.Length == 0) + { + return -1; + } + + return StringComparer.OrdinalIgnoreCase.Compare(subX, subY); + } + } + + public override string ToString() + { + return $"{Version}{SubVersion}"; + } + + /// + /// 解析 + /// + /// + /// + public static ProductionVersion Parse(string productionVersion) + { + const string VERSION = @"^\d+\.(\d+.){0,2}\d+"; + var verion = Regex.Match(productionVersion, VERSION).Value; + var subVersion = productionVersion[verion.Length..]; + return new ProductionVersion(Version.Parse(verion), subVersion); + } + } +} diff --git a/FastGithub/VersionHostedService.cs b/FastGithub.Upgrade/UpgradeHostedService.cs similarity index 51% rename from FastGithub/VersionHostedService.cs rename to FastGithub.Upgrade/UpgradeHostedService.cs index f303f86..b6bf811 100644 --- a/FastGithub/VersionHostedService.cs +++ b/FastGithub.Upgrade/UpgradeHostedService.cs @@ -7,66 +7,50 @@ using System.Net.Http.Json; using System.Reflection; using System.Text; using System.Text.Json.Serialization; -using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; -namespace FastGithub +namespace FastGithub.Upgrade { /// - /// 版本检查 + /// 升级检查后台服务 /// - sealed class VersionHostedService : IHostedService + sealed class UpgradeHostedService : IHostedService { - private readonly ILogger logger; + private readonly ILogger logger; private const string DownloadPage = "https://gitee.com/jiulang/fast-github/releases"; private const string ReleasesUri = "https://gitee.com/api/v5/repos/jiulang/fast-github/releases?page=1&per_page=1&direction=desc"; - public VersionHostedService(ILogger logger) + public UpgradeHostedService(ILogger logger) { this.logger = logger; } - public Task StartAsync(CancellationToken cancellationToken) - { - return this.CheckVersionAsync(cancellationToken); - } - - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } - /// - /// 检测新版本 + /// 检测版本 /// /// /// - private async Task CheckVersionAsync(CancellationToken cancellationToken) + public async Task StartAsync(CancellationToken cancellationToken) { try { - var version = Assembly - .GetEntryAssembly()? - .GetCustomAttribute()? - .InformationalVersion; - - if (version == null) + var currentVersion = GetCurrentVersion(); + if (currentVersion == null) { return; } - using var httpClient = new HttpClient(); - var releases = await httpClient.GetFromJsonAsync(ReleasesUri, cancellationToken); - var lastRelease = releases?.FirstOrDefault(); + var lastRelease = await GetLastedReleaseAsync(cancellationToken); if (lastRelease == null) { return; } - if (VersionCompare(lastRelease.TagName, version) > 0) + var lastedVersion = ProductionVersion.Parse(lastRelease.TagName); + if (lastedVersion.CompareTo(currentVersion) > 0) { - this.logger.LogInformation($"您正在使用{version}版本{Environment.NewLine}请前往{DownloadPage}下载新版本"); + this.logger.LogInformation($"您正在使用{currentVersion}版本{Environment.NewLine}请前往{DownloadPage}下载新版本"); this.logger.LogInformation(lastRelease.ToString()); } } @@ -76,45 +60,34 @@ namespace FastGithub } } - /// - /// 版本比较 - /// - /// - /// - /// - private static int VersionCompare(string x, string y) + public Task StopAsync(CancellationToken cancellationToken) { - const string VERSION = @"^\d+\.(\d+.){0,2}\d+"; - var xVersion = Regex.Match(x, VERSION).Value; - var yVersion = Regex.Match(y, VERSION).Value; + return Task.CompletedTask; + } - var xSubVersion = x[xVersion.Length..]; - var ySubVersion = y[yVersion.Length..]; + /// + /// 获取当前版本 + /// + /// + private static ProductionVersion? GetCurrentVersion() + { + var version = Assembly + .GetEntryAssembly()? + .GetCustomAttribute()? + .InformationalVersion; - var value = Version.Parse(xVersion).CompareTo(Version.Parse(yVersion)); - if (value == 0) - { - value = SubCompare(xSubVersion, ySubVersion); - } - return value; + return version == null ? null : ProductionVersion.Parse(version); + } - static int SubCompare(string subX, string subY) - { - if (subX.Length == 0 && subY.Length == 0) - { - return 0; - } - if (subX.Length == 0) - { - return 1; - } - if (subY.Length == 0) - { - return -1; - } - - return StringComparer.OrdinalIgnoreCase.Compare(subX, subY); - } + /// + /// 获取最新发布 + /// + /// + private static async Task GetLastedReleaseAsync(CancellationToken cancellationToken) + { + using var httpClient = new HttpClient(); + var releases = await httpClient.GetFromJsonAsync(ReleasesUri, cancellationToken); + return releases?.FirstOrDefault(); } /// diff --git a/FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs b/FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs new file mode 100644 index 0000000..484d128 --- /dev/null +++ b/FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs @@ -0,0 +1,22 @@ +using FastGithub.Upgrade; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace FastGithub +{ + /// + /// 服务注册扩展 + /// + public static class DnsServiceCollectionExtensions + { + /// + /// 注册升级后台服务 + /// + /// + /// + public static IServiceCollection AddAppUpgrade(this IServiceCollection services) + { + return services.AddHostedService(); + } + } +} diff --git a/FastGithub.sln b/FastGithub.sln index 2b0900c..ef5bf32 100644 --- a/FastGithub.sln +++ b/FastGithub.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Dns", "FastGithu EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Scanner", "FastGithub.Scanner\FastGithub.Scanner.csproj", "{7F24CD2F-07C0-4002-A534-80688DE95ECF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastGithub.Upgrade", "FastGithub.Upgrade\FastGithub.Upgrade.csproj", "{8239A077-A84C-4FDF-A204-02A2DE4243F3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {7F24CD2F-07C0-4002-A534-80688DE95ECF}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F24CD2F-07C0-4002-A534-80688DE95ECF}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F24CD2F-07C0-4002-A534-80688DE95ECF}.Release|Any CPU.Build.0 = Release|Any CPU + {8239A077-A84C-4FDF-A204-02A2DE4243F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8239A077-A84C-4FDF-A204-02A2DE4243F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8239A077-A84C-4FDF-A204-02A2DE4243F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8239A077-A84C-4FDF-A204-02A2DE4243F3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 336d209..c2f93c1 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -18,6 +18,7 @@ + diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 829b7e7..bfd09cc 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -1,5 +1,4 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Hosting; namespace FastGithub { @@ -27,7 +26,7 @@ namespace FastGithub .UseBinaryPathContentRoot() .ConfigureServices((ctx, services) => { - services.AddHostedService(); + services.AddAppUpgrade(); services.AddGithubDns(ctx.Configuration); }); }