Skip to content

Commit

Permalink
Cleanup options and app startup code
Browse files Browse the repository at this point in the history
  • Loading branch information
haefele committed Oct 15, 2023
1 parent abc7ef1 commit f52e517
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/ChatPrisma/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -108,6 +110,15 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho
})
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<UpdaterOptions>()
.Configure(o =>
{
o.GitHubUsername = "haefele";
o.GitHubRepository = "ChatPrisma";
o.GitHubReleaseAssetName = "App.zip";
})
.ValidateDataAnnotations()
.ValidateOnStart();

// Services
services.AddSingleton<IKeyboardHooks, GlobalKeyInterceptorKeyboardHooks>();
Expand All @@ -116,10 +127,18 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho
services.AddSingleton<IChatBotService, OpenAIChatBotService>();
services.AddSingleton<IViewModelFactory, ViewModelFactory>();
services.AddSingleton<IDialogService, DialogService>();
services.AddSingleton<IUpdateManager>(new UpdateManager(
new AssemblyMetadata("Chat Prisma", Version.Parse(ThisAssembly.AssemblyVersion), Environment.ProcessPath!),
new GithubPackageResolver("haefele", "ChatPrisma", "App.zip"),
new ZipPackageExtractor()));
services.AddSingleton<AssemblyMetadata>(serviceProvider =>
{
var applicationOptions = serviceProvider.GetRequiredService<IOptions<ApplicationOptions>>().Value;
return new AssemblyMetadata(applicationOptions.ApplicationName, Version.Parse(applicationOptions.ApplicationVersion), Environment.ProcessPath!);
});
services.AddSingleton<IPackageResolver>(serviceProvider =>
{
var updaterOptions = serviceProvider.GetRequiredService<IOptions<UpdaterOptions>>().Value;
return new GithubPackageResolver(updaterOptions.GitHubUsername, updaterOptions.GitHubRepository, updaterOptions.GitHubReleaseAssetName);
});
services.AddSingleton<IPackageExtractor, ZipPackageExtractor>();
services.AddSingleton<IUpdateManager, UpdateManager>();

// Hosted Services
services.AddHostedService<StartKeyboardHooksHostedService>();
Expand Down
13 changes: 13 additions & 0 deletions src/ChatPrisma/Options/UpdaterOptions.cs
Original file line number Diff line number Diff line change
@@ -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!;
}

0 comments on commit f52e517

Please sign in to comment.