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
{
///
/// 升级检查后台服务
///
sealed class UpgradeHostedService : IHostedService
{
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 UpgradeHostedService(ILogger logger)
{
this.logger = logger;
}
///
/// 检测版本
///
///
///
public async Task StartAsync(CancellationToken cancellationToken)
{
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());
}
}
catch (Exception ex)
{
this.logger.LogWarning($"检测升级信息失败:{ex.Message}");
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
///
/// 获取当前版本
///
///
private static ProductionVersion? GetCurrentVersion()
{
var version = Assembly
.GetEntryAssembly()?
.GetCustomAttribute()?
.InformationalVersion;
return version == null ? null : ProductionVersion.Parse(version);
}
///
/// 获取最新发布
///
///
private static async Task GetLastedReleaseAsync(CancellationToken cancellationToken)
{
using var httpClient = new HttpClient();
var releases = await httpClient.GetFromJsonAsync(ReleasesUri, cancellationToken);
return releases?.FirstOrDefault();
}
///
/// 发行记录
///
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();
}
}
}
}