From 8e833103849ed56368832dc72eb04db8705acd8f Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Sun, 10 Jan 2021 23:53:41 +0100 Subject: [PATCH 1/3] Auto-update - Added installer update-logic --- .../General/GeneralSettingsTabViewModel.cs | 2 +- src/Artemis.UI/Services/UpdateService.cs | 103 +++++++++++------- 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/src/Artemis.UI/Screens/Settings/Tabs/General/GeneralSettingsTabViewModel.cs b/src/Artemis.UI/Screens/Settings/Tabs/General/GeneralSettingsTabViewModel.cs index 3b852d910..d6ccc65b3 100644 --- a/src/Artemis.UI/Screens/Settings/Tabs/General/GeneralSettingsTabViewModel.cs +++ b/src/Artemis.UI/Screens/Settings/Tabs/General/GeneralSettingsTabViewModel.cs @@ -276,7 +276,7 @@ namespace Artemis.UI.Screens.Settings.Tabs.General return; CanOfferUpdatesIfFound = false; - bool updateFound = await _updateService.OfferUpdatesIfFound(); + bool updateFound = await _updateService.OfferUpdateIfFound(); if (!updateFound) _messageService.ShowMessage("You are already running the latest Artemis build. (☞゚ヮ゚)☞"); CanOfferUpdatesIfFound = true; diff --git a/src/Artemis.UI/Services/UpdateService.cs b/src/Artemis.UI/Services/UpdateService.cs index fbabfaa8f..8cc364a84 100644 --- a/src/Artemis.UI/Services/UpdateService.cs +++ b/src/Artemis.UI/Services/UpdateService.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.IO; using System.Net.Http; using System.Threading.Tasks; using Artemis.Core; @@ -36,42 +37,15 @@ namespace Artemis.UI.Services _checkForUpdates.SettingChanged += CheckForUpdatesOnSettingChanged; } - public async Task GetLatestBuildNumber() + public async Task AutoUpdate() { - // TODO: The URL is hardcoded, that should change in the future - string latestBuildUrl = ApiUrl + "build/builds?api-version=6.1-preview.6&branchName=refs/heads/master&resultFilter=succeeded&$top=1"; - _logger.Debug("Getting latest build number from {latestBuildUrl}", latestBuildUrl); + if (!_checkForUpdates.Value) + return false; - // Make the request - using HttpClient client = new(); - HttpResponseMessage httpResponseMessage = await client.GetAsync(latestBuildUrl); - - // Ensure it returned correctly - if (!httpResponseMessage.IsSuccessStatusCode) - { - _logger.Warning("Failed to check for updates, request returned {statusCode}", httpResponseMessage.StatusCode); - return 0; - } - - // Parse the response - string response = await httpResponseMessage.Content.ReadAsStringAsync(); - try - { - JToken buildNumberToken = JObject.Parse(response).SelectToken("value[0].buildNumber"); - if (buildNumberToken != null) - return buildNumberToken.Value(); - - _logger.Warning("Failed to find build number at \"value[0].buildNumber\""); - return 0; - } - catch (Exception e) - { - _logger.Warning(e, "Failed to retrieve build info JSON"); - return 0; - } + return await OfferUpdateIfFound(); } - public async Task OfferUpdatesIfFound() + public async Task OfferUpdateIfFound() { _logger.Information("Checking for updates"); @@ -93,6 +67,8 @@ namespace Artemis.UI.Services $"Build {buildNumberDisplay} is available, currently on {Constants.BuildInfo.BuildNumberDisplay}.", PackIconKind.Update ); + + await ApplyUpdate(buildNumber); } else { @@ -113,17 +89,62 @@ namespace Artemis.UI.Services return buildNumber > Constants.BuildInfo.BuildNumber; } - public void ApplyUpdate() + public async Task GetLatestBuildNumber() + { + JToken buildInfo = await GetBuildInfo(1); + JToken buildNumberToken = buildInfo?.SelectToken("value[0].buildNumber"); + + if (buildNumberToken != null) + return buildNumberToken.Value(); + + _logger.Warning("Failed to find build number at \"value[0].buildNumber\""); + return 0; + } + + public async Task ApplyUpdate(double buildNumber) + { + // Get installer build info + JToken buildInfo = await GetBuildInfo(6); + string installerPath = Path.Combine(Constants.ApplicationFolder, "Installer", "Artemis.Installer.exe"); + + // Always update installer if it is missing ^^ + if (!File.Exists(installerPath)) + UpdateInstaller(buildInfo); + // Compare the creation date of the installer with the build date and update if needed + } + + private void UpdateInstaller(JToken buildInfo) { throw new NotImplementedException(); } - public async Task AutoUpdate() + private async Task GetBuildInfo(int buildDefinition) { - if (!_checkForUpdates.Value) - return false; + string latestBuildUrl = ApiUrl + $"build/builds?definitions=6&resultFilter=succeeded&$top={buildDefinition}&api-version=6.1-preview.6"; + _logger.Debug("Getting build info from {latestBuildUrl}", latestBuildUrl); - return await OfferUpdatesIfFound(); + // Make the request + using HttpClient client = new(); + HttpResponseMessage httpResponseMessage = await client.GetAsync(latestBuildUrl); + + // Ensure it returned correctly + if (!httpResponseMessage.IsSuccessStatusCode) + { + _logger.Warning("Getting build info, request returned {statusCode}", httpResponseMessage.StatusCode); + return null; + } + + // Parse the response + string response = await httpResponseMessage.Content.ReadAsStringAsync(); + try + { + return JObject.Parse(response); + } + catch (Exception e) + { + _logger.Warning(e, "Failed to retrieve build info JSON"); + return null; + } } #region Event handlers @@ -145,13 +166,13 @@ namespace Artemis.UI.Services public interface IUpdateService : IArtemisUIService { - Task OfferUpdatesIfFound(); - Task IsUpdateAvailable(); - void ApplyUpdate(); - /// /// If auto-update is enabled this will offer updates if found /// Task AutoUpdate(); + + Task OfferUpdateIfFound(); + Task IsUpdateAvailable(); + Task ApplyUpdate(double buildNumber); } } \ No newline at end of file From e3c906390d4377341d2658492fc0f7c99f19787d Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Tue, 12 Jan 2021 22:32:24 +0100 Subject: [PATCH 2/3] Auto-update - Added update dialog --- .../Settings/Dialogs/UpdateDialogView.xaml | 37 ++++++++++ .../Settings/Dialogs/UpdateDialogViewModel.cs | 31 ++++++++ src/Artemis.UI/Services/UpdateService.cs | 72 +++++++++++++------ 3 files changed, 119 insertions(+), 21 deletions(-) create mode 100644 src/Artemis.UI/Screens/Settings/Dialogs/UpdateDialogView.xaml create mode 100644 src/Artemis.UI/Screens/Settings/Dialogs/UpdateDialogViewModel.cs diff --git a/src/Artemis.UI/Screens/Settings/Dialogs/UpdateDialogView.xaml b/src/Artemis.UI/Screens/Settings/Dialogs/UpdateDialogView.xaml new file mode 100644 index 000000000..be9bacf79 --- /dev/null +++ b/src/Artemis.UI/Screens/Settings/Dialogs/UpdateDialogView.xaml @@ -0,0 +1,37 @@ + + + + Update available + + + A new Artemis update is available! 🥳 + You are currently running build while the latest build is . + Updating Artemis will give you the latest bug(fixes), features and improvements. + + + +