mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-06 23:16:07 +00:00
Implement auto-update via Onova
This commit is contained in:
@@ -14,7 +14,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
|
||||
<PackageReference Include="Onova" Version="1.0.0" />
|
||||
<PackageReference Include="Tyrrrz.Extensions" Version="1.5.0" />
|
||||
<PackageReference Include="Tyrrrz.Settings" Version="1.3.2" />
|
||||
</ItemGroup>
|
||||
|
||||
14
DiscordChatExporter.Core/Services/IUpdateService.cs
Normal file
14
DiscordChatExporter.Core/Services/IUpdateService.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiscordChatExporter.Core.Services
|
||||
{
|
||||
public interface IUpdateService
|
||||
{
|
||||
Task<Version> CheckForUpdatesAsync();
|
||||
|
||||
Task PrepareUpdateAsync();
|
||||
|
||||
Task ApplyUpdateAsync(bool restart = true);
|
||||
}
|
||||
}
|
||||
61
DiscordChatExporter.Core/Services/UpdateService.cs
Normal file
61
DiscordChatExporter.Core/Services/UpdateService.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Onova;
|
||||
using Onova.Services;
|
||||
|
||||
namespace DiscordChatExporter.Core.Services
|
||||
{
|
||||
public class UpdateService : IUpdateService
|
||||
{
|
||||
private readonly UpdateManager _updateManager;
|
||||
|
||||
private Version _lastVersion;
|
||||
private bool _applied;
|
||||
|
||||
public UpdateService()
|
||||
{
|
||||
_updateManager = new UpdateManager(
|
||||
new GithubPackageResolver("Tyrrrz", "DiscordChatExporter", "DiscordChatExporter.zip"),
|
||||
new ZipPackageExtractor());
|
||||
}
|
||||
|
||||
public async Task<Version> CheckForUpdatesAsync()
|
||||
{
|
||||
#if DEBUG
|
||||
// Never update in DEBUG mode
|
||||
return null;
|
||||
#endif
|
||||
|
||||
// Remove some junk left over from last update
|
||||
_updateManager.Cleanup();
|
||||
|
||||
// Check for updates
|
||||
var check = await _updateManager.CheckForUpdatesAsync();
|
||||
|
||||
// Return latest version or null if running latest version already
|
||||
return check.CanUpdate ? _lastVersion = check.LastVersion : null;
|
||||
}
|
||||
|
||||
public async Task PrepareUpdateAsync()
|
||||
{
|
||||
if (_lastVersion == null)
|
||||
return;
|
||||
|
||||
// Download and prepare update
|
||||
await _updateManager.PreparePackageAsync(_lastVersion);
|
||||
}
|
||||
|
||||
public async Task ApplyUpdateAsync(bool restart = true)
|
||||
{
|
||||
if (_lastVersion == null)
|
||||
return;
|
||||
if (_applied)
|
||||
return;
|
||||
|
||||
// Enqueue an update
|
||||
await _updateManager.EnqueueApplyPackageAsync(_lastVersion, restart);
|
||||
|
||||
_applied = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user