Modularize update process

This commit is contained in:
Michael Bucari-Tovo 2022-07-28 17:18:43 -06:00
parent 070ed1d373
commit d69ff24c2d

View File

@ -9,6 +9,7 @@ using LibationFileManager;
using DataLayer;
using System.Collections.Generic;
using System.Threading.Tasks;
using AppScaffolding;
namespace LibationAvalonia.Views
{
@ -67,50 +68,63 @@ namespace LibationAvalonia.Views
private async void MainWindow_Opened(object sender, EventArgs e)
{
#if !DEBUG
if (App.IsWindows)
{
try
{
await Task.Run(checkForAndDownloadUpdate);
(string zipFile, UpgradeProperties upgradeProperties) = await Task.Run(() => downloadUpdate(App.IsWindows ? LibationScaffolding.ReleaseIdentifier.WindowsAvalonia : LibationScaffolding.ReleaseIdentifier.LinuxAvalonia));
if (string.IsNullOrEmpty(zipFile) || !System.IO.File.Exists(zipFile))
return;
var result = MessageBox.Show($"{upgradeProperties.HtmlUrl}\r\n\r\nWould you like to upgrade now?", "New version available", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result != DialogResult.Yes)
return;
if (App.IsWindows)
{
runWindowsUpgrader(zipFile);
}
else if (App.IsUnix)
{
}
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "An error occured while checking for app updates.");
return;
}
}
#endif
}
private async Task checkForAndDownloadUpdate()
private async Task<(string zipFile, UpgradeProperties release)> downloadUpdate(LibationScaffolding.ReleaseIdentifier releaseID)
{
AppScaffolding.UpgradeProperties upgradeProperties;
UpgradeProperties upgradeProperties;
try
{
upgradeProperties = AppScaffolding.LibationScaffolding.GetLatestRelease(AppScaffolding.LibationScaffolding.ReleaseIdentifier.WindowsAvalonia);
upgradeProperties = LibationScaffolding.GetLatestRelease(releaseID);
if (upgradeProperties is null)
return;
return (null,null);
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Failed to check for update");
return;
return (null, null);
}
if (upgradeProperties.ZipUrl is null)
{
Serilog.Log.Logger.Information("Download link for new version not found");
return;
return (null, null);
}
//Silently download the update in the background, save it to a temp file.
var zipPath = System.IO.Path.GetTempFileName();
var zipFile = System.IO.Path.GetTempFileName();
try
{
System.Net.Http.HttpClient cli = new();
using (var fs = System.IO.File.OpenWrite(zipPath))
using (var fs = System.IO.File.OpenWrite(zipFile))
{
using (var dlStream = await cli.GetStreamAsync(new Uri(upgradeProperties.ZipUrl)))
await dlStream.CopyToAsync(fs);
@ -119,18 +133,18 @@ namespace LibationAvalonia.Views
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Failed to download the update: {pdate}", upgradeProperties.ZipUrl);
return;
return (null, null);
}
return (zipFile, upgradeProperties);
}
var result = MessageBox.Show($"{upgradeProperties.HtmlUrl}\r\n\r\nWould you like to upgrade now?", "New version available", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result != DialogResult.Yes)
return;
private void runWindowsUpgrader(string zipFile)
{
var thisExe = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
var thisDir = System.IO.Path.GetDirectoryName(thisExe);
var args = $"--input {zipPath} --output {thisDir} --executable {thisExe}";
var args = $"--input {zipFile} --output {thisDir} --executable {thisExe}";
var zipExtractor = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "ZipExtractor.exe");