Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module configuration #65

Merged
merged 12 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/RepoM.Api/Common/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public AppSettings()

public List<string> EnabledSearchProviders { get; set; }

public string SonarCloudPersonalAccessToken { get; set; }
public string? SonarCloudPersonalAccessToken { get; set; }

public AzureDevOpsOptions AzureDevOps { get; set; }
public AzureDevOpsOptions? AzureDevOps { get; set; }

public List<PluginOptions> Plugins { get; set; }

Expand All @@ -46,23 +46,19 @@ public AppSettings()
MenuSize = Size.Default,
ReposRootDirectories = new(),
EnabledSearchProviders = new List<string>(1),
SonarCloudPersonalAccessToken = string.Empty,
SonarCloudPersonalAccessToken = null,
AzureDevOps = AzureDevOpsOptions.Default,
Plugins = new List<PluginOptions>(),
};
}

public class AzureDevOpsOptions
{
public string PersonalAccessToken { get; set; } = string.Empty;
public string? PersonalAccessToken { get; set; } = string.Empty;

public string BaseUrl { get; set; } = string.Empty;
public string? BaseUrl { get; set; } = string.Empty;

public static AzureDevOpsOptions Default => new()
{
PersonalAccessToken = string.Empty,
BaseUrl = string.Empty,
};
public static AzureDevOpsOptions? Default => null;
}

public class Size
Expand Down
97 changes: 83 additions & 14 deletions src/RepoM.Api/Common/FileAppSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,51 +235,120 @@ public List<string> EnabledSearchProviders
}
}

[Obsolete("Will be removed in next big version")]
public string SonarCloudPersonalAccessToken
{
get => Settings.SonarCloudPersonalAccessToken;
get => Settings.SonarCloudPersonalAccessToken ?? string.Empty;
set
{
if (value.Equals(Settings.SonarCloudPersonalAccessToken, StringComparison.InvariantCulture))
if (string.IsNullOrWhiteSpace(value))
{
return;
if (Settings.SonarCloudPersonalAccessToken == null)
{
return;
}

Settings.SonarCloudPersonalAccessToken = null;
}
else
{
if (value.Equals(Settings.SonarCloudPersonalAccessToken, StringComparison.InvariantCulture))
{
return;
}

Settings.SonarCloudPersonalAccessToken = value;
Settings.SonarCloudPersonalAccessToken = value;
}

NotifyChange();
Save();
}
}

[Obsolete("Will be removed in next big version")]
public string AzureDevOpsPersonalAccessToken
{
get => Settings.AzureDevOps.PersonalAccessToken;
get => Settings.AzureDevOps?.PersonalAccessToken ?? string.Empty;
set
{
if (value.Equals(Settings.AzureDevOps.PersonalAccessToken, StringComparison.InvariantCulture))
if (string.IsNullOrWhiteSpace(value))
{
return;
if (Settings.AzureDevOps == null)
{
return;
}

if (string.IsNullOrWhiteSpace(Settings.AzureDevOps.PersonalAccessToken))
{
return;
}

Settings.AzureDevOps.PersonalAccessToken = null;

if (Settings.AzureDevOps.BaseUrl == null)
{
Settings.AzureDevOps = null;
}
}
else
{
if (Settings.AzureDevOps == null)
{
Settings.AzureDevOps = new AzureDevOpsOptions { PersonalAccessToken = value, };
}
else
{
if (value.Equals(Settings.AzureDevOps.PersonalAccessToken, StringComparison.InvariantCulture))
{
return;
}
}
}

Settings.AzureDevOps.PersonalAccessToken = value;

NotifyChange();
Save();
}
}

[Obsolete("Will be removed in next big version")]
public string AzureDevOpsBaseUrl
{
get => Settings.AzureDevOps.BaseUrl;
get => Settings.AzureDevOps?.BaseUrl ?? string.Empty;
set
{
if (value.Equals(Settings.AzureDevOps.BaseUrl, StringComparison.InvariantCulture))
if (string.IsNullOrWhiteSpace(value))
{
return;
if (Settings.AzureDevOps == null)
{
return;
}

if (string.IsNullOrWhiteSpace(Settings.AzureDevOps.BaseUrl))
{
return;
}

Settings.AzureDevOps.BaseUrl = null;

if (Settings.AzureDevOps.PersonalAccessToken == null)
{
Settings.AzureDevOps = null;
}
}
else
{
if (Settings.AzureDevOps == null)
{
Settings.AzureDevOps = new AzureDevOpsOptions { BaseUrl = value, };
}
else
{
if (value.Equals(Settings.AzureDevOps.BaseUrl, StringComparison.InvariantCulture))
{
return;
}
}
}

Settings.AzureDevOps.BaseUrl = value;

NotifyChange();
Save();
Expand Down
3 changes: 3 additions & 0 deletions src/RepoM.Api/Common/IAppSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ public interface IAppSettingsService

List<string> EnabledSearchProviders { get; set; }

[Obsolete("Will be removed in next big version")]
string SonarCloudPersonalAccessToken { get; set; }

[Obsolete("Will be removed in next big version")]
string AzureDevOpsPersonalAccessToken { get; set; }

[Obsolete("Will be removed in next big version")]
string AzureDevOpsBaseUrl { get; set; }

string SortKey { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/RepoM.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected override void OnStartup(StartupEventArgs e)
logger.LogInformation("Started");
Bootstrapper.RegisterLogging(loggerFactory);
Bootstrapper.RegisterServices(fileSystem);
Bootstrapper.RegisterPlugins(pluginFinder, fileSystem);
Bootstrapper.RegisterPlugins(pluginFinder, fileSystem, loggerFactory).GetAwaiter().GetResult();

#if DEBUG
Bootstrapper.Container.Verify(VerificationOption.VerifyAndDiagnose);
Expand Down
11 changes: 9 additions & 2 deletions src/RepoM.App/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace RepoM.App;
using System.Reflection;
using System;
using System.Linq;
using System.Threading.Tasks;
using SimpleInjector;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
Expand Down Expand Up @@ -172,7 +173,7 @@ public static void RegisterServices(IFileSystem fileSystem)
Container.RegisterSingleton<WindowSizeService>();
}

public static void RegisterPlugins(IPluginFinder pluginFinder, IFileSystem fileSystem)
public static async Task RegisterPlugins(IPluginFinder pluginFinder, IFileSystem fileSystem, ILoggerFactory loggerFactory)
{
Container.Register<ModuleService>(Lifestyle.Singleton);
Container.RegisterInstance(pluginFinder);
Expand Down Expand Up @@ -211,7 +212,13 @@ 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,
loggerFactory.CreateLogger<FileBasedPackageConfiguration>(),
filename)).ConfigureAwait(false);
}
}

Expand Down
132 changes: 132 additions & 0 deletions src/RepoM.App/Services/FileBasedPackageConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
namespace RepoM.App.Services;

using System;
using System.IO;
using System.IO.Abstractions;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using RepoM.Core.Plugin;
using RepoM.Core.Plugin.Common;

internal 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()
{
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?.Settings;
}

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

var filename = GetFilename();

var exists = MakeSureDirectoryExists(filename);
if (!exists)
{
return;
}

var json = JsonConvert.SerializeObject(new ConfigEnvelope<T> { Version = version, Settings = configuration, }, Formatting.Indented);

try
{
await _fileSystem.File.WriteAllTextAsync(filename, json).ConfigureAwait(false);
}
catch (Exception)
{
// swallow for now
}
}

private bool MakeSureDirectoryExists(string filename)
{
try
{
IFileInfo fi = _fileSystem.FileInfo.New(filename);
var directoryName = fi.Directory?.FullName;

if (string.IsNullOrWhiteSpace(directoryName))
{
return false;
}

if (_fileSystem.Directory.Exists(directoryName))
{
return true;
}

try
{
_fileSystem.Directory.CreateDirectory(directoryName);
}
catch (Exception e)
{
_logger.LogError(e, "Could not create directory '{directoryName}'. {message}", directoryName, e.Message);
}

return _fileSystem.Directory.Exists(directoryName);
}
catch (Exception)
{
return false;
}
}

private async Task<ConfigEnvelope<T>?> LoadAsync<T>()
{
var filename = GetFilename();
if (!_fileSystem.File.Exists(filename))
{
return null;
}

try
{
var json = await _fileSystem.File.ReadAllTextAsync(filename).ConfigureAwait(false);
ConfigEnvelope<T>? result = JsonConvert.DeserializeObject<ConfigEnvelope<T>>(json);
return result;
}
catch (Exception e)
{
_logger.LogError(e, "Could not deserialize '{filename}'", filename);
return null;
}
}

private string GetFilename()
{
return Path.Combine(_appDataPathProvider.GetAppDataPath(), "Module", _filename + ".json");
}

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

public T? Settings { get; init; }
}
}
12 changes: 12 additions & 0 deletions src/RepoM.Core.Plugin/IPackageConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace RepoM.Core.Plugin;

using System.Threading.Tasks;

public interface IPackageConfiguration
{
Task<int?> GetConfigurationVersionAsync();

Task<T?> LoadConfigurationAsync<T>() where T : class, new();

Task PersistConfigurationAsync<T>(T configuration, int version);
}
14 changes: 14 additions & 0 deletions src/RepoM.Core.Plugin/IPackageWithConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace RepoM.Core.Plugin;

using System.Threading.Tasks;
using SimpleInjector;
using SimpleInjector.Packaging;

public interface IPackageWithConfiguration : IPackage
{
public string Name { get; }

/// <summary>Registers the set of services in the specified <paramref name="container"/>.</summary>
/// <param name="container">The container the set of services is registered into.</param>
Task RegisterServicesAsync(Container container, IPackageConfiguration packageConfiguration);
}
1 change: 1 addition & 0 deletions src/RepoM.Core.Plugin/RepoM.Core.Plugin.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=simpleinjector/@EntryIndexedValue">False</s:Boolean></wpf:ResourceDictionary>
Loading
Loading