-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (64 loc) · 2.27 KB
/
Program.cs
File metadata and controls
74 lines (64 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NodeGenerator.Application.Abstractions;
using NodeGenerator.Application.Parsers;
using NodeGenerator.Application.Writers;
using NodeGenerator.Options;
namespace NodeGenerator;
public static class Program
{
public static async Task<int> Main()
{
using var host = CreateHostBuilder().Build();
var generator = host.Services.GetRequiredService<Generator>();
await generator.GenerateAsync();
return Environment.ExitCode;
}
private static IHostBuilder CreateHostBuilder()
{
return new HostBuilder()
.ConfigureAppConfiguration(app =>
{
app.AddJsonFile("appsettings.json");
})
.ConfigureLogging(logging =>
{
logging.AddSimpleConsole();
})
.ConfigureServices((hostContext, services) =>
{
services.AddOptions();
services.Configure<OpcOptions>(hostContext.Configuration.GetRequiredSection(OpcOptions.Section));
services.Configure<XmlOptions>(hostContext.Configuration.GetSection(XmlOptions.Section));
services.Configure<CsvOptions>(hostContext.Configuration.GetSection(CsvOptions.Section));
services.AddSingleton<Generator>();
services.AddSingleton<IFileWriter, JsonWriter>();
services.AddSingleton<IFileParser>(provider =>
{
var config = provider.GetRequiredService<IConfiguration>();
var fileName = config.GetValue<string>("InputFileName");
if (string.IsNullOrEmpty(fileName))
{
throw new NullReferenceException("InputFileName");
}
var extension = Path.GetExtension(fileName);
if (".xml".Equals(extension, StringComparison.OrdinalIgnoreCase))
{
var opc = provider.GetRequiredService<IOptions<OpcOptions>>();
var xml = provider.GetRequiredService<IOptions<XmlOptions>>();
return new XmlParser(xml, opc);
}
if (".csv".Equals(extension, StringComparison.OrdinalIgnoreCase))
{
var opc = provider.GetRequiredService<IOptions<OpcOptions>>();
var csv = provider.GetRequiredService<IOptions<CsvOptions>>();
return new CsvParser(csv, opc);
}
throw new NotSupportedException($"Extension '{extension}' is not supported.");
});
});
}
}