Browse Source

拆分升级项目

xljiulang 4 years ago
parent
commit
97856bf281

+ 12 - 0
FastGithub.Upgrade/FastGithub.Upgrade.csproj

@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+	<PropertyGroup>
+		<TargetFramework>net5.0</TargetFramework>
+		<Nullable>enable</Nullable>
+	</PropertyGroup>
+
+	<ItemGroup>
+		<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
+	</ItemGroup>
+
+</Project>

+ 91 - 0
FastGithub.Upgrade/ProductionVersion.cs

@@ -0,0 +1,91 @@
+using System;
+using System.Text.RegularExpressions;
+
+namespace FastGithub.Upgrade
+{
+    /// <summary>
+    /// 表示产品版本
+    /// </summary>
+    sealed class ProductionVersion : IComparable<ProductionVersion>
+    {
+        /// <summary>
+        /// 版本
+        /// </summary>
+        public Version Version { get; }
+
+        /// <summary>
+        /// 子版本
+        /// </summary>
+        public string SubVersion { get; }
+
+        /// <summary>
+        /// 产品版本
+        /// </summary>
+        /// <param name="version"></param>
+        /// <param name="subVersion"></param>
+        public ProductionVersion(Version version, string subVersion)
+        {
+            this.Version = version;
+            this.SubVersion = subVersion;
+        }
+
+        /// <summary>
+        /// 比较版本
+        /// </summary>
+        /// <param name="other"></param>
+        /// <returns></returns>
+        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}";
+        }
+
+        /// <summary>
+        /// 解析
+        /// </summary>
+        /// <param name="productionVersion"></param>
+        /// <returns></returns>
+        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);
+        }
+    }
+}

+ 35 - 62
FastGithub/VersionHostedService.cs → FastGithub.Upgrade/UpgradeHostedService.cs

@@ -7,66 +7,50 @@ using System.Net.Http.Json;
 using System.Reflection;
 using System.Reflection;
 using System.Text;
 using System.Text;
 using System.Text.Json.Serialization;
 using System.Text.Json.Serialization;
-using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 
 
-namespace FastGithub
+namespace FastGithub.Upgrade
 {
 {
     /// <summary>
     /// <summary>
-    /// 版本检查
+    /// 升级检查后台服务
     /// </summary>
     /// </summary>
-    sealed class VersionHostedService : IHostedService
+    sealed class UpgradeHostedService : IHostedService
     {
     {
-        private readonly ILogger<VersionHostedService> logger;
+        private readonly ILogger<UpgradeHostedService> logger;
         private const string DownloadPage = "https://gitee.com/jiulang/fast-github/releases";
         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";
         private const string ReleasesUri = "https://gitee.com/api/v5/repos/jiulang/fast-github/releases?page=1&per_page=1&direction=desc";
 
 
-        public VersionHostedService(ILogger<VersionHostedService> logger)
+        public UpgradeHostedService(ILogger<UpgradeHostedService> logger)
         {
         {
             this.logger = logger;
             this.logger = logger;
         }
         }
 
 
-        public Task StartAsync(CancellationToken cancellationToken)
-        {
-            return this.CheckVersionAsync(cancellationToken);
-        }
-
-        public Task StopAsync(CancellationToken cancellationToken)
-        {
-            return Task.CompletedTask;
-        }
-
         /// <summary>
         /// <summary>
-        /// 检测版本
+        /// 检测版本
         /// </summary>
         /// </summary>
         /// <param name="cancellationToken"></param>
         /// <param name="cancellationToken"></param>
         /// <returns></returns>
         /// <returns></returns>
-        private async Task CheckVersionAsync(CancellationToken cancellationToken)
+        public async Task StartAsync(CancellationToken cancellationToken)
         {
         {
             try
             try
             {
             {
-                var version = Assembly
-                    .GetEntryAssembly()?
-                    .GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
-                    .InformationalVersion;
-
-                if (version == null)
+                var currentVersion = GetCurrentVersion();
+                if (currentVersion == null)
                 {
                 {
                     return;
                     return;
                 }
                 }
 
 
-                using var httpClient = new HttpClient();
-                var releases = await httpClient.GetFromJsonAsync<Release[]>(ReleasesUri, cancellationToken);
-                var lastRelease = releases?.FirstOrDefault();
+                var lastRelease = await GetLastedReleaseAsync(cancellationToken);
                 if (lastRelease == null)
                 if (lastRelease == null)
                 {
                 {
                     return;
                     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());
                     this.logger.LogInformation(lastRelease.ToString());
                 }
                 }
             }
             }
@@ -76,45 +60,34 @@ namespace FastGithub
             }
             }
         }
         }
 
 
+        public Task StopAsync(CancellationToken cancellationToken)
+        {
+            return Task.CompletedTask;
+        }
+
         /// <summary>
         /// <summary>
-        /// 版本比较
+        /// 获取当前版本
         /// </summary>
         /// </summary>
-        /// <param name="x"></param>
-        /// <param name="y"></param>
         /// <returns></returns>
         /// <returns></returns>
-        private static int VersionCompare(string x, string y)
+        private static ProductionVersion? GetCurrentVersion()
         {
         {
-            const string VERSION = @"^\d+\.(\d+.){0,2}\d+";
-            var xVersion = Regex.Match(x, VERSION).Value;
-            var yVersion = Regex.Match(y, VERSION).Value;
+            var version = Assembly
+                .GetEntryAssembly()?
+                .GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
+                .InformationalVersion;
 
 
-            var xSubVersion = x[xVersion.Length..];
-            var ySubVersion = y[yVersion.Length..];
-
-            var value = Version.Parse(xVersion).CompareTo(Version.Parse(yVersion));
-            if (value == 0)
-            {
-                value = SubCompare(xSubVersion, ySubVersion);
-            }
-            return value;
-
-            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 version == null ? null : ProductionVersion.Parse(version);
+        }
 
 
-                return StringComparer.OrdinalIgnoreCase.Compare(subX, subY);
-            }
+        /// <summary>
+        /// 获取最新发布
+        /// </summary>
+        /// <returns></returns>
+        private static async Task<Release?> GetLastedReleaseAsync(CancellationToken cancellationToken)
+        {
+            using var httpClient = new HttpClient();
+            var releases = await httpClient.GetFromJsonAsync<Release[]>(ReleasesUri, cancellationToken);
+            return releases?.FirstOrDefault();
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 22 - 0
FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs

@@ -0,0 +1,22 @@
+using FastGithub.Upgrade;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace FastGithub
+{
+    /// <summary>
+    /// 服务注册扩展
+    /// </summary>
+    public static class DnsServiceCollectionExtensions
+    {
+        /// <summary>
+        /// 注册升级后台服务
+        /// </summary>
+        /// <param name="services"></param> 
+        /// <returns></returns>
+        public static IServiceCollection AddAppUpgrade(this IServiceCollection services)
+        {
+            return services.AddHostedService<UpgradeHostedService>();
+        }
+    }
+}

+ 6 - 0
FastGithub.sln

@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Dns", "FastGithu
 EndProject
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Scanner", "FastGithub.Scanner\FastGithub.Scanner.csproj", "{7F24CD2F-07C0-4002-A534-80688DE95ECF}"
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Scanner", "FastGithub.Scanner\FastGithub.Scanner.csproj", "{7F24CD2F-07C0-4002-A534-80688DE95ECF}"
 EndProject
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastGithub.Upgrade", "FastGithub.Upgrade\FastGithub.Upgrade.csproj", "{8239A077-A84C-4FDF-A204-02A2DE4243F3}"
+EndProject
 Global
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
 		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}.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.ActiveCfg = Release|Any CPU
 		{7F24CD2F-07C0-4002-A534-80688DE95ECF}.Release|Any CPU.Build.0 = 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
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 		HideSolutionNode = FALSE

+ 1 - 0
FastGithub/FastGithub.csproj

@@ -18,6 +18,7 @@
 		<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
 		<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
 		<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
 		<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
 		<ProjectReference Include="..\FastGithub.Dns\FastGithub.Dns.csproj" />
 		<ProjectReference Include="..\FastGithub.Dns\FastGithub.Dns.csproj" />
+		<ProjectReference Include="..\FastGithub.Upgrade\FastGithub.Upgrade.csproj" />
 	</ItemGroup>
 	</ItemGroup>
 
 
 	<ItemGroup>
 	<ItemGroup>

+ 2 - 3
FastGithub/Program.cs

@@ -1,5 +1,4 @@
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Hosting;
 
 
 namespace FastGithub
 namespace FastGithub
 {
 {
@@ -27,7 +26,7 @@ namespace FastGithub
                 .UseBinaryPathContentRoot()
                 .UseBinaryPathContentRoot()
                 .ConfigureServices((ctx, services) =>
                 .ConfigureServices((ctx, services) =>
                 {
                 {
-                    services.AddHostedService<VersionHostedService>();
+                    services.AddAppUpgrade();
                     services.AddGithubDns(ctx.Configuration);
                     services.AddGithubDns(ctx.Configuration);
                 });
                 });
         }
         }