BililiveRecorder/BililiveRecorder.WPF/Update.cs

88 lines
3.0 KiB
C#
Raw Normal View History

2021-02-23 18:03:37 +08:00
using System;
using System.Threading.Tasks;
using Serilog;
using Squirrel;
#nullable enable
namespace BililiveRecorder.WPF
{
internal class Update
{
private readonly ILogger logger;
private Task updateInProgress = Task.CompletedTask;
public Update(ILogger logger)
{
2021-05-21 21:03:53 +08:00
this.logger = logger.ForContext<Update>();
2021-02-23 18:03:37 +08:00
}
public async Task UpdateAsync()
{
if (!this.updateInProgress.IsCompleted)
await this.updateInProgress;
this.updateInProgress = this.RealUpdateAsync();
await this.updateInProgress;
}
public async Task WaitForUpdatesOnShutdownAsync() => await this.updateInProgress.ContinueWith(ex => { }, TaskScheduler.Default).ConfigureAwait(false);
private async Task RealUpdateAsync()
{
this.logger.Debug("Checking updates");
try
{
using var updateManager = new UpdateManager(@"https://soft.danmuji.org/BililiveRecorder/");
2022-07-01 18:53:05 +08:00
if (!updateManager.IsInstalledApp)
{
this.logger.Information("当前不是安装版,不检查是否有新版本。");
return;
}
2021-02-23 18:03:37 +08:00
var ignoreDeltaUpdates = false;
retry:
try
{
var updateInfo = await updateManager.CheckForUpdate(ignoreDeltaUpdates);
if (updateInfo.ReleasesToApply.Count == 0)
{
2022-07-01 18:53:05 +08:00
this.logger.Information("当前运行的是最新版本 {BuildVersion} ({InstalledVersion})",
GitVersionInformation.FullSemVer,
2021-02-23 18:03:37 +08:00
updateInfo.CurrentlyInstalledVersion?.Version?.ToString() ?? "×");
}
else
{
2022-07-01 18:53:05 +08:00
this.logger.Information("有新版本 {RemoteVersion},当前本地是 {BuildVersion} ({InstalledVersion})",
2021-02-23 18:03:37 +08:00
updateInfo.FutureReleaseEntry?.Version?.ToString() ?? "×",
2022-07-01 18:53:05 +08:00
GitVersionInformation.FullSemVer,
2021-02-23 18:03:37 +08:00
updateInfo.CurrentlyInstalledVersion?.Version?.ToString() ?? "×");
await updateManager.DownloadReleases(updateInfo.ReleasesToApply);
await updateManager.ApplyReleases(updateInfo);
this.logger.Information("更新完成");
}
}
catch (Exception ex)
{
if (ignoreDeltaUpdates == false)
{
ignoreDeltaUpdates = true;
this.logger.Debug(ex, "第一次检查更新出错");
goto retry;
}
throw;
}
}
catch (Exception ex)
{
2022-07-01 18:53:05 +08:00
this.logger.Warning(ex, "检查更新时发生错误");
2021-02-23 18:03:37 +08:00
}
}
}
}