108 lines
5.1 KiB
C#
108 lines
5.1 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace View_by_Distance.Rename.Models.Binder;
|
|
|
|
public class AppSettings
|
|
{
|
|
|
|
public string? Company { get; set; }
|
|
public string[]? ConfigurationDirectoryNames { get; set; }
|
|
public string? ConfigurationFileName { get; set; }
|
|
public int? ConfigurationSpecialFolder { get; set; }
|
|
public int? MaxDegreeOfParallelism { get; set; }
|
|
public bool? RequireRootDirectoryExists { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, BinderAppSettingsSourceGenerationContext.Default.AppSettings);
|
|
return result;
|
|
}
|
|
|
|
private static void PreVerify(IConfigurationRoot configurationRoot, AppSettings? appSettings)
|
|
{
|
|
if (appSettings?.Company is null)
|
|
{
|
|
List<string> paths = [];
|
|
foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers)
|
|
{
|
|
if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider)
|
|
continue;
|
|
if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider)
|
|
continue;
|
|
paths.Add(physicalFileProvider.Root);
|
|
}
|
|
throw new NotSupportedException($"Not found!{Environment.NewLine}{string.Join(Environment.NewLine, paths.Distinct())}");
|
|
}
|
|
}
|
|
|
|
private static void Verify(Models.AppSettings appSettings)
|
|
{
|
|
if (appSettings.MaxDegreeOfParallelism > 1 && (appSettings.RenameConfiguration.InPlaceWithOriginalName || appSettings.RenameConfiguration.InPlace))
|
|
throw new NotSupportedException($"Change configuration: {nameof(appSettings.RenameConfiguration.InPlace)} or {nameof(appSettings.MaxDegreeOfParallelism)}");
|
|
if (appSettings.RenameConfiguration.InPlaceWithOriginalName && appSettings.RenameConfiguration.InPlace)
|
|
throw new NotSupportedException($"Change configuration: {nameof(appSettings.RenameConfiguration.InPlace)} or {nameof(appSettings.RenameConfiguration.InPlaceWithOriginalName)}");
|
|
}
|
|
|
|
private static Models.AppSettings Get(AppSettings? appSettings, RenameConfiguration renameConfiguration)
|
|
{
|
|
Models.AppSettings result;
|
|
if (appSettings is null) throw new NullReferenceException(nameof(appSettings));
|
|
if (appSettings.Company is null) throw new NullReferenceException(nameof(appSettings.Company));
|
|
if (appSettings.MaxDegreeOfParallelism is null) throw new NullReferenceException(nameof(appSettings.MaxDegreeOfParallelism));
|
|
if (appSettings.RequireRootDirectoryExists is null) throw new NullReferenceException(nameof(appSettings.RequireRootDirectoryExists));
|
|
result = new(renameConfiguration,
|
|
appSettings.Company,
|
|
appSettings.MaxDegreeOfParallelism.Value,
|
|
appSettings.RequireRootDirectoryExists.Value);
|
|
Verify(result);
|
|
return result;
|
|
}
|
|
|
|
private static Models.AppSettings Get(AppSettings? appSettings)
|
|
{
|
|
Models.AppSettings? result;
|
|
string? json;
|
|
if (appSettings is null || appSettings.ConfigurationFileName is null)
|
|
throw new NotSupportedException($"{nameof(appSettings.ConfigurationFileName)} must be set!");
|
|
string jsonFile = Path.Combine(AppContext.BaseDirectory, appSettings.ConfigurationFileName);
|
|
if (File.Exists(jsonFile))
|
|
json = File.ReadAllText(jsonFile);
|
|
else
|
|
{
|
|
json = null;
|
|
string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
List<string> collection = [applicationData];
|
|
if (appSettings?.ConfigurationDirectoryNames is not null)
|
|
collection.AddRange(appSettings.ConfigurationDirectoryNames);
|
|
if (appSettings?.ConfigurationFileName is not null)
|
|
collection.Add(appSettings.ConfigurationFileName);
|
|
jsonFile = Path.Combine(collection.ToArray());
|
|
}
|
|
if (string.IsNullOrEmpty(json) && File.Exists(jsonFile))
|
|
json = File.ReadAllText(jsonFile);
|
|
result = (string.IsNullOrEmpty(json) ? null : result = JsonSerializer.Deserialize(json, AppSettingsSourceGenerationContext.Default.AppSettings)) ??
|
|
throw new NullReferenceException(nameof(Models.AppSettings));
|
|
result = Get(appSettings, result.RenameConfiguration);
|
|
return result;
|
|
}
|
|
|
|
public static Models.AppSettings Get(IConfigurationRoot configurationRoot)
|
|
{
|
|
Models.AppSettings result;
|
|
#pragma warning disable IL3050, IL2026
|
|
AppSettings? appSettings = configurationRoot.Get<AppSettings>();
|
|
#pragma warning restore IL3050, IL2026
|
|
PreVerify(configurationRoot, appSettings);
|
|
result = Get(appSettings);
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
[JsonSerializable(typeof(AppSettings))]
|
|
internal partial class BinderAppSettingsSourceGenerationContext : JsonSerializerContext
|
|
{
|
|
} |