From f52e517562a9da15adc26184bb9d724d70436aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Sun, 15 Oct 2023 23:20:33 +0200 Subject: [PATCH] Cleanup options and app startup code --- src/ChatPrisma/App.xaml.cs | 27 ++++++++++++++++++++---- src/ChatPrisma/Options/UpdaterOptions.cs | 13 ++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/ChatPrisma/Options/UpdaterOptions.cs diff --git a/src/ChatPrisma/App.xaml.cs b/src/ChatPrisma/App.xaml.cs index 62aca84..65d172b 100644 --- a/src/ChatPrisma/App.xaml.cs +++ b/src/ChatPrisma/App.xaml.cs @@ -4,6 +4,7 @@ using ChatPrisma.Host; using ChatPrisma.HostedServices; using ChatPrisma.Options; +using ChatPrisma.Services.AutoStart; using ChatPrisma.Services.ChatBot; using ChatPrisma.Services.Dialogs; using ChatPrisma.Services.KeyboardHooks; @@ -13,6 +14,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NLog.Extensions.Hosting; using Onova; using Onova.Models; @@ -108,6 +110,15 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho }) .ValidateDataAnnotations() .ValidateOnStart(); + services.AddOptions() + .Configure(o => + { + o.GitHubUsername = "haefele"; + o.GitHubRepository = "ChatPrisma"; + o.GitHubReleaseAssetName = "App.zip"; + }) + .ValidateDataAnnotations() + .ValidateOnStart(); // Services services.AddSingleton(); @@ -116,10 +127,18 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(new UpdateManager( - new AssemblyMetadata("Chat Prisma", Version.Parse(ThisAssembly.AssemblyVersion), Environment.ProcessPath!), - new GithubPackageResolver("haefele", "ChatPrisma", "App.zip"), - new ZipPackageExtractor())); + services.AddSingleton(serviceProvider => + { + var applicationOptions = serviceProvider.GetRequiredService>().Value; + return new AssemblyMetadata(applicationOptions.ApplicationName, Version.Parse(applicationOptions.ApplicationVersion), Environment.ProcessPath!); + }); + services.AddSingleton(serviceProvider => + { + var updaterOptions = serviceProvider.GetRequiredService>().Value; + return new GithubPackageResolver(updaterOptions.GitHubUsername, updaterOptions.GitHubRepository, updaterOptions.GitHubReleaseAssetName); + }); + services.AddSingleton(); + services.AddSingleton(); // Hosted Services services.AddHostedService(); diff --git a/src/ChatPrisma/Options/UpdaterOptions.cs b/src/ChatPrisma/Options/UpdaterOptions.cs new file mode 100644 index 0000000..b1fad9d --- /dev/null +++ b/src/ChatPrisma/Options/UpdaterOptions.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace ChatPrisma.Options; + +public class UpdaterOptions +{ + [Required] + public string GitHubUsername { get; set; } = default!; + [Required] + public string GitHubRepository { get; set; } = default!; + [Required] + public string GitHubReleaseAssetName { get; set; } = default!; +}