Implement auto-update via Onova

This commit is contained in:
Alexey Golub
2018-02-25 17:00:44 +02:00
parent 7bfd645e8e
commit 63c835df88
11 changed files with 187 additions and 22 deletions

View File

@@ -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>

View 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);
}
}

View 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;
}
}
}