-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
181 lines (152 loc) · 7.28 KB
/
Copy pathProgram.cs
File metadata and controls
181 lines (152 loc) · 7.28 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// ------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="Starion Group S.A.">
//
// Copyright 2017-2026 Starion Group S.A.
// SPDX-License-Identifier: Apache-2.0
//
// </copyright>
// ------------------------------------------------------------------------------------------------
namespace ECoreNetto.Tools
{
using System.CommandLine;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Autofac.Extensions.DependencyInjection;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using ECoreNetto.Tools.Commands;
using ECoreNetto.Reporting.Drawing;
using ECoreNetto.Reporting.Generators;
using ECoreNetto.Tools.Services;
/// <summary>
/// Main entry point for the command line application
/// </summary>
[ExcludeFromCodeCoverage]
public static class Program
{
/// <summary>
/// Runtime-adjustable Serilog minimum level.
/// </summary>
private static readonly LoggingLevelSwitch LoggingLevelSwitch = new();
/// <summary>
/// Main entry point for the command line application
/// </summary>
/// <param name="args">
/// command line arguments
/// </param>
public static async Task<int> Main(string[] args)
{
using var host = CreateHostBuilder(args).Build();
var rootCommand = CreateCommandChain(host);
var parseResult = rootCommand.Parse(args);
var result = await parseResult.InvokeAsync();
return result;
}
/// <summary>
/// Creates the <see cref="IHostBuilder"/>
/// </summary>
/// <param name="args">
/// the command line arguments
/// </param>
/// <returns>
/// a configured instance of <see cref="IHostBuilder"/>
/// </returns>
private static IHostBuilder CreateHostBuilder(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
var template = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] ({SourceContext}) {Message:lj}{NewLine}{Exception}";
var loggerConfig = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.ControlledBy(LoggingLevelSwitch)
.WriteTo.File("ecorenetto.logs",
rollingInterval: RollingInterval.Day,
outputTemplate: template);
var serilogLogger = loggerConfig.CreateLogger();
loggingBuilder.AddSerilog(serilogLogger, dispose: true);
loggingBuilder.SetMinimumLevel(LogLevel.Information);
})
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
{
services.AddHttpClient();
services.AddSingleton<IVersionChecker, VersionChecker>();
services.AddSingleton<IXlReportGenerator, XlReportGenerator>();
services.AddSingleton<IModelInspector, ModelInspector>();
services.AddSingleton<IInheritanceDiagramRenderer, InheritanceDiagramRenderer>();
services.AddSingleton<IAssociationDiagramRenderer, AssociationDiagramRenderer>();
services.AddSingleton<IHtmlReportGenerator, HtmlReportGenerator>();
services.AddSingleton<IMarkdownReportGenerator, MarkdownReportGenerator>();
});
return host;
}
/// <summary>
/// Creates the root and sub commands
/// </summary>
/// <returns>
/// returns an instance of <see cref="RootCommand"/>
/// </returns>
private static RootCommand CreateCommandChain(IHost host)
{
var root = new RootCommand("ECoreNetto Tools");
var reportCommand = new XlReportCommand();
reportCommand.SetAction((parseResult, cancellationToken) =>
{
ApplyLogLevel(parseResult);
using var scope = host.Services.CreateScope();
var generator = scope.ServiceProvider.GetRequiredService<IXlReportGenerator>();
var versionChecker = scope.ServiceProvider.GetRequiredService<IVersionChecker>();
var handler = new XlReportCommand.Handler(generator, versionChecker);
return handler.InvokeAsync(parseResult, cancellationToken);
});
root.Add(reportCommand);
var modelInspectionCommand = new ModelInspectionCommand();
modelInspectionCommand.SetAction((parseResult, cancellationToken) =>
{
ApplyLogLevel(parseResult);
using var scope = host.Services.CreateScope();
var generator = scope.ServiceProvider.GetRequiredService<IModelInspector>();
var versionChecker = scope.ServiceProvider.GetRequiredService<IVersionChecker>();
var handler = new ModelInspectionCommand.Handler(generator, versionChecker);
return handler.InvokeAsync(parseResult, cancellationToken);
});
root.Add(modelInspectionCommand);
var htmlReportCommand = new HtmlReportCommand();
htmlReportCommand.SetAction((parseResult, cancellationToken) =>
{
ApplyLogLevel(parseResult);
using var scope = host.Services.CreateScope();
var generator = scope.ServiceProvider.GetRequiredService<IHtmlReportGenerator>();
var versionChecker = scope.ServiceProvider.GetRequiredService<IVersionChecker>();
var handler = new HtmlReportCommand.Handler(generator, versionChecker);
return handler.InvokeAsync(parseResult, cancellationToken);
});
root.Add(htmlReportCommand);
var markdownReportCommand = new MarkdownReportCommand();
markdownReportCommand.SetAction((parseResult, cancellationToken) =>
{
ApplyLogLevel(parseResult);
using var scope = host.Services.CreateScope();
var generator = scope.ServiceProvider.GetRequiredService<IMarkdownReportGenerator>();
var versionChecker = scope.ServiceProvider.GetRequiredService<IVersionChecker>();
var handler = new MarkdownReportCommand.Handler(generator, versionChecker);
return handler.InvokeAsync(parseResult, cancellationToken);
});
root.Add(markdownReportCommand);
return root;
}
/// <summary>
/// Reads the log level from the parse result and updates the Serilog <see cref="LoggingLevelSwitch"/>>.
/// </summary>
private static void ApplyLogLevel(ParseResult parseResult)
{
LoggingLevelSwitch.MinimumLevel = parseResult.GetValue<LogEventLevel>("--log-level");
}
}
}