1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Updating - Fix crash when client has internet

Updating - Check for updates periodically and when window opens (if auto-update enabled)
Webserver - Don't write wildcard to webserver.txt
This commit is contained in:
Robert 2021-03-11 19:32:25 +01:00
parent 98b9770250
commit f829743b6c
9 changed files with 120 additions and 57 deletions

View File

@ -46,7 +46,14 @@ namespace Artemis.Core
/// </summary>
public static readonly BuildInfo BuildInfo = File.Exists(Path.Combine(ApplicationFolder, "buildinfo.json"))
? JsonConvert.DeserializeObject<BuildInfo>(File.ReadAllText(Path.Combine(ApplicationFolder, "buildinfo.json")))
: new BuildInfo();
: new BuildInfo
{
IsLocalBuild = true,
BuildId = 1337,
BuildNumber = 1337,
SourceBranch = "local",
SourceVersion = "local"
};
/// <summary>
/// The plugin used by core components of Artemis
@ -108,6 +115,6 @@ namespace Artemis.Core
typeof(float),
typeof(double),
typeof(decimal)
};
};
}
}

View File

@ -38,5 +38,10 @@ namespace Artemis.Core.Services.Core
/// </summary>
[JsonProperty]
public string SourceVersion { get; internal set; } = null!;
/// <summary>
/// Gets a boolean indicating whether the current build is a local build
/// </summary>
public bool IsLocalBuild { get; internal set; }
}
}

View File

@ -61,7 +61,7 @@ namespace Artemis.Core.Services
server.StateChanged += (s, e) => _logger.Verbose("WebServer new state - {state}", e.NewState);
// Store the URL in a webserver.txt file so that remote applications can find it
File.WriteAllText(Path.Combine(Constants.DataFolder, "webserver.txt"), url);
File.WriteAllText(Path.Combine(Constants.DataFolder, "webserver.txt"), $"http://localhost:{_webServerPortSetting.Value}/");
return server;
}

View File

@ -334,9 +334,6 @@
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="buildinfo.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

View File

@ -32,13 +32,20 @@
<ProgressBar IsIndeterminate="True" />
</StackPanel>
<ItemsControl ItemsSource="{Binding Changes}" Visibility="{Binding RetrievingChanges, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Style="{StaticResource MaterialDesignSubtitle1TextBlock}" Foreground="{DynamicResource MaterialDesignBodyLight}"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Grid Visibility="{Binding RetrievingChanges, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}">
<ItemsControl ItemsSource="{Binding Changes}" Visibility="{Binding HasChanges, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Style="{StaticResource MaterialDesignSubtitle1TextBlock}" Foreground="{DynamicResource MaterialDesignBodyLight}"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Visibility="{Binding HasChanges, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}"
Style="{StaticResource MaterialDesignSubtitle1TextBlock}"
Foreground="{DynamicResource MaterialDesignBodyLight}">
Couldn't retrieve changes, sorry :(
</TextBlock>
</Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0 8 0 0">
<Button Style="{StaticResource MaterialDesignFlatButton}"

View File

@ -1,28 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Artemis.Core;
using Artemis.UI.Services;
using Artemis.UI.Services.Models.UpdateService;
using Artemis.UI.Shared.Services;
using Newtonsoft.Json.Linq;
using Stylet;
namespace Artemis.UI.Screens.Settings.Dialogs
{
public class UpdateDialogViewModel : DialogViewModelBase
{
private readonly DevOpsBuild _buildInfo;
private readonly IDialogService _dialogService;
// Based on https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/github?view=azure-devops&tabs=yaml#skipping-ci-for-individual-commits
private readonly string[] _excludedCommitMessages =
{
"[skip ci]",
"[ci skip]",
"skip-checks: true",
"skip-checks:true",
"[skip azurepipelines]",
"[azurepipelines skip]",
"[skip azpipelines]",
"[azpipelines skip]",
"[skip azp]",
"[azp skip]",
"***NO_CI***"
};
private readonly IMessageService _messageService;
private readonly IUpdateService _updateService;
private bool _canUpdate = true;
private bool _hasChanges;
private bool _retrievingChanges;
public UpdateDialogViewModel(DevOpsBuild buildInfo, IUpdateService updateService, IDialogService dialogService, IMessageService messageService)
{
_buildInfo = buildInfo;
_updateService = updateService;
_dialogService = dialogService;
_messageService = messageService;
@ -44,12 +58,37 @@ namespace Artemis.UI.Screens.Settings.Dialogs
set => SetAndNotify(ref _retrievingChanges, value);
}
public bool HasChanges
{
get => _hasChanges;
set => SetAndNotify(ref _hasChanges, value);
}
public bool CanUpdate
{
get => _canUpdate;
set => SetAndNotify(ref _canUpdate, value);
}
public async Task Update()
{
try
{
CanUpdate = false;
await _updateService.ApplyUpdate();
}
catch (Exception e)
{
_dialogService.ShowExceptionDialog("An exception occurred while applying the update", e);
}
finally
{
CanUpdate = true;
}
Session.Close(true);
}
private async Task GetBuildChanges()
{
try
@ -70,9 +109,10 @@ namespace Artemis.UI.Screens.Settings.Dialogs
Changes.AddRange(difference.Commits.Where(c => c.Parents.Count == 1)
.SelectMany(c => c.Commit.Message.Split("\n"))
.Select(m => m.Trim())
.Where(m => !string.IsNullOrWhiteSpace(m))
.Where(m => !string.IsNullOrWhiteSpace(m) && !_excludedCommitMessages.Contains(m))
.OrderBy(m => m)
);
HasChanges = Changes.Any();
}
}
catch (Exception e)
@ -84,24 +124,5 @@ namespace Artemis.UI.Screens.Settings.Dialogs
RetrievingChanges = false;
}
}
public async Task Update()
{
try
{
CanUpdate = false;
await _updateService.ApplyUpdate();
}
catch (Exception e)
{
_dialogService.ShowExceptionDialog("An exception occurred while applying the update", e);
}
finally
{
CanUpdate = true;
}
Session.Close();
}
}
}

View File

@ -274,10 +274,20 @@ namespace Artemis.UI.Screens.Settings.Tabs.General
return;
CanOfferUpdatesIfFound = false;
bool updateFound = await _updateService.OfferUpdateIfFound();
if (!updateFound)
_messageService.ShowMessage("You are already running the latest Artemis build. (☞゚ヮ゚)☞");
CanOfferUpdatesIfFound = true;
try
{
bool updateFound = await _updateService.OfferUpdateIfFound();
if (!updateFound)
_messageService.ShowMessage("You are already running the latest Artemis build. (☞゚ヮ゚)☞");
}
catch (Exception exception)
{
_messageService.ShowMessage($"Failed to check for updates: {exception.Message}");
}
finally
{
CanOfferUpdatesIfFound = true;
}
}
protected override void OnInitialActivate()

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Timers;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Exceptions;
@ -23,7 +24,9 @@ namespace Artemis.UI.Services
{
public class UpdateService : IUpdateService
{
private const double UpdateCheckInterval = 3600000; // once per hour
private const string ApiUrl = "https://dev.azure.com/artemis-rgb/Artemis/_apis/";
private readonly PluginSetting<bool> _autoInstallUpdates;
private readonly PluginSetting<bool> _checkForUpdates;
private readonly IDialogService _dialogService;
@ -41,13 +44,20 @@ namespace Artemis.UI.Services
_checkForUpdates = settingsService.GetSetting("UI.CheckForUpdates", true);
_autoInstallUpdates = settingsService.GetSetting("UI.AutoInstallUpdates", false);
_checkForUpdates.SettingChanged += CheckForUpdatesOnSettingChanged;
Timer timer = new(UpdateCheckInterval);
timer.Elapsed += async (_, _) => await AutoUpdate();
timer.Start();
}
public bool SuspendAutoUpdate { get; set; }
private async Task OfferUpdate(DevOpsBuild buildInfo)
{
await _dialogService.ShowDialog<UpdateDialogViewModel>(new Dictionary<string, object> {{"buildInfo", buildInfo}});
object result = await _dialogService.ShowDialog<UpdateDialogViewModel>(new Dictionary<string, object> {{"buildInfo", buildInfo}});
if (result is bool resultBool && resultBool == false)
SuspendAutoUpdate = true;
}
private async Task UpdateInstaller()
@ -73,10 +83,20 @@ namespace Artemis.UI.Services
public async Task<bool> AutoUpdate()
{
if (!_checkForUpdates.Value)
if (Constants.BuildInfo.IsLocalBuild)
return false;
if (!_checkForUpdates.Value || SuspendAutoUpdate)
return false;
return await OfferUpdateIfFound();
try
{
return await OfferUpdateIfFound();
}
catch (Exception e)
{
_logger.Warning(e, "Auto update failed");
return false;
}
}
public async Task<bool> OfferUpdateIfFound()
@ -186,13 +206,13 @@ namespace Artemis.UI.Services
catch (Exception e)
{
_logger.Warning(e, "GetBuildInfo: Failed to retrieve build info JSON");
return null;
throw;
}
}
catch (FlurlHttpException e)
{
_logger.Warning("GetBuildInfo: Getting build info, request returned {statusCode}", e.StatusCode);
return null;
throw;
}
}
@ -208,16 +228,16 @@ namespace Artemis.UI.Services
#region Event handlers
private void CheckForUpdatesOnSettingChanged(object sender, EventArgs e)
private async void CheckForUpdatesOnSettingChanged(object sender, EventArgs e)
{
// Run an auto-update as soon as the setting gets changed to enabled
if (_checkForUpdates.Value)
AutoUpdate();
await AutoUpdate();
}
private void WindowServiceOnMainWindowOpened(object sender, EventArgs e)
private async void WindowServiceOnMainWindowOpened(object sender, EventArgs e)
{
_logger.Information("Main window opened!");
await AutoUpdate();
}
#endregion
@ -225,6 +245,8 @@ namespace Artemis.UI.Services
public interface IUpdateService : IArtemisUIService
{
bool SuspendAutoUpdate { get; set; }
/// <summary>
/// If auto-update is enabled this will offer updates if found
/// </summary>

View File

@ -1,6 +0,0 @@
{
"BuildId": 442,
"BuildNumber": 20210130.3,
"SourceBranch": "refs/heads/master",
"SourceVersion": "b29ab064ae46d93141f31afaa8ee3ea9063c6263"
}