Skip to content

Commit

Permalink
minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed Jul 17, 2023
1 parent 08c325a commit 6122d02
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 116 deletions.
72 changes: 68 additions & 4 deletions src/RepoM.App/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -235,4 +235,68 @@ public static void RegisterLogging(ILoggerFactory loggerFactory)
Lifestyle.Singleton,
_ => true);
}
}
}

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<int?> GetConfigurationVersionAsync()
{
ConfigEnvelope<object>? result = await LoadAsync<object>().ConfigureAwait(false);
return result?.Version;
}

public async Task<T?> LoadConfigurationAsync<T>() where T : class, new()
{
ConfigEnvelope<T>? result = await LoadAsync<T>().ConfigureAwait(false);
return result?.Payload;
}

public async Task PersistConfigurationAsync<T>(T configuration, int version)
{
if (configuration == null)
{
return;
}

var filename = GetFilename();

var json = JsonConvert.SerializeObject(new ConfigEnvelope<T> { 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<ConfigEnvelope<T>?> LoadAsync<T>()
{
var filename = GetFilename();
if (!_fileSystem.File.Exists(filename))
{
return null;
}

var json = await _fileSystem.File.ReadAllTextAsync(filename).ConfigureAwait(false);
ConfigEnvelope<T>? result = JsonConvert.DeserializeObject<ConfigEnvelope<T>>(json);
return result;
}

private sealed class ConfigEnvelope<T>
{
public int Version { get; init; }

public T? Payload { get; init; }
}
}
1 change: 0 additions & 1 deletion src/RepoM.Core.Plugin/RepoM.Core.Plugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SimpleInjector" Version="5.4.1" />
<PackageReference Include="System.IO.Abstractions" Version="19.2.29" />
</ItemGroup>
Expand Down
126 changes: 23 additions & 103 deletions src/RepoM.Core.Plugin/SimpleInjector/PackageExtensionsRepoM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -33,9 +24,10 @@ public static class PackageExtensionsRepoM
/// </summary>
/// <param name="container">The container to which the packages will be applied to.</param>
/// <param name="assemblies">The assemblies that will be searched for packages.</param>
/// <param name="packageConfigurationFactoryMethod">The factory method to create an <see cref="IPackageConfiguration"/> instance based.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="container"/> is a null
/// reference.</exception>
public static async Task RegisterPackagesAsync(this Container container, IEnumerable<Assembly> assemblies, Func<string, IPackageConfiguration> packageConfigurationFactoryMethod)
public static Task RegisterPackagesAsync(this Container container, IEnumerable<Assembly> assemblies, Func<string, IPackageConfiguration> packageConfigurationFactoryMethod)
{
if (container is null)
{
Expand All @@ -52,112 +44,40 @@ 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<Assembly> assemblies, Func<string, IPackageConfiguration> 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
{
package.RegisterServices(container);
}
}
}

// 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<int?> GetConfigurationVersionAsync()
{
var result = await LoadAsync<object>().ConfigureAwait(false);
return result?.Version;
}

public async Task<T?> LoadConfigurationAsync<T>() where T : class, new()
{
var result = await LoadAsync<T>().ConfigureAwait(false);
return result?.Payload;
}

public async Task PersistConfigurationAsync<T>(T configuration, int version)
{
if (configuration == null)
{
return;
}

var filename = GetFilename();

var json = Newtonsoft.Json.JsonConvert.SerializeObject(new ConfigEnveloppe<T> { 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<ConfigEnveloppe<T>?> LoadAsync<T>()
{
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<Newtonsoft.Json.JsonConverter> { new Newtonsoft.Json.Converters.StringEnumConverter() },
// NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
// Formatting = Newtonsoft.Json.Formatting.Indented,
// };

var result = JsonConvert.DeserializeObject<ConfigEnveloppe<T>>(json);

return result;

}

}

public class ConfigEnveloppe<T>
{
public int Version { get; set; }

public T Payload { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,7 +16,6 @@ public AzureDevopsConfiguration(string? url, string? pat)
{
AzureDevOpsBaseUrl = null;
}

}

public string? AzureDevOpsPersonalAccessToken { get; }
Expand Down
10 changes: 10 additions & 0 deletions src/RepoM.Plugin.AzureDevOps/Internal/IAzureDevopsConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RepoM.Plugin.AzureDevOps.Internal;

using System;

internal interface IAzureDevopsConfiguration
{
string? AzureDevOpsPersonalAccessToken { get; }

Uri? AzureDevOpsBaseUrl { get; }
}

0 comments on commit 6122d02

Please sign in to comment.