예제 #1
0
        /// <summary>
        ///     Runs an instance of <see cref="CommandLineApplication" /> to provide
        ///     command line parsing on the given <paramref name="args" />.  This method should be the primary approach
        ///     taken for command line applications which use the Builder API for configuration.
        /// </summary>
        /// <param name="hostBuilder">This instance</param>
        /// <param name="args">The command line arguments</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <returns>A task whose result is the exit code of the application</returns>
        public static async Task <int> RunCommandLineApplicationAsync(
            this IHostBuilder hostBuilder, string[] args, CancellationToken cancellationToken = default)
        {
            var exceptionHandler = new StoreExceptionHandler();
            var state            = new CommandLineState(args);

            hostBuilder.ConfigureServices(
                (context, services)
                =>
            {
                services
                .TryAddSingleton <IUnhandledExceptionHandler>(exceptionHandler);
                services
                .AddSingleton <IHostLifetime, CommandLineLifetime>()
                .TryAddSingleton(PhysicalConsole.Singleton);
                services
                .AddSingleton(provider =>
                {
                    state.SetConsole(provider.GetService <IConsole>());
                    return(state);
                })
                .AddSingleton <CommandLineContext>(state)
                .AddSingleton <ICommandLineService, CommandLineService>();
            });

            using var host = hostBuilder.Build();
            await host.RunAsync(cancellationToken);

            if (exceptionHandler.StoredException != null)
            {
                ExceptionDispatchInfo.Capture(exceptionHandler.StoredException).Throw();
            }

            return(state.ExitCode);
        }
예제 #2
0
        /// <summary>
        /// Configures an instance of <see cref="IHostBuilder"/> for running a <see cref="CommandLineApplication"/>.
        /// </summary>
        /// <typeparam name="TApp"></typeparam>
        /// <param name="hostBuilder"></param>
        /// <param name="args"></param>
        /// <param name="applicationState"></param>
        /// <returns></returns>
        public static IHostBuilder ConfigureCommandLineApplication <TApp>(
            this IHostBuilder hostBuilder,
            string[] args,
            out CommandLineState applicationState)
            where TApp : class
        {
            var state = new CommandLineState(args);

            hostBuilder.ConfigureServices((_, services) =>
            {
                services.AddSingleton(PhysicalConsole.Singleton);

                services.TryAddSingleton(provider =>
                {
                    state.SetConsole(provider.GetRequiredService <IConsole>());
                    return(state);
                });

                services
                .AddSingleton <IHostLifetime, CommandLineLifetime>()
                .AddSingleton <CommandLineContext>(state)
                .AddSingleton <ICommandLineService, CommandLineService <TApp> >();
            });

            applicationState = state;
            return(hostBuilder);
        }
예제 #3
0
        /// <summary>
        ///     Runs an instance of <typeparamref name="TApp" /> using <see cref="CommandLineApplication" /> to provide
        ///     command line parsing on the given <paramref name="args" />.  This method should be the primary approach
        ///     taken for command line applications.
        /// </summary>
        /// <typeparam name="TApp">The type of the command line application implementation.</typeparam>
        /// <param name="hostBuilder">This instance.</param>
        /// <param name="args">The command line arguments.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A task whose result is the exit code of the application.</returns>
        public static async Task <int> RunCommandLineAsync <TApp>(
            this IHostBuilder hostBuilder,
            string[] args,
            CancellationToken cancellationToken = default) where TApp : class
        {
            var exceptionHandler = new StoreExceptionHandler();
            var state            = new CommandLineState(args);

            hostBuilder.ConfigureServices(
                (context, services)
                =>
            {
                services
                .TryAddSingleton <IUnhandledExceptionHandler>(exceptionHandler);
                services
                .AddSingleton <IHostLifetime, CommandLineLifetime>()
                .TryAddSingleton(PhysicalConsole.Singleton);
                services
                .AddSingleton(provider =>
                {
                    state.SetConsole(provider.GetService <IConsole>());
                    return(state);
                })
                .AddSingleton <CommandLineContext>(state)
                .AddSingleton <ICommandLineService, CommandLineService <TApp> >();
            });

            hostBuilder.UseSerilog((context, logger) =>
            {
                var configuration = context.Configuration;

                logger.ReadFrom.Configuration(configuration)
                .Enrich.FromLogContext()
                .WriteTo.Console();
            });

            using var host = hostBuilder.Build();
            await host.RunAsync(cancellationToken);

            if (exceptionHandler.StoredException != null)
            {
                ExceptionDispatchInfo.Capture(exceptionHandler.StoredException).Throw();
            }

            return(state.ExitCode);
        }
예제 #4
0
        /// <summary>
        ///     Configures an instance of <typeparamref name="TApp" /> using <see cref="CommandLineApplication" /> to provide
        ///     command line parsing on the given <paramref name="args" />.
        /// </summary>
        /// <typeparam name="TApp">The type of the command line application implementation</typeparam>
        /// <param name="hostBuilder">This instance</param>
        /// <param name="args">The command line arguments</param>
        /// <param name="configure">The delegate to configure the application</param>
        /// <returns><see cref="IHostBuilder"/></returns>
        public static IHostBuilder UseCommandLineApplication <TApp>(
            this IHostBuilder hostBuilder,
            string[] args,
            Action <CommandLineApplication <TApp> > configure)
            where TApp : class
        {
            configure ??= _ => { };
            var state = new CommandLineState(args);

            hostBuilder.Properties[typeof(CommandLineState)] = state;
            hostBuilder.ConfigureServices((context, services) =>
                                          services
                                          .AddCommonServices(state)
                                          .AddSingleton <ICommandLineService, CommandLineService <TApp> >()
                                          .AddSingleton(configure));

            return(hostBuilder);
        }
예제 #5
0
        /// <summary>
        ///     Runs an instance of <see cref="CommandLineApplication" /> using builder API to provide
        ///     command line parsing on the given <paramref name="args" />.
        /// </summary>
        /// <param name="hostBuilder">This instance</param>
        /// <param name="args">The command line arguments</param>
        /// <param name="configure">The delegate to configure the application</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <returns>A task whose result is the exit code of the application</returns>
#pragma warning disable RS0026 // disable warning about optional parameter for CancellationToken
        public static async Task <int> RunCommandLineApplicationAsync(
#pragma warning restore RS0026
            this IHostBuilder hostBuilder,
            string[] args,
            Action <CommandLineApplication> configure,
            CancellationToken cancellationToken = default)
        {
            configure ??= _ => { };
            var state = new CommandLineState(args);

            hostBuilder.Properties[typeof(CommandLineState)] = state;
            hostBuilder.ConfigureServices((context, services) =>
                                          services
                                          .AddCommonServices(state)
                                          .AddSingleton <ICommandLineService, CommandLineService>()
                                          .AddSingleton(configure));

            using var host = hostBuilder.Build();
            return(await host.RunCommandLineApplicationAsync(cancellationToken));
        }
예제 #6
0
        /// <summary>
        ///     Runs an instance of <typeparamref name="TApp" /> using <see cref="CommandLineApplication" /> to provide
        ///     command line parsing on the given <paramref name="args" />.  This method should be the primary approach
        ///     taken for command line applications.
        /// </summary>
        /// <typeparam name="TApp">The type of the command line application implementation</typeparam>
        /// <param name="hostBuilder">This instance</param>
        /// <param name="args">The command line arguments</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <returns>A task whose result is the exit code of the application</returns>
        public static async Task <int> RunCommandLineApplicationAsync <TApp>(
            this IHostBuilder hostBuilder, string[] args, CancellationToken cancellationToken = default)
            where TApp : class
        {
            var state = new CommandLineState(args);

            hostBuilder.ConfigureServices(
                (context, services)
                => services
                .AddSingleton <IHostLifetime, CommandLineLifetime>()
                .AddSingleton(state)
                .AddSingleton <CommandLineContext>(state)
                .AddSingleton(PhysicalConsole.Singleton)
                .AddSingleton <ICommandLineService, CommandLineService <TApp> >());

            using (var host = hostBuilder.Build())
            {
                await host.RunAsync(cancellationToken);

                return(state.ExitCode);
            }
        }
예제 #7
0
        public CommandLineApplication(params string[] args)
        {
            _console = new StringOutputConsole();
            FakeHttpClientHandler = A.Fake <ServersListApiClientHandler>(o => o.CallsBaseMethods());

            _host = new HostBuilder()
                    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                    .ConfigureContainer <ContainerBuilder>(HostConfiguration.Container)
                    .ConfigureServices(HostConfiguration.Services)
                    .ConfigureLogging(HostConfiguration.Logging)
                    .ConfigureCommandLineApplication <RootCommandHandler>(args, out var state)
                    .ConfigureContainer <ContainerBuilder>(containerBuilder =>
            {
                containerBuilder.RegisterInstance(_console);
                containerBuilder.RegisterInstance <HttpClientHandler>(FakeHttpClientHandler);
                containerBuilder.RegisterInstance(new ServersListRenderer(false));
            })
                    .Build();

            _state           = state;
            _databaseContext = _host.Services.GetRequiredService <ServersDbContext>();
        }
예제 #8
0
 private static IServiceCollection AddCommonServices(this IServiceCollection services, CommandLineState state)
 {
     services.TryAddSingleton <StoreExceptionHandler>();
     services.TryAddSingleton <IUnhandledExceptionHandler>(provider => provider.GetRequiredService <StoreExceptionHandler>());
     services.TryAddSingleton(PhysicalConsole.Singleton);
     services
     .AddSingleton <IHostLifetime, CommandLineLifetime>()
     .AddSingleton(provider =>
     {
         state.SetConsole(provider.GetRequiredService <IConsole>());
         return(state);
     })
     .AddSingleton <CommandLineContext>(state);
     return(services);
 }