1- using System . CommandLine . Builder ;
2- using System . CommandLine . Hosting ;
3- using System . CommandLine . Parsing ;
1+ using System . CommandLine . Parsing ;
42using System . Diagnostics . CodeAnalysis ;
53using HydraScript ;
64using HydraScript . Infrastructure ;
75using Microsoft . Extensions . DependencyInjection ;
8- using Microsoft . Extensions . Hosting ;
96using Microsoft . Extensions . Logging ;
107using Microsoft . Extensions . Logging . Console ;
118
12- return GetRunner ( ConfigureHost ) . Invoke ( args ) ;
9+ return CliParser . Parse ( GetCommand ( ) , args ) . Invoke ( ) ;
1310
1411[ ExcludeFromCodeCoverage ]
1512internal static partial class Program
1613{
17- internal static readonly ExecuteCommand Command = new ( ) ;
18-
19- internal static Parser GetRunner ( Action < IHostBuilder > configureHost , bool useDefault = true )
14+ private static ExecuteCommand GetCommand ( )
2015 {
21- var builder = new CommandLineBuilder ( Command )
22- . UseHost ( Host . CreateDefaultBuilder , configureHost ) ;
23- if ( useDefault )
24- builder = builder . UseDefaults ( ) ;
25- return builder . Build ( ) ;
16+ ExecuteCommand command = new ( ) ;
17+ command . SetAction ( parseResult =>
18+ {
19+ var fileInfo = parseResult . GetValue ( command . PathArgument ) ! ;
20+ var dump = parseResult . GetValue ( command . DumpOption ) ;
21+ var serviceProvider = GetServiceProvider ( fileInfo , dump ) ;
22+ var executor = serviceProvider . GetRequiredService < Executor > ( ) ;
23+ return executor . Invoke ( ) ;
24+ } ) ;
25+ return command ;
2626 }
2727
28- private static void ConfigureHost ( IHostBuilder builder ) => builder
29- . ConfigureServices ( ( context , services ) =>
30- {
31- services . AddLogging ( c => c . ClearProviders ( )
32- . AddConsole ( options => options . FormatterName = nameof ( SimplestConsoleFormatter ) )
33- . AddConsoleFormatter < SimplestConsoleFormatter , ConsoleFormatterOptions > ( ) ) ;
34- services . Configure < InvocationLifetimeOptions > ( options => options . SuppressStatusMessages = true ) ;
35- var parseResult = context . GetInvocationContext ( ) . ParseResult ;
36- var fileInfo = parseResult . GetValueForArgument ( Command . PathArgument ) ;
37- var dump = parseResult . GetValueForOption ( Command . DumpOption ) ;
38- services
39- . AddDomain ( )
40- . AddApplication ( )
41- . AddInfrastructure ( dump , fileInfo ) ;
42- } )
43- . UseDefaultServiceProvider ( ( _ , options ) => options . ValidateScopes = true )
44- . UseCommandHandler < ExecuteCommand , ExecuteCommandHandler > ( ) ;
28+ internal static IServiceProvider GetServiceProvider (
29+ FileInfo fileInfo ,
30+ bool dump ,
31+ Action < IServiceCollection > ? configureServices = null )
32+ {
33+ var services = new ServiceCollection ( ) ;
34+ services . AddLogging ( c => c . ClearProviders ( )
35+ . AddConsole ( options => options . FormatterName = nameof ( SimplestConsoleFormatter ) )
36+ . AddConsoleFormatter < SimplestConsoleFormatter , ConsoleFormatterOptions > ( ) ) ;
37+ services
38+ . AddDomain ( )
39+ . AddApplication ( )
40+ . AddInfrastructure ( dump , fileInfo ) ;
41+ configureServices ? . Invoke ( services ) ;
42+ return services . BuildServiceProvider ( ) ;
43+ }
4544}
0 commit comments