From 6122d0252389adf36df9791d784ff1b6147c4f1f Mon Sep 17 00:00:00 2001 From: Coen van den Munckhof Date: Mon, 17 Jul 2023 23:30:50 +0200 Subject: [PATCH] minor update --- src/RepoM.App/Bootstrapper.cs | 72 +++++++++- .../RepoM.Core.Plugin.csproj | 1 - .../SimpleInjector/PackageExtensionsRepoM.cs | 126 ++++-------------- .../Internal/AzureDevopsConfiguration.cs | 8 -- .../Internal/IAzureDevopsConfiguration.cs | 10 ++ 5 files changed, 101 insertions(+), 116 deletions(-) create mode 100644 src/RepoM.Plugin.AzureDevOps/Internal/IAzureDevopsConfiguration.cs diff --git a/src/RepoM.App/Bootstrapper.cs b/src/RepoM.App/Bootstrapper.cs index cdb3d759..b5d2beca 100644 --- a/src/RepoM.App/Bootstrapper.cs +++ b/src/RepoM.App/Bootstrapper.cs @@ -48,6 +48,8 @@ namespace RepoM.App; using RepoM.Api.Plugins; using RepoM.App.Plugins; using RepoM.App.Services.HotKey; +using Newtonsoft.Json; +using RepoM.Core.Plugin; internal static class Bootstrapper { @@ -212,11 +214,9 @@ static PluginSettings Convert(PluginInfo pluginInfo, string baseDir, bool enable if (assemblies.Any()) { - // Container.RegisterPackages(assemblies); await Container.RegisterPackagesAsync( assemblies, - filename => new FileBasedPackageConfiguration(DefaultAppDataPathProvider.Instance, fileSystem, NullLogger.Instance, filename)) - .ConfigureAwait(false); //tmp + filename => new FileBasedPackageConfiguration(DefaultAppDataPathProvider.Instance, fileSystem, filename)).ConfigureAwait(false); } } @@ -235,4 +235,68 @@ public static void RegisterLogging(ILoggerFactory loggerFactory) Lifestyle.Singleton, _ => true); } -} \ No newline at end of file +} + +file class FileBasedPackageConfiguration : IPackageConfiguration +{ + private readonly IAppDataPathProvider _appDataPathProvider; + private readonly IFileSystem _fileSystem; + private readonly string _filename; + + public FileBasedPackageConfiguration(IAppDataPathProvider appDataPathProvider, IFileSystem fileSystem, string filename) + { + _appDataPathProvider = appDataPathProvider ?? throw new ArgumentNullException(nameof(appDataPathProvider)); + _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); + _filename = filename ?? throw new ArgumentNullException(nameof(filename)); + } + + public async Task GetConfigurationVersionAsync() + { + ConfigEnvelope? result = await LoadAsync().ConfigureAwait(false); + return result?.Version; + } + + public async Task LoadConfigurationAsync() where T : class, new() + { + ConfigEnvelope? result = await LoadAsync().ConfigureAwait(false); + return result?.Payload; + } + + public async Task PersistConfigurationAsync(T configuration, int version) + { + if (configuration == null) + { + return; + } + + var filename = GetFilename(); + + var json = JsonConvert.SerializeObject(new ConfigEnvelope { Version = version, Payload = configuration, }, Formatting.Indented); + await _fileSystem.File.WriteAllTextAsync(filename, json).ConfigureAwait(false); + } + + private string GetFilename() + { + return Path.Combine(_appDataPathProvider.GetAppDataPath(), "Module", _filename + ".json"); + } + + private async Task?> LoadAsync() + { + var filename = GetFilename(); + if (!_fileSystem.File.Exists(filename)) + { + return null; + } + + var json = await _fileSystem.File.ReadAllTextAsync(filename).ConfigureAwait(false); + ConfigEnvelope? result = JsonConvert.DeserializeObject>(json); + return result; + } + + private sealed class ConfigEnvelope + { + public int Version { get; init; } + + public T? Payload { get; init; } + } +} diff --git a/src/RepoM.Core.Plugin/RepoM.Core.Plugin.csproj b/src/RepoM.Core.Plugin/RepoM.Core.Plugin.csproj index eb93da28..d42797f6 100644 --- a/src/RepoM.Core.Plugin/RepoM.Core.Plugin.csproj +++ b/src/RepoM.Core.Plugin/RepoM.Core.Plugin.csproj @@ -6,7 +6,6 @@ - diff --git a/src/RepoM.Core.Plugin/SimpleInjector/PackageExtensionsRepoM.cs b/src/RepoM.Core.Plugin/SimpleInjector/PackageExtensionsRepoM.cs index a50c1fc8..7beb4703 100644 --- a/src/RepoM.Core.Plugin/SimpleInjector/PackageExtensionsRepoM.cs +++ b/src/RepoM.Core.Plugin/SimpleInjector/PackageExtensionsRepoM.cs @@ -7,18 +7,9 @@ namespace SimpleInjector { using System; using System.Collections.Generic; - using System.ComponentModel; - using System.Globalization; - using System.IO; - using System.IO.Abstractions; - using System.Linq; using System.Reflection; - using System.Text.Json.Serialization; using System.Threading.Tasks; - using Microsoft.Extensions.Logging; - using Newtonsoft.Json; using RepoM.Core.Plugin; - using RepoM.Core.Plugin.Common; using SimpleInjector.Packaging; /// @@ -33,9 +24,10 @@ public static class PackageExtensionsRepoM /// /// The container to which the packages will be applied to. /// The assemblies that will be searched for packages. + /// The factory method to create an instance based. /// Thrown when the is a null /// reference. - public static async Task RegisterPackagesAsync(this Container container, IEnumerable assemblies, Func packageConfigurationFactoryMethod) + public static Task RegisterPackagesAsync(this Container container, IEnumerable assemblies, Func packageConfigurationFactoryMethod) { if (container is null) { @@ -52,17 +44,33 @@ public static async Task RegisterPackagesAsync(this Container container, IEnumer throw new ArgumentNullException(nameof(packageConfigurationFactoryMethod)); } + return RegisterPackagesInnerAsync(container, assemblies, packageConfigurationFactoryMethod); + } + + private static async Task RegisterPackagesInnerAsync(Container container, IEnumerable assemblies, Func packageConfigurationFactoryMethod) + { foreach (Assembly assembly in assemblies) { - var n = assembly.GetName().Name; + var assemblyName = assembly.GetName().Name ?? string.Empty; - foreach (IPackage package in container.GetPackagesToRegister(new [] { assembly, })) + foreach (IPackage package in container.GetPackagesToRegister(new[] { assembly, })) { if (package is IPackageWithConfiguration packageWithConfiguration) { - var x = n + "." + packageWithConfiguration.Name; - - await packageWithConfiguration.RegisterServicesAsync(container, packageConfigurationFactoryMethod.Invoke(x)).ConfigureAwait(false); + var fileName = assemblyName; + if (fileName.StartsWith("RepoM.Plugin.")) + { + fileName = fileName["RepoM.Plugin.".Length..]; + } + + if (!string.IsNullOrWhiteSpace(packageWithConfiguration.Name)) + { + fileName += "." + packageWithConfiguration.Name; + } + + await packageWithConfiguration + .RegisterServicesAsync(container, packageConfigurationFactoryMethod.Invoke(fileName)) + .ConfigureAwait(false); } else { @@ -70,94 +78,6 @@ public static async Task RegisterPackagesAsync(this Container container, IEnumer } } } - - // foreach (IPackage package in container.GetPackagesToRegister(assemblies)) - // { - // if (package is IPackageWithConfiguration packageWithConfiguration) - // { - // await packageWithConfiguration.RegisterServicesAsync(container, packageConfigurationFactoryMethod.Invoke("")).ConfigureAwait(false); - // } - // else - // { - // package.RegisterServices(container); - // } - // } - } - } - - public class FileBasedPackageConfiguration : IPackageConfiguration - { - private readonly IAppDataPathProvider _appDataPathProvider; - private readonly IFileSystem _fileSystem; - private readonly ILogger _logger; - private readonly string _filename; - - public FileBasedPackageConfiguration(IAppDataPathProvider appDataPathProvider, IFileSystem fileSystem, ILogger logger, string filename) - { - _appDataPathProvider = appDataPathProvider ?? throw new ArgumentNullException(nameof(appDataPathProvider)); - _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); - _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _filename = filename ?? throw new ArgumentNullException(nameof(filename)); } - - public async Task GetConfigurationVersionAsync() - { - var result = await LoadAsync().ConfigureAwait(false); - return result?.Version; - } - - public async Task LoadConfigurationAsync() where T : class, new() - { - var result = await LoadAsync().ConfigureAwait(false); - return result?.Payload; - } - - public async Task PersistConfigurationAsync(T configuration, int version) - { - if (configuration == null) - { - return; - } - - var filename = GetFilename(); - - var json = Newtonsoft.Json.JsonConvert.SerializeObject(new ConfigEnveloppe { Version = version, Payload = configuration, }, Formatting.Indented); - await _fileSystem.File.WriteAllTextAsync(filename, json).ConfigureAwait(false); - } - - private string GetFilename() - { - return Path.Combine(_appDataPathProvider.GetAppDataPath(), "Module", _filename + ".json"); - } - - private async Task?> LoadAsync() - { - var filename = GetFilename(); - if (!_fileSystem.File.Exists(filename)) - { - return null; - } - - var json = await _fileSystem.File.ReadAllTextAsync(filename).ConfigureAwait(false); - // Newtonsoft.Json.JsonConvert.DefaultSettings = () => new Newtonsoft.Json.JsonSerializerSettings - // { - // Converters = new List { new Newtonsoft.Json.Converters.StringEnumConverter() }, - // NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, - // Formatting = Newtonsoft.Json.Formatting.Indented, - // }; - - var result = JsonConvert.DeserializeObject>(json); - - return result; - - } - - } - - public class ConfigEnveloppe - { - public int Version { get; set; } - - public T Payload { get; set; } } } \ No newline at end of file diff --git a/src/RepoM.Plugin.AzureDevOps/Internal/AzureDevopsConfiguration.cs b/src/RepoM.Plugin.AzureDevOps/Internal/AzureDevopsConfiguration.cs index 3da8a76d..87274e72 100644 --- a/src/RepoM.Plugin.AzureDevOps/Internal/AzureDevopsConfiguration.cs +++ b/src/RepoM.Plugin.AzureDevOps/Internal/AzureDevopsConfiguration.cs @@ -2,13 +2,6 @@ namespace RepoM.Plugin.AzureDevOps.Internal; using System; -internal interface IAzureDevopsConfiguration -{ - string? AzureDevOpsPersonalAccessToken { get; } - - Uri? AzureDevOpsBaseUrl { get; } -} - internal class AzureDevopsConfiguration : IAzureDevopsConfiguration { public AzureDevopsConfiguration(string? url, string? pat) @@ -23,7 +16,6 @@ public AzureDevopsConfiguration(string? url, string? pat) { AzureDevOpsBaseUrl = null; } - } public string? AzureDevOpsPersonalAccessToken { get; } diff --git a/src/RepoM.Plugin.AzureDevOps/Internal/IAzureDevopsConfiguration.cs b/src/RepoM.Plugin.AzureDevOps/Internal/IAzureDevopsConfiguration.cs new file mode 100644 index 00000000..6e0e3001 --- /dev/null +++ b/src/RepoM.Plugin.AzureDevOps/Internal/IAzureDevopsConfiguration.cs @@ -0,0 +1,10 @@ +namespace RepoM.Plugin.AzureDevOps.Internal; + +using System; + +internal interface IAzureDevopsConfiguration +{ + string? AzureDevOpsPersonalAccessToken { get; } + + Uri? AzureDevOpsBaseUrl { get; } +} \ No newline at end of file