mirror of
https://github.com/real-zony/ZonyLrcToolsX.git
synced 2025-07-01 20:30:41 +00:00
feat: Add auto-detect update feature.
This commit is contained in:
parent
a11ef70021
commit
d19b1d8d2a
26
src/ZonyLrcTools.Cli/Infrastructure/UpdaterHostedService.cs
Normal file
26
src/ZonyLrcTools.Cli/Infrastructure/UpdaterHostedService.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using ZonyLrcTools.Common.Updater;
|
||||||
|
|
||||||
|
namespace ZonyLrcTools.Cli.Infrastructure;
|
||||||
|
|
||||||
|
public class UpdaterHostedService : IHostedService
|
||||||
|
{
|
||||||
|
private readonly IUpdater _updater;
|
||||||
|
|
||||||
|
public UpdaterHostedService(IUpdater updater)
|
||||||
|
{
|
||||||
|
_updater = updater;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
await _updater.CheckUpdateAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,7 @@ using Serilog.Events;
|
|||||||
using Serilog.Sinks.SystemConsole.Themes;
|
using Serilog.Sinks.SystemConsole.Themes;
|
||||||
using ZonyLrcTools.Cli.Commands;
|
using ZonyLrcTools.Cli.Commands;
|
||||||
using ZonyLrcTools.Cli.Commands.SubCommand;
|
using ZonyLrcTools.Cli.Commands.SubCommand;
|
||||||
|
using ZonyLrcTools.Cli.Infrastructure;
|
||||||
using ZonyLrcTools.Cli.Infrastructure.Logging;
|
using ZonyLrcTools.Cli.Infrastructure.Logging;
|
||||||
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
|
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
|
||||||
using ZonyLrcTools.Common.Infrastructure.Exceptions;
|
using ZonyLrcTools.Common.Infrastructure.Exceptions;
|
||||||
@ -90,6 +91,7 @@ namespace ZonyLrcTools.Cli
|
|||||||
services.BeginAutoDependencyInject<IWarpHttpClient>();
|
services.BeginAutoDependencyInject<IWarpHttpClient>();
|
||||||
services.ConfigureConfiguration();
|
services.ConfigureConfiguration();
|
||||||
services.ConfigureToolService();
|
services.ConfigureToolService();
|
||||||
|
services.AddHostedService<UpdaterHostedService>();
|
||||||
})
|
})
|
||||||
.RunCommandLineApplicationAsync<Program>(args);
|
.RunCommandLineApplicationAsync<Program>(args);
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,9 @@ using ZonyLrcTools.Common.Updater.JsonModel;
|
|||||||
|
|
||||||
namespace ZonyLrcTools.Common.Updater;
|
namespace ZonyLrcTools.Common.Updater;
|
||||||
|
|
||||||
public class DefaultUpdater : ISingletonDependency
|
public class DefaultUpdater : IUpdater, ISingletonDependency
|
||||||
{
|
{
|
||||||
public const string UpdateUrl = "https://api.zony.me/lrc-tools/update";
|
public const string UpdateUrl = "https://api.myzony.com/lrc-tools/update";
|
||||||
|
|
||||||
private readonly IWarpHttpClient _warpHttpClient;
|
private readonly IWarpHttpClient _warpHttpClient;
|
||||||
private readonly ILogger<DefaultUpdater> _logger;
|
private readonly ILogger<DefaultUpdater> _logger;
|
||||||
@ -23,7 +23,7 @@ public class DefaultUpdater : ISingletonDependency
|
|||||||
|
|
||||||
public async Task CheckUpdateAsync()
|
public async Task CheckUpdateAsync()
|
||||||
{
|
{
|
||||||
var response = await _warpHttpClient.GetAsync<NewVersionResponse>(UpdateUrl);
|
var response = await _warpHttpClient.GetAsync<NewVersionResponse?>(UpdateUrl);
|
||||||
if (response == null)
|
if (response == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -35,13 +35,13 @@ public class DefaultUpdater : ISingletonDependency
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var importantItem = response.Items.FirstOrDefault(x => x.ItemType == NewVersionItemType.Important);
|
var importantItem = response.Items?.FirstOrDefault(x => x.ItemType == NewVersionItemType.Important);
|
||||||
if (importantItem != null)
|
if (importantItem != null)
|
||||||
{
|
{
|
||||||
_logger.LogWarning($"发现了新版本,请点击下面的链接进行更新:{importantItem.Url}");
|
_logger.LogWarning($"发现了新版本,请点击下面的链接进行更新:{importantItem.Url}");
|
||||||
_logger.LogWarning($"最新版本号:{response.NewVersion},当前版本号: ${currentVersion}");
|
_logger.LogWarning($"最新版本号:{response.NewVersion},当前版本号: ${currentVersion}");
|
||||||
_logger.LogWarning($"更新内容:{response.NewVersionDescription}");
|
_logger.LogWarning($"更新内容:{response.NewVersionDescription}");
|
||||||
|
|
||||||
if (OperatingSystem.IsWindows())
|
if (OperatingSystem.IsWindows())
|
||||||
{
|
{
|
||||||
Process.Start("explorer.exe", importantItem.Url);
|
Process.Start("explorer.exe", importantItem.Url);
|
||||||
|
6
src/ZonyLrcTools.Common/Updater/IUpdater.cs
Normal file
6
src/ZonyLrcTools.Common/Updater/IUpdater.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace ZonyLrcTools.Common.Updater;
|
||||||
|
|
||||||
|
public interface IUpdater
|
||||||
|
{
|
||||||
|
Task CheckUpdateAsync();
|
||||||
|
}
|
@ -2,7 +2,7 @@ namespace ZonyLrcTools.Common.Updater.JsonModel;
|
|||||||
|
|
||||||
public class NewVersionItem
|
public class NewVersionItem
|
||||||
{
|
{
|
||||||
public string Url { get; set; }
|
public string? Url { get; set; }
|
||||||
|
|
||||||
public NewVersionItemType ItemType { get; set; }
|
public NewVersionItemType ItemType { get; set; }
|
||||||
}
|
}
|
@ -2,6 +2,6 @@ namespace ZonyLrcTools.Common.Updater.JsonModel;
|
|||||||
|
|
||||||
public enum NewVersionItemType
|
public enum NewVersionItemType
|
||||||
{
|
{
|
||||||
Important,
|
Important = 1,
|
||||||
NormalBinaryFile
|
NormalBinaryFile
|
||||||
}
|
}
|
@ -2,11 +2,11 @@ namespace ZonyLrcTools.Common.Updater.JsonModel;
|
|||||||
|
|
||||||
public class NewVersionResponse
|
public class NewVersionResponse
|
||||||
{
|
{
|
||||||
public Version NewVersion { get; set; }
|
public Version? NewVersion { get; set; }
|
||||||
|
|
||||||
public string NewVersionDescription { get; set; }
|
public string? NewVersionDescription { get; set; }
|
||||||
|
|
||||||
public List<NewVersionItem> Items { get; set; }
|
public List<NewVersionItem>? Items { get; set; }
|
||||||
|
|
||||||
public DateTime UpdateTime { get; set; }
|
public DateTime? UpdateTime { get; set; }
|
||||||
}
|
}
|
@ -4,6 +4,7 @@
|
|||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<Version>4.0.0.49</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
using ZonyLrcTools.Common.Updater;
|
||||||
|
using ZonyLrcTools.Common.Updater.JsonModel;
|
||||||
|
|
||||||
|
namespace ZonyLrcTools.Tests.Infrastructure.Updater;
|
||||||
|
|
||||||
|
public class UpdaterTests : TestBase
|
||||||
|
{
|
||||||
|
private readonly IUpdater _updater;
|
||||||
|
|
||||||
|
public UpdaterTests()
|
||||||
|
{
|
||||||
|
_updater = GetService<IUpdater>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CheckUpdateAsync()
|
||||||
|
{
|
||||||
|
var response = new NewVersionResponse
|
||||||
|
{
|
||||||
|
NewVersion = new Version(0, 0, 1, 49),
|
||||||
|
NewVersionDescription = "这里是新版本描述",
|
||||||
|
UpdateTime = DateTime.Now,
|
||||||
|
Items = new List<NewVersionItem>
|
||||||
|
{
|
||||||
|
new NewVersionItem
|
||||||
|
{
|
||||||
|
ItemType = NewVersionItemType.Important,
|
||||||
|
Url = "https://github.com/real-zony/ZonyLrcToolsX/releases/tag/ZonyLrcToolsX_Alpha.2022092449"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var responseString = JsonConvert.SerializeObject(response);
|
||||||
|
responseString.ShouldNotBeNull();
|
||||||
|
|
||||||
|
await _updater.CheckUpdateAsync();
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ using System;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using ZonyLrcTools.Cli;
|
using ZonyLrcTools.Cli;
|
||||||
using ZonyLrcTools.Cli.Commands;
|
using ZonyLrcTools.Cli.Commands;
|
||||||
|
using ZonyLrcTools.Common;
|
||||||
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
|
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
|
||||||
|
|
||||||
namespace ZonyLrcTools.Tests
|
namespace ZonyLrcTools.Tests
|
||||||
@ -22,6 +23,7 @@ namespace ZonyLrcTools.Tests
|
|||||||
var service = new ServiceCollection();
|
var service = new ServiceCollection();
|
||||||
|
|
||||||
service.BeginAutoDependencyInject<Program>();
|
service.BeginAutoDependencyInject<Program>();
|
||||||
|
service.BeginAutoDependencyInject<MusicInfo>();
|
||||||
service.ConfigureToolService();
|
service.ConfigureToolService();
|
||||||
service.ConfigureConfiguration();
|
service.ConfigureConfiguration();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user