150 lines
7.2 KiB
C#
150 lines
7.2 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace File_Watcher.Models.Binder;
|
|
|
|
public class AppSettings
|
|
{
|
|
|
|
public string? BuildNumber { get; set; }
|
|
public string[]? ConfigurationDirectoryNames { get; set; }
|
|
public int? ConfigurationSpecialFolder { get; set; }
|
|
public string? ConfigurationFileName { get; set; }
|
|
public string? Company { get; set; }
|
|
public string? GitCommitSeven { get; set; }
|
|
public string? Helper { get; set; }
|
|
public int? MillisecondsDelay { get; set; }
|
|
public string? WatchDirectory { 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?.BuildNumber 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(AppSettings _)
|
|
{
|
|
}
|
|
|
|
private static Models.AppSettings Get(AppSettings? appSettings,
|
|
CamstarOracleConfiguration camstarOracleConfiguration,
|
|
CompassConfiguration compassConfiguration,
|
|
DriveConfiguration driveConfiguration,
|
|
EAFLogConfiguration eafLogConfiguration,
|
|
EDADatabaseConfiguration edaDatabaseConfiguration,
|
|
InfinityQSConfiguration infinityQSConfiguration,
|
|
IsoConfiguration isoConfiguration,
|
|
MetrologyConfiguration metrologyConfiguration,
|
|
NugetConfiguration nugetConfiguration,
|
|
SerialConfiguration serialConfiguration,
|
|
StratusConfiguration stratusConfiguration,
|
|
TransmissionControlProtocolConfiguration transmissionControlProtocolConfiguration,
|
|
WaferCounterConfiguration waferCounterConfiguration)
|
|
{
|
|
Models.AppSettings result;
|
|
if (appSettings is null) throw new NullReferenceException(nameof(appSettings));
|
|
if (appSettings.BuildNumber is null) throw new NullReferenceException(nameof(BuildNumber));
|
|
if (appSettings.Company is null) throw new NullReferenceException(nameof(Company));
|
|
if (appSettings.GitCommitSeven is null) throw new NullReferenceException(nameof(GitCommitSeven));
|
|
if (appSettings.Helper is null) throw new NullReferenceException(nameof(Helper));
|
|
if (appSettings.MillisecondsDelay is null) throw new NullReferenceException(nameof(MillisecondsDelay));
|
|
if (appSettings.WatchDirectory is null) throw new NullReferenceException(nameof(WatchDirectory));
|
|
Verify(appSettings);
|
|
result = new(camstarOracleConfiguration,
|
|
compassConfiguration,
|
|
driveConfiguration,
|
|
eafLogConfiguration,
|
|
edaDatabaseConfiguration,
|
|
infinityQSConfiguration,
|
|
isoConfiguration,
|
|
metrologyConfiguration,
|
|
nugetConfiguration,
|
|
serialConfiguration,
|
|
stratusConfiguration,
|
|
transmissionControlProtocolConfiguration,
|
|
waferCounterConfiguration,
|
|
appSettings.BuildNumber,
|
|
appSettings.Company,
|
|
appSettings.GitCommitSeven,
|
|
appSettings.Helper,
|
|
appSettings.MillisecondsDelay.Value,
|
|
appSettings.WatchDirectory);
|
|
return result;
|
|
}
|
|
|
|
private static Models.AppSettings Get(AppSettings? appSettings)
|
|
{
|
|
Models.AppSettings? results;
|
|
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);
|
|
results = (string.IsNullOrEmpty(json) ? null : results = JsonSerializer.Deserialize(json, AppSettingsSourceGenerationContext.Default.AppSettings)) ??
|
|
throw new NullReferenceException(nameof(Models.AppSettings));
|
|
results = Get(appSettings,
|
|
results.CamstarOracleConfiguration,
|
|
results.CompassConfiguration,
|
|
results.DriveConfiguration,
|
|
results.EAFLogConfiguration,
|
|
results.EDADatabaseConfiguration,
|
|
results.InfinityQSConfiguration,
|
|
results.IsoConfiguration,
|
|
results.MetrologyConfiguration,
|
|
results.NugetConfiguration,
|
|
results.SerialConfiguration,
|
|
results.StratusConfiguration,
|
|
results.TransmissionControlProtocolConfiguration,
|
|
results.WaferCounterConfiguration);
|
|
return results;
|
|
}
|
|
|
|
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
|
|
{
|
|
} |