完善升级项目
This commit is contained in:
parent
97856bf281
commit
4a014ccfba
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace FastGithub.Upgrade
|
||||
@ -87,5 +88,20 @@ namespace FastGithub.Upgrade
|
||||
var subVersion = productionVersion[verion.Length..];
|
||||
return new ProductionVersion(Version.Parse(verion), subVersion);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前应用程序的产品版本
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static ProductionVersion? GetApplicationVersion()
|
||||
{
|
||||
var version = Assembly
|
||||
.GetEntryAssembly()?
|
||||
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
|
||||
.InformationalVersion;
|
||||
|
||||
return version == null ? null : ProductionVersion.Parse(version);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
50
FastGithub.Upgrade/Release.cs
Normal file
50
FastGithub.Upgrade/Release.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace FastGithub.Upgrade
|
||||
{
|
||||
/// <summary>
|
||||
/// 发行记录
|
||||
/// </summary>
|
||||
sealed class Release
|
||||
{
|
||||
/// <summary>
|
||||
/// 标签名
|
||||
/// </summary>
|
||||
[JsonPropertyName("tag_name")]
|
||||
public string TagName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 发行说明
|
||||
/// </summary>
|
||||
[JsonPropertyName("body")]
|
||||
public string Body { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 发行时间
|
||||
/// </summary>
|
||||
|
||||
[JsonPropertyName("created_at")]
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取产品版本
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public ProductionVersion GetProductionVersion()
|
||||
{
|
||||
return ProductionVersion.Parse(this.TagName);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return new StringBuilder()
|
||||
.Append("最新版本:").AppendLine(this.TagName)
|
||||
.Append("发布时间:").AppendLine(this.CreatedAt.ToString())
|
||||
.AppendLine("更新内容:").AppendLine(this.Body)
|
||||
.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,28 +1,30 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub.Upgrade
|
||||
{
|
||||
/// <summary>
|
||||
/// 升级检查后台服务
|
||||
/// 升级后台服务
|
||||
/// </summary>
|
||||
sealed class UpgradeHostedService : IHostedService
|
||||
{
|
||||
private readonly IServiceScopeFactory serviceScopeFactory;
|
||||
private readonly ILogger<UpgradeHostedService> 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 UpgradeHostedService(ILogger<UpgradeHostedService> logger)
|
||||
/// <summary>
|
||||
/// 升级后台服务
|
||||
/// </summary>
|
||||
/// <param name="serviceScopeFactory"></param>
|
||||
/// <param name="logger"></param>
|
||||
public UpgradeHostedService(
|
||||
IServiceScopeFactory serviceScopeFactory,
|
||||
ILogger<UpgradeHostedService> logger)
|
||||
{
|
||||
this.serviceScopeFactory = serviceScopeFactory;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@ -35,28 +37,13 @@ namespace FastGithub.Upgrade
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentVersion = GetCurrentVersion();
|
||||
if (currentVersion == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var lastRelease = await GetLastedReleaseAsync(cancellationToken);
|
||||
if (lastRelease == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var lastedVersion = ProductionVersion.Parse(lastRelease.TagName);
|
||||
if (lastedVersion.CompareTo(currentVersion) > 0)
|
||||
{
|
||||
this.logger.LogInformation($"您正在使用{currentVersion}版本{Environment.NewLine}请前往{DownloadPage}下载新版本");
|
||||
this.logger.LogInformation(lastRelease.ToString());
|
||||
}
|
||||
using var scope = this.serviceScopeFactory.CreateScope();
|
||||
var upgradeService = scope.ServiceProvider.GetRequiredService<UpgradeService>();
|
||||
await upgradeService.UpgradeAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogWarning($"检测升级信息失败:{ex.Message}");
|
||||
this.logger.LogWarning($"升级失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,56 +51,5 @@ namespace FastGithub.Upgrade
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前版本
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static ProductionVersion? GetCurrentVersion()
|
||||
{
|
||||
var version = Assembly
|
||||
.GetEntryAssembly()?
|
||||
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
|
||||
.InformationalVersion;
|
||||
|
||||
return version == null ? null : ProductionVersion.Parse(version);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
private class Release
|
||||
{
|
||||
[JsonPropertyName("tag_name")]
|
||||
public string TagName { get; set; } = string.Empty;
|
||||
|
||||
|
||||
[JsonPropertyName("body")]
|
||||
public string Body { get; set; } = string.Empty;
|
||||
|
||||
|
||||
[JsonPropertyName("created_at")]
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return new StringBuilder()
|
||||
.Append("最新版本:").AppendLine(this.TagName)
|
||||
.Append("发布时间:").AppendLine(this.CreatedAt.ToString())
|
||||
.AppendLine("更新内容:").AppendLine(this.Body)
|
||||
.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
67
FastGithub.Upgrade/UpgradeService.cs
Normal file
67
FastGithub.Upgrade/UpgradeService.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub.Upgrade
|
||||
{
|
||||
/// <summary>
|
||||
/// 升级服务
|
||||
/// </summary>
|
||||
sealed class UpgradeService
|
||||
{
|
||||
private readonly ILogger<UpgradeService> 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";
|
||||
|
||||
/// <summary>
|
||||
/// 升级服务
|
||||
/// </summary>
|
||||
/// <param name="logger"></param>
|
||||
public UpgradeService(ILogger<UpgradeService> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进行升级
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task UpgradeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var currentVersion = ProductionVersion.GetApplicationVersion();
|
||||
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}请前往{DownloadPage}下载新版本");
|
||||
this.logger.LogInformation(lastRelease.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取最新发布
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<Release?> GetLastedReleaseAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
var releases = await httpClient.GetFromJsonAsync<Release[]>(ReleasesUri, cancellationToken);
|
||||
return releases?.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
using FastGithub.Upgrade;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FastGithub
|
||||
@ -16,7 +15,9 @@ namespace FastGithub
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddAppUpgrade(this IServiceCollection services)
|
||||
{
|
||||
return services.AddHostedService<UpgradeHostedService>();
|
||||
return services
|
||||
.AddTransient<UpgradeService>()
|
||||
.AddHostedService<UpgradeHostedService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user