73 lines
2.9 KiB
C#
73 lines
2.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using System.Collections.ObjectModel;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace View_by_Distance.Instance.Models.Binder;
|
|
|
|
public class AppSettings
|
|
{
|
|
|
|
public string? Company { get; set; }
|
|
public int? MaxDegreeOfParallelism { get; set; }
|
|
public string[]? Places { get; set; }
|
|
public string? WorkingDirectoryName { 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 Models.AppSettings Get(AppSettings? appSettings)
|
|
{
|
|
Models.AppSettings result;
|
|
if (appSettings?.Company is null) throw new NullReferenceException(nameof(appSettings.Company));
|
|
if (appSettings?.MaxDegreeOfParallelism is null) throw new NullReferenceException(nameof(appSettings.MaxDegreeOfParallelism));
|
|
// if (appSettings?.Places is null) throw new NullReferenceException(nameof(appSettings.Places));
|
|
if (appSettings?.WorkingDirectoryName is null) throw new NullReferenceException(nameof(appSettings.WorkingDirectoryName));
|
|
ReadOnlyCollection<Models.Place> places = Place.GetPlaces(appSettings.Places);
|
|
result = new(
|
|
appSettings.Company,
|
|
appSettings.MaxDegreeOfParallelism.Value,
|
|
places,
|
|
appSettings.WorkingDirectoryName
|
|
);
|
|
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
|
|
{
|
|
} |