From cbd12b69d58f37ba2fbd0553b77af69dd3263e39 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Tue, 27 Jul 2021 21:31:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4upgrade=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Upgrade/FastGithub.Upgrade.csproj | 11 -- FastGithub.Upgrade/GithubRelease.cs | 65 ----------- FastGithub.Upgrade/GithubRequestMessage.cs | 17 --- FastGithub.Upgrade/ProductionVersion.cs | 107 ------------------ .../ServiceCollectionExtensions.cs | 23 ---- FastGithub.Upgrade/UpgradeService.cs | 90 --------------- 6 files changed, 313 deletions(-) delete mode 100644 FastGithub.Upgrade/FastGithub.Upgrade.csproj delete mode 100644 FastGithub.Upgrade/GithubRelease.cs delete mode 100644 FastGithub.Upgrade/GithubRequestMessage.cs delete mode 100644 FastGithub.Upgrade/ProductionVersion.cs delete mode 100644 FastGithub.Upgrade/ServiceCollectionExtensions.cs delete mode 100644 FastGithub.Upgrade/UpgradeService.cs diff --git a/FastGithub.Upgrade/FastGithub.Upgrade.csproj b/FastGithub.Upgrade/FastGithub.Upgrade.csproj deleted file mode 100644 index 1ee4920..0000000 --- a/FastGithub.Upgrade/FastGithub.Upgrade.csproj +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/FastGithub.Upgrade/GithubRelease.cs b/FastGithub.Upgrade/GithubRelease.cs deleted file mode 100644 index cf076a9..0000000 --- a/FastGithub.Upgrade/GithubRelease.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using System.Text; -using System.Text.Json.Serialization; - -namespace FastGithub.Upgrade -{ - /// - /// 发行记录 - /// - public class GithubRelease - { - /// - /// 标签名 - /// - [JsonPropertyName("tag_name")] - public string TagName { get; set; } = string.Empty; - - /// - /// 是否预览版本 - /// - [JsonPropertyName("prerelease")] - public bool Prerelease { get; set; } - - /// - /// 发行说明 - /// - [JsonPropertyName("body")] - public string Body { get; set; } = string.Empty; - - /// - /// 发行时间 - /// - - [JsonPropertyName("created_at")] - public DateTime CreatedAt { get; set; } - - /// - /// 下载页面 - /// - [AllowNull] - [JsonPropertyName("html_url")] - public Uri HtmlUrl { get; set; } - - - /// - /// 获取产品版本 - /// - /// - public ProductionVersion GetProductionVersion() - { - var version = this.TagName.TrimStart('v', 'V'); - return ProductionVersion.Parse(version); - } - - public override string ToString() - { - return new StringBuilder() - .Append("最新版本:").AppendLine(this.TagName) - .Append("发布时间:").AppendLine(this.CreatedAt.ToString()) - .AppendLine("更新内容:").AppendLine(this.Body) - .ToString(); - } - } -} diff --git a/FastGithub.Upgrade/GithubRequestMessage.cs b/FastGithub.Upgrade/GithubRequestMessage.cs deleted file mode 100644 index 8428d5b..0000000 --- a/FastGithub.Upgrade/GithubRequestMessage.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Net.Http; -using System.Net.Http.Headers; - -namespace FastGithub.Upgrade -{ - /// - /// github请求消息 - /// - class GithubRequestMessage : HttpRequestMessage - { - public GithubRequestMessage() - { - this.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); - this.Headers.UserAgent.Add(new ProductInfoHeaderValue(nameof(FastGithub), "1.0")); - } - } -} diff --git a/FastGithub.Upgrade/ProductionVersion.cs b/FastGithub.Upgrade/ProductionVersion.cs deleted file mode 100644 index 52c1c64..0000000 --- a/FastGithub.Upgrade/ProductionVersion.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.Reflection; -using System.Text.RegularExpressions; - -namespace FastGithub.Upgrade -{ - /// - /// 表示产品版本 - /// - public 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); - } - - - /// - /// 获取当前应用程序的产品版本 - /// - /// - public static ProductionVersion? GetAppVersion() - { - var version = Assembly - .GetEntryAssembly()? - .GetCustomAttribute()? - .InformationalVersion; - - return version == null ? null : Parse(version); - } - } -} diff --git a/FastGithub.Upgrade/ServiceCollectionExtensions.cs b/FastGithub.Upgrade/ServiceCollectionExtensions.cs deleted file mode 100644 index be2a873..0000000 --- a/FastGithub.Upgrade/ServiceCollectionExtensions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using FastGithub.Upgrade; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; - -namespace FastGithub -{ - /// - /// 服务注册扩展 - /// - public static class ServiceCollectionExtensions - { - /// - /// 添加升级服务 - /// - /// - /// - public static IServiceCollection AddUpgrade(this IServiceCollection services) - { - services.TryAddSingleton(); - return services; - } - } -} diff --git a/FastGithub.Upgrade/UpgradeService.cs b/FastGithub.Upgrade/UpgradeService.cs deleted file mode 100644 index c340bea..0000000 --- a/FastGithub.Upgrade/UpgradeService.cs +++ /dev/null @@ -1,90 +0,0 @@ -using FastGithub.Configuration; -using FastGithub.DomainResolve; -using FastGithub.Http; -using Microsoft.Extensions.Logging; -using System; -using System.Linq; -using System.Net.Http.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace FastGithub.Upgrade -{ - /// - /// 升级服务 - /// - public class UpgradeService - { - private readonly IDomainResolver domainResolver; - private readonly ILogger logger; - private readonly Uri releasesUri = new("https://api.github.com/repos/xljiulang/fastgithub/releases"); - - /// - /// 升级服务 - /// - /// - /// - public UpgradeService( - IDomainResolver domainResolver, - ILogger logger) - { - this.domainResolver = domainResolver; - this.logger = logger; - } - - /// - /// 进行升级 - /// - /// - /// - public async Task UpgradeAsync(CancellationToken cancellationToken) - { - var currentVersion = ProductionVersion.GetAppVersion(); - if (currentVersion == null) - { - return; - } - - var lastRelease = await this.GetLastedReleaseAsync(cancellationToken); - if (lastRelease == null) - { - return; - } - - var lastedVersion = lastRelease.GetProductionVersion(); - if (lastedVersion.CompareTo(currentVersion) > 0) - { - this.logger.LogInformation($"当前版本{currentVersion}为老版本{Environment.NewLine}请前往{lastRelease.HtmlUrl}下载新版本"); - this.logger.LogInformation(lastRelease.ToString()); - } - else - { - this.logger.LogInformation($"当前版本{currentVersion}已经是最新版本"); - } - } - - /// - /// 获取最新发布 - /// - /// - public async Task GetLastedReleaseAsync(CancellationToken cancellationToken) - { - var domainConfig = new DomainConfig - { - TlsSni = false, - TlsIgnoreNameMismatch = true, - Timeout = TimeSpan.FromSeconds(30d) - }; - - using var request = new GithubRequestMessage - { - RequestUri = this.releasesUri - }; - - using var httpClient = new HttpClient(domainConfig, this.domainResolver); - var response = await httpClient.SendAsync(request, cancellationToken); - var releases = await response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken); - return releases?.FirstOrDefault(item => item.Prerelease == false); - } - } -}