mirror of
https://github.com/gwdevhub/Daybreak.git
synced 2026-09-17 05:44:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71ab3fdefb |
@@ -23,6 +23,9 @@ using Daybreak.Services.Options;
|
||||
using Daybreak.Models;
|
||||
using Microsoft.CorrelationVector;
|
||||
using System.Logging;
|
||||
using Daybreak.Services.Updater.PostUpdate;
|
||||
using System.Core.Extensions;
|
||||
using Daybreak.Services.Updater.PostUpdate.Actions;
|
||||
|
||||
namespace Daybreak.Configuration
|
||||
{
|
||||
@@ -30,7 +33,7 @@ namespace Daybreak.Configuration
|
||||
{
|
||||
public static void RegisterResolvers(IServiceManager serviceManager)
|
||||
{
|
||||
serviceManager.ThrowIfNull(nameof(serviceManager));
|
||||
serviceManager.ThrowIfNull();
|
||||
|
||||
serviceManager.RegisterHttpFactory((serviceProvider, categoryType) =>
|
||||
{
|
||||
@@ -44,7 +47,7 @@ namespace Daybreak.Configuration
|
||||
|
||||
public static void RegisterServices(IServiceProducer serviceProducer)
|
||||
{
|
||||
serviceProducer.ThrowIfNull(nameof(serviceProducer));
|
||||
serviceProducer.ThrowIfNull();
|
||||
|
||||
serviceProducer.RegisterSingleton<ILogsManager, JsonLogsManager>();
|
||||
serviceProducer.RegisterSingleton<IDebugLogsWriter, Services.Logging.DebugLogsWriter>();
|
||||
@@ -60,6 +63,7 @@ namespace Daybreak.Configuration
|
||||
|
||||
serviceProducer.RegisterScoped((sp) => new ScopeMetadata(new CorrelationVector()));
|
||||
serviceProducer.RegisterSingleton<ViewManager>(registerAllInterfaces: true);
|
||||
serviceProducer.RegisterSingleton<PostUpdateActionManager>(registerAllInterfaces: true);
|
||||
serviceProducer.RegisterSingleton<IConfigurationManager, ConfigurationManager>();
|
||||
serviceProducer.RegisterSingleton<ILiteDatabase, LiteDatabase>(sp => new LiteDatabase("Daybreak.db"));
|
||||
serviceProducer.RegisterSingleton<IMutexHandler, MutexHandler>();
|
||||
@@ -79,7 +83,7 @@ namespace Daybreak.Configuration
|
||||
|
||||
public static void RegisterViews(IViewProducer viewProducer)
|
||||
{
|
||||
viewProducer.ThrowIfNull(nameof(viewProducer));
|
||||
viewProducer.ThrowIfNull();
|
||||
|
||||
viewProducer.RegisterView<MainView>();
|
||||
viewProducer.RegisterView<SettingsView>();
|
||||
@@ -97,5 +101,12 @@ namespace Daybreak.Configuration
|
||||
viewProducer.RegisterView<LogsView>();
|
||||
viewProducer.RegisterView<IconDownloadView>();
|
||||
}
|
||||
|
||||
public static void RegisterPostUpdateActions(IPostUpdateActionProducer postUpdateActionProducer)
|
||||
{
|
||||
postUpdateActionProducer.ThrowIfNull();
|
||||
|
||||
postUpdateActionProducer.AddPostUpdateAction<RenameInstallerAction>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<LangVersion>preview</LangVersion>
|
||||
<ApplicationIcon>Daybreak.ico</ApplicationIcon>
|
||||
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
|
||||
<Version>0.9.4.2</Version>
|
||||
<Version>0.9.5</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Daybreak.Configuration;
|
||||
using Daybreak.Exceptions;
|
||||
using Daybreak.Services.Updater.PostUpdate;
|
||||
using Daybreak.Services.ViewManagement;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Slim;
|
||||
@@ -33,6 +34,7 @@ namespace Daybreak.Launch
|
||||
ProjectConfiguration.RegisterServices(this.ServiceManager);
|
||||
ServiceManager.BuildSingletons();
|
||||
ProjectConfiguration.RegisterViews(this.ServiceManager.GetService<IViewManager>());
|
||||
ProjectConfiguration.RegisterPostUpdateActions(this.ServiceManager.GetService<IPostUpdateActionProducer>());
|
||||
}
|
||||
protected override bool HandleException(Exception e)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Daybreak.Exceptions;
|
||||
using Daybreak.Models.Github;
|
||||
using Daybreak.Models.Progress;
|
||||
using Daybreak.Services.Updater.PostUpdate;
|
||||
using Daybreak.Services.ViewManagement;
|
||||
using Daybreak.Views;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -9,6 +10,7 @@ using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Core.Extensions;
|
||||
using System.Diagnostics;
|
||||
using System.Extensions;
|
||||
using System.IO;
|
||||
@@ -36,24 +38,29 @@ namespace Daybreak.Services.Updater
|
||||
private const string DownloadUrl = $"https://github.com/AlexMacocian/Daybreak/releases/download/{VersionTag}/Daybreak{VersionTag}.zip";
|
||||
|
||||
private readonly CancellationTokenSource updateCancellationTokenSource = new();
|
||||
private readonly ILogger<ApplicationUpdater> logger;
|
||||
private readonly IViewManager viewManager;
|
||||
private readonly IPostUpdateActionProvider postUpdateActionProvider;
|
||||
private readonly ILiveOptions<ApplicationConfiguration> liveOptions;
|
||||
private readonly IHttpClient<ApplicationUpdater> httpClient;
|
||||
private readonly ILogger<ApplicationUpdater> logger;
|
||||
|
||||
public Version CurrentVersion { get; }
|
||||
|
||||
public ApplicationUpdater(
|
||||
ILogger<ApplicationUpdater> logger,
|
||||
ILiveOptions<ApplicationConfiguration> liveOptions,
|
||||
IViewManager viewManager,
|
||||
IHttpClient<ApplicationUpdater> httpClient)
|
||||
IPostUpdateActionProvider postUpdateActionProvider,
|
||||
ILiveOptions<ApplicationConfiguration> liveOptions,
|
||||
IHttpClient<ApplicationUpdater> httpClient,
|
||||
ILogger<ApplicationUpdater> logger)
|
||||
{
|
||||
this.viewManager = viewManager.ThrowIfNull(nameof(viewManager));
|
||||
this.liveOptions = liveOptions.ThrowIfNull(nameof(liveOptions));
|
||||
this.logger = logger.ThrowIfNull(nameof(logger));
|
||||
this.httpClient = httpClient.ThrowIfNull(nameof(httpClient));
|
||||
this.viewManager = viewManager.ThrowIfNull();
|
||||
this.postUpdateActionProvider = postUpdateActionProvider.ThrowIfNull();
|
||||
this.liveOptions = liveOptions.ThrowIfNull();
|
||||
this.httpClient = httpClient.ThrowIfNull();
|
||||
this.logger = logger.ThrowIfNull();
|
||||
|
||||
this.httpClient.DefaultRequestHeaders.Add("user-agent", "Daybreak Client");
|
||||
|
||||
if (Version.TryParse(Assembly.GetExecutingAssembly().GetName().Version.ToString(), out var currentVersion))
|
||||
{
|
||||
if (currentVersion.HasPrefix is false)
|
||||
@@ -178,8 +185,8 @@ namespace Daybreak.Services.Updater
|
||||
{
|
||||
if (UpdateMarkedInRegistry())
|
||||
{
|
||||
PerformPostUpdateActions();
|
||||
UnmarkUpdateInRegistry();
|
||||
this.ExecutePostUpdateActions();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,6 +222,18 @@ namespace Daybreak.Services.Updater
|
||||
}
|
||||
}
|
||||
|
||||
private async void ExecutePostUpdateActions()
|
||||
{
|
||||
this.logger.LogInformation("Executing post-update actions");
|
||||
foreach(var action in this.postUpdateActionProvider.GetPostUpdateActions())
|
||||
{
|
||||
this.logger.LogInformation($"Starting [{action.GetType().Name}]");
|
||||
action.DoPostUpdateAction();
|
||||
await action.DoPostUpdateActionAsync();
|
||||
this.logger.LogInformation($"Finished [{action.GetType().Name}]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void MarkUpdateInRegistry()
|
||||
{
|
||||
var homeRegistryKey = GetOrCreateHomeKey();
|
||||
@@ -259,19 +278,5 @@ namespace Daybreak.Services.Updater
|
||||
|
||||
return homeRegistryKey;
|
||||
}
|
||||
|
||||
private static void PerformPostUpdateActions()
|
||||
{
|
||||
RenameInstallerIfAvailable();
|
||||
}
|
||||
|
||||
private static void RenameInstallerIfAvailable()
|
||||
{
|
||||
if (File.Exists(TemporaryInstallerFileName))
|
||||
{
|
||||
File.Copy(TemporaryInstallerFileName, InstallerFileName, true);
|
||||
File.Delete(TemporaryInstallerFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Core.Extensions;
|
||||
using System.IO;
|
||||
|
||||
namespace Daybreak.Services.Updater.PostUpdate.Actions
|
||||
{
|
||||
public sealed class RenameInstallerAction : PostUpdateActionBase
|
||||
{
|
||||
private const string TemporaryInstallerFileName = "Daybreak.Installer.Temp.exe";
|
||||
private const string InstallerFileName = "Daybreak.Installer.exe";
|
||||
|
||||
private readonly ILogger<RenameInstallerAction> logger;
|
||||
|
||||
public RenameInstallerAction(
|
||||
ILogger<RenameInstallerAction> logger)
|
||||
{
|
||||
this.logger = logger.ThrowIfNull();
|
||||
}
|
||||
|
||||
public override void DoPostUpdateAction()
|
||||
{
|
||||
if (File.Exists(TemporaryInstallerFileName))
|
||||
{
|
||||
this.logger.LogInformation("Detected new installer version. Overwriting old installer with new one");
|
||||
File.Copy(TemporaryInstallerFileName, InstallerFileName, true);
|
||||
|
||||
this.logger.LogInformation("Deleting new installer temporary file");
|
||||
File.Delete(TemporaryInstallerFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Daybreak.Services.Updater.PostUpdate
|
||||
{
|
||||
public interface IPostUpdateActionManager : IPostUpdateActionProducer, IPostUpdateActionProvider
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Daybreak.Services.Updater.PostUpdate
|
||||
{
|
||||
public interface IPostUpdateActionProducer
|
||||
{
|
||||
void AddPostUpdateAction<T>()
|
||||
where T : PostUpdateActionBase;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Daybreak.Services.Updater.PostUpdate
|
||||
{
|
||||
public interface IPostUpdateActionProvider
|
||||
{
|
||||
IEnumerable<PostUpdateActionBase> GetPostUpdateActions();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Daybreak.Services.Updater.PostUpdate
|
||||
{
|
||||
public abstract class PostUpdateActionBase
|
||||
{
|
||||
public virtual void DoPostUpdateAction()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual Task DoPostUpdateActionAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Slim;
|
||||
using System.Collections.Generic;
|
||||
using System.Core.Extensions;
|
||||
|
||||
namespace Daybreak.Services.Updater.PostUpdate
|
||||
{
|
||||
public sealed class PostUpdateActionManager : IPostUpdateActionManager
|
||||
{
|
||||
private readonly IServiceManager serviceManager;
|
||||
private readonly ILogger<PostUpdateActionManager> logger;
|
||||
|
||||
public PostUpdateActionManager(
|
||||
IServiceManager serviceManager,
|
||||
ILogger<PostUpdateActionManager> logger)
|
||||
{
|
||||
this.serviceManager = serviceManager.ThrowIfNull();
|
||||
this.logger = logger.ThrowIfNull();
|
||||
}
|
||||
|
||||
public void AddPostUpdateAction<T>()
|
||||
where T : PostUpdateActionBase
|
||||
{
|
||||
this.serviceManager.RegisterSingleton<T>();
|
||||
this.logger.LogDebug($"Added post update action {typeof(T).Name}");
|
||||
}
|
||||
|
||||
public IEnumerable<PostUpdateActionBase> GetPostUpdateActions()
|
||||
{
|
||||
return this.serviceManager.GetServicesOfType<PostUpdateActionBase>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user