From d787843fd23af177775127b7b0f25143aac734d3 Mon Sep 17 00:00:00 2001 From: Mbucari Date: Mon, 27 Feb 2023 14:07:52 -0700 Subject: [PATCH] Unify upgrade process and add update progress bar --- .../HangoverAvalonia/HangoverAvalonia.csproj | 8 +- .../HangoverWinForms/HangoverWinForms.csproj | 8 +- .../LibationAvalonia/LibationAvalonia.csproj | 11 +- .../ViewModels/MainWindowViewModel.cs | 3 + .../Views/MainWindow.Update.cs | 87 ---------- .../Views/MainWindow.Upgrade.cs | 33 ++++ .../LibationAvalonia/Views/MainWindow.axaml | 13 +- .../Views/MainWindow.axaml.cs | 2 - Source/LibationCli/LibationCli.csproj | 8 +- Source/LibationUiBase/Upgrader.cs | 148 ++++++++++++++++++ Source/LibationWinForms/Form1.Designer.cs | 31 +++- Source/LibationWinForms/Form1.Upgrade.cs | 34 ++++ Source/LibationWinForms/Form1.cs | 1 + .../LibationWinForms/LibationWinForms.csproj | 10 +- Source/LibationWinForms/Program.cs | 28 ---- Source/LibationWinForms/Updater.cs | 57 ------- .../LinuxConfigApp/LinuxConfigApp.csproj | 8 +- .../MacOSConfigApp/MacOSConfigApp.csproj | 8 +- .../PublishProfiles/WindowsProfile.pubxml | 2 +- .../WindowsConfigApp/WindowsConfigApp.csproj | 11 +- 20 files changed, 264 insertions(+), 247 deletions(-) delete mode 100644 Source/LibationAvalonia/Views/MainWindow.Update.cs create mode 100644 Source/LibationAvalonia/Views/MainWindow.Upgrade.cs create mode 100644 Source/LibationUiBase/Upgrader.cs create mode 100644 Source/LibationWinForms/Form1.Upgrade.cs delete mode 100644 Source/LibationWinForms/Updater.cs diff --git a/Source/HangoverAvalonia/HangoverAvalonia.csproj b/Source/HangoverAvalonia/HangoverAvalonia.csproj index c00bc51c..6db44c40 100644 --- a/Source/HangoverAvalonia/HangoverAvalonia.csproj +++ b/Source/HangoverAvalonia/HangoverAvalonia.csproj @@ -21,13 +21,7 @@ - - en;es + en diff --git a/Source/HangoverWinForms/HangoverWinForms.csproj b/Source/HangoverWinForms/HangoverWinForms.csproj index dd37f2e3..78b131cf 100644 --- a/Source/HangoverWinForms/HangoverWinForms.csproj +++ b/Source/HangoverWinForms/HangoverWinForms.csproj @@ -16,13 +16,7 @@ - - en;es + en - copyused true libation.ico Libation @@ -17,13 +15,7 @@ - - en;es + en @@ -109,6 +101,7 @@ + diff --git a/Source/LibationAvalonia/ViewModels/MainWindowViewModel.cs b/Source/LibationAvalonia/ViewModels/MainWindowViewModel.cs index 0fca0bb4..8f5fa93a 100644 --- a/Source/LibationAvalonia/ViewModels/MainWindowViewModel.cs +++ b/Source/LibationAvalonia/ViewModels/MainWindowViewModel.cs @@ -25,6 +25,9 @@ namespace LibationAvalonia.ViewModels public ProcessQueueViewModel ProcessQueue { get; } = new ProcessQueueViewModel(); public ProductsDisplayViewModel ProductsDisplay { get; } = new ProductsDisplayViewModel(); + private double? _downloadProgress = null; + public double? DownloadProgress { get => _downloadProgress; set => this.RaiseAndSetIfChanged(ref _downloadProgress, value); } + /// Library filterting query public string FilterString { get => _filterString; set => this.RaiseAndSetIfChanged(ref _filterString, value); } diff --git a/Source/LibationAvalonia/Views/MainWindow.Update.cs b/Source/LibationAvalonia/Views/MainWindow.Update.cs deleted file mode 100644 index e466c41a..00000000 --- a/Source/LibationAvalonia/Views/MainWindow.Update.cs +++ /dev/null @@ -1,87 +0,0 @@ -using AppScaffolding; -using LibationAvalonia.Dialogs; -using LibationFileManager; -using System; -using System.IO; -using System.Threading.Tasks; - -namespace LibationAvalonia.Views -{ - public partial class MainWindow - { - private void Configure_Update() - { - Opened += async (_, _) => await checkForUpdates(); - } - - private async Task checkForUpdates() - { - async Task downloadUpdate(UpgradeProperties upgradeProperties) - { - if (upgradeProperties.ZipUrl is null) - { - Serilog.Log.Logger.Warning("Download link for new version not found"); - return null; - } - - //Silently download the update in the background, save it to a temp file. - - var zipFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(upgradeProperties.ZipUrl)); - - Serilog.Log.Logger.Information($"Downloading {zipFile}"); - - try - { - System.Net.Http.HttpClient cli = new(); - using var fs = File.OpenWrite(zipFile); - using var dlStream = await cli.GetStreamAsync(new Uri(upgradeProperties.ZipUrl)); - await dlStream.CopyToAsync(fs); - } - catch (Exception ex) - { - Serilog.Log.Logger.Error(ex, "Failed to download the update: {pdate}", upgradeProperties.ZipUrl); - return null; - } - return zipFile; - } - - try - { - var upgradeProperties = await Task.Run(LibationScaffolding.GetLatestRelease); - if (upgradeProperties is null) return; - - const string ignoreUpdate = "IgnoreUpdate"; - var config = Configuration.Instance; - - if (config.GetString(propertyName: ignoreUpdate) == upgradeProperties.LatestRelease.ToString()) - return; - - var interop = InteropFactory.Create(); - - if (!interop.CanUpdate) - Serilog.Log.Logger.Information("Can't perform update automatically"); - - var notificationResult = await new UpgradeNotificationDialog(upgradeProperties, interop.CanUpdate).ShowDialog(this); - - if (notificationResult == DialogResult.Ignore) - config.SetString(upgradeProperties.LatestRelease.ToString(), ignoreUpdate); - - if (notificationResult != DialogResult.OK) return; - - //Download the update file in the background, - string updateBundle = await downloadUpdate(upgradeProperties); - - if (string.IsNullOrEmpty(updateBundle) || !File.Exists(updateBundle)) return; - - //Install the update - Serilog.Log.Logger.Information($"Begin running auto-updater"); - interop.InstallUpdate(updateBundle); - Serilog.Log.Logger.Information($"Completed running auto-updater"); - } - catch (Exception ex) - { - Serilog.Log.Logger.Error(ex, "An error occured while checking for app updates."); - } - } - } -} diff --git a/Source/LibationAvalonia/Views/MainWindow.Upgrade.cs b/Source/LibationAvalonia/Views/MainWindow.Upgrade.cs new file mode 100644 index 00000000..c7402ba8 --- /dev/null +++ b/Source/LibationAvalonia/Views/MainWindow.Upgrade.cs @@ -0,0 +1,33 @@ +using Avalonia.Threading; +using LibationAvalonia.Dialogs; +using LibationUiBase; +using System.Threading.Tasks; + +namespace LibationAvalonia.Views +{ + public partial class MainWindow + { + private void Configure_Update() + { + setProgressVisible(false); +#if !DEBUG + var upgrader = new Upgrader(); + upgrader.DownloadProgress += async (_, e) => await Dispatcher.UIThread.InvokeAsync(() => _viewModel.DownloadProgress = e.ProgressPercentage); + upgrader.DownloadBegin += async (_, _) => await Dispatcher.UIThread.InvokeAsync(() => setProgressVisible(false)); + upgrader.DownloadCompleted += async (_, _) => await Dispatcher.UIThread.InvokeAsync(() => setProgressVisible(true)); + + Opened += async (_, _) => await upgrader.CheForUpgradeAsync(UpgradeAvailable); +#endif + } + + private void setProgressVisible(bool visible) => _viewModel.DownloadProgress = visible ? 0 : null; + + private async Task UpgradeAvailable(UpgradeEventArgs e) + { + var notificationResult = await new UpgradeNotificationDialog(e.UpgradeProperties, e.CapUpgrade).ShowDialog(this); + + e.Ignore = notificationResult == DialogResult.Ignore; + e.InstallUpdate = notificationResult == DialogResult.OK; + } + } +} diff --git a/Source/LibationAvalonia/Views/MainWindow.axaml b/Source/LibationAvalonia/Views/MainWindow.axaml index c1a44e47..8b2fffb8 100644 --- a/Source/LibationAvalonia/Views/MainWindow.axaml +++ b/Source/LibationAvalonia/Views/MainWindow.axaml @@ -194,9 +194,16 @@ - - - + + + + + + + + diff --git a/Source/LibationAvalonia/Views/MainWindow.axaml.cs b/Source/LibationAvalonia/Views/MainWindow.axaml.cs index 33094e6f..95353f0b 100644 --- a/Source/LibationAvalonia/Views/MainWindow.axaml.cs +++ b/Source/LibationAvalonia/Views/MainWindow.axaml.cs @@ -40,9 +40,7 @@ namespace LibationAvalonia.Views Configure_Export(); Configure_Settings(); Configure_ProcessQueue(); -#if !DEBUG Configure_Update(); -#endif Configure_Filter(); // misc which belongs in winforms app but doesn't have a UI element Configure_NonUI(); diff --git a/Source/LibationCli/LibationCli.csproj b/Source/LibationCli/LibationCli.csproj index b61341eb..04926587 100644 --- a/Source/LibationCli/LibationCli.csproj +++ b/Source/LibationCli/LibationCli.csproj @@ -11,13 +11,7 @@ - - en;es + en - - en;es + en @@ -44,7 +37,6 @@ - diff --git a/Source/LibationWinForms/Program.cs b/Source/LibationWinForms/Program.cs index 28148e21..5afd05b1 100644 --- a/Source/LibationWinForms/Program.cs +++ b/Source/LibationWinForms/Program.cs @@ -51,9 +51,6 @@ namespace LibationWinForms MessageBoxLib.VerboseLoggingWarning_ShowIfTrue(); -#if !DEBUG - checkForUpdate(); -#endif // logging is init'd here AppScaffolding.LibationScaffolding.RunPostMigrationScaffolding(config); } @@ -165,31 +162,6 @@ namespace LibationWinForms // - long running. won't get a chance to finish in cli. don't move to app scaffolding } - private static void checkForUpdate() - { - AppScaffolding.UpgradeProperties upgradeProperties; - - try - { - upgradeProperties = AppScaffolding.LibationScaffolding.GetLatestRelease(); - if (upgradeProperties is null) - return; - } - catch (Exception ex) - { - MessageBoxLib.ShowAdminAlert(null, "Error checking for update", "Error checking for update", ex); - return; - } - - if (upgradeProperties.ZipUrl is null) - { - MessageBox.Show(upgradeProperties.HtmlUrl, "New version available"); - return; - } - - Updater.Run(upgradeProperties); - } - private static void postLoggingGlobalExceptionHandling() { // this line is all that's needed for strict handling diff --git a/Source/LibationWinForms/Updater.cs b/Source/LibationWinForms/Updater.cs deleted file mode 100644 index 3bb6a926..00000000 --- a/Source/LibationWinForms/Updater.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Windows.Forms; -using AppScaffolding; -using AutoUpdaterDotNET; -using LibationFileManager; -using LibationWinForms.Dialogs; - -namespace LibationWinForms -{ - public static class Updater - { - public static void Run(UpgradeProperties upgradeProperties) - { - string latestVersionOnServer = upgradeProperties.LatestRelease.ToString(); - string downloadZipUrl = upgradeProperties.ZipUrl; - AutoUpdater.ParseUpdateInfoEvent += - args => args.UpdateInfo = new() - { - CurrentVersion = latestVersionOnServer, - DownloadURL = downloadZipUrl, - ChangelogURL = LibationScaffolding.RepositoryLatestUrl - }; - - void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args) - { - if (args is null || !args.IsUpdateAvailable) - return; - - const string ignoreUpdate = "IgnoreUpdate"; - var config = Configuration.Instance; - - if (config.GetString(propertyName: ignoreUpdate) == args.CurrentVersion) - return; - - var notificationResult = new UpgradeNotificationDialog(upgradeProperties).ShowDialog(); - - if (notificationResult == DialogResult.Ignore) - config.SetString(upgradeProperties.LatestRelease.ToString(), ignoreUpdate); - - if (notificationResult != DialogResult.Yes) return; - - try - { - Serilog.Log.Logger.Information("Start upgrade. {@DebugInfo}", new { CurrentlyInstalled = args.InstalledVersion, TargetVersion = args.CurrentVersion }); - AutoUpdater.DownloadUpdate(args); - } - catch (Exception ex) - { - MessageBoxLib.ShowAdminAlert(null, "Error downloading update", "Error downloading update", ex); - } - } - - AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent; - AutoUpdater.Start(LibationScaffolding.RepositoryLatestUrl); - } - } -} diff --git a/Source/LoadByOS/LinuxConfigApp/LinuxConfigApp.csproj b/Source/LoadByOS/LinuxConfigApp/LinuxConfigApp.csproj index 855b7034..b114d7cd 100644 --- a/Source/LoadByOS/LinuxConfigApp/LinuxConfigApp.csproj +++ b/Source/LoadByOS/LinuxConfigApp/LinuxConfigApp.csproj @@ -11,13 +11,7 @@ - - en;es + en diff --git a/Source/LoadByOS/MacOSConfigApp/MacOSConfigApp.csproj b/Source/LoadByOS/MacOSConfigApp/MacOSConfigApp.csproj index 865067f1..9d80ef2e 100644 --- a/Source/LoadByOS/MacOSConfigApp/MacOSConfigApp.csproj +++ b/Source/LoadByOS/MacOSConfigApp/MacOSConfigApp.csproj @@ -11,13 +11,7 @@ - - en;es + en diff --git a/Source/LoadByOS/WindowsConfigApp/Properties/PublishProfiles/WindowsProfile.pubxml b/Source/LoadByOS/WindowsConfigApp/Properties/PublishProfiles/WindowsProfile.pubxml index 03ef4015..75e91e8b 100644 --- a/Source/LoadByOS/WindowsConfigApp/Properties/PublishProfiles/WindowsProfile.pubxml +++ b/Source/LoadByOS/WindowsConfigApp/Properties/PublishProfiles/WindowsProfile.pubxml @@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. Release Any CPU - ..\..\bin\Publish\classic + C:\Users\mbuca\OneDrive\Projects\Libation\Source\bin\Publish\Windows-chardonnay FileSystem net7.0-windows win-x64 diff --git a/Source/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj b/Source/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj index 0dba0cbf..d547a2d4 100644 --- a/Source/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj +++ b/Source/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj @@ -4,22 +4,15 @@ WinExe net7.0-windows true - true + true enable true - win-x64 false false - - en;es + en