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 DataLayer;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using AppScaffolding;
namespace LibationAvalonia.Views namespace LibationAvalonia.Views
{ {
@ -53,7 +54,7 @@ namespace LibationAvalonia.Views
this.LibraryLoaded += MainWindow_LibraryLoaded; this.LibraryLoaded += MainWindow_LibraryLoaded;
LibraryCommands.LibrarySizeChanged += async (_, _) => await _viewModel.ProductsDisplay.DisplayBooks(DbContexts.GetLibrary_Flat_NoTracking(includeParents: true)); LibraryCommands.LibrarySizeChanged += async (_, _) => await _viewModel.ProductsDisplay.DisplayBooks(DbContexts.GetLibrary_Flat_NoTracking(includeParents: true));
Closing += (_,_) => this.SaveSizeAndLocation(Configuration.Instance); Closing += (_, _) => this.SaveSizeAndLocation(Configuration.Instance);
} }
Opened += MainWindow_Opened; Opened += MainWindow_Opened;
Closing += MainWindow_Closing; Closing += MainWindow_Closing;
@ -67,50 +68,63 @@ namespace LibationAvalonia.Views
private async void MainWindow_Opened(object sender, EventArgs e) private async void MainWindow_Opened(object sender, EventArgs e)
{ {
#if !DEBUG #if !DEBUG
if (App.IsWindows)
{
try 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);
} }
catch(Exception ex) else if (App.IsUnix)
{
}
}
catch (Exception ex)
{ {
Serilog.Log.Logger.Error(ex, "An error occured while checking for app updates."); Serilog.Log.Logger.Error(ex, "An error occured while checking for app updates.");
return; return;
} }
}
#endif #endif
} }
private async Task checkForAndDownloadUpdate() private async Task<(string zipFile, UpgradeProperties release)> downloadUpdate(LibationScaffolding.ReleaseIdentifier releaseID)
{ {
AppScaffolding.UpgradeProperties upgradeProperties; UpgradeProperties upgradeProperties;
try try
{ {
upgradeProperties = AppScaffolding.LibationScaffolding.GetLatestRelease(AppScaffolding.LibationScaffolding.ReleaseIdentifier.WindowsAvalonia); upgradeProperties = LibationScaffolding.GetLatestRelease(releaseID);
if (upgradeProperties is null) if (upgradeProperties is null)
return; return (null,null);
} }
catch (Exception ex) catch (Exception ex)
{ {
Serilog.Log.Logger.Error(ex, "Failed to check for update"); Serilog.Log.Logger.Error(ex, "Failed to check for update");
return; return (null, null);
} }
if (upgradeProperties.ZipUrl is null) if (upgradeProperties.ZipUrl is null)
{ {
Serilog.Log.Logger.Information("Download link for new version not found"); 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. //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 try
{ {
System.Net.Http.HttpClient cli = new(); 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))) using (var dlStream = await cli.GetStreamAsync(new Uri(upgradeProperties.ZipUrl)))
await dlStream.CopyToAsync(fs); await dlStream.CopyToAsync(fs);
@ -119,18 +133,18 @@ namespace LibationAvalonia.Views
catch (Exception ex) catch (Exception ex)
{ {
Serilog.Log.Logger.Error(ex, "Failed to download the update: {pdate}", upgradeProperties.ZipUrl); 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); private void runWindowsUpgrader(string zipFile)
{
if (result != DialogResult.Yes)
return;
var thisExe = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; var thisExe = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
var thisDir = System.IO.Path.GetDirectoryName(thisExe); 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"); var zipExtractor = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "ZipExtractor.exe");