/// <inheritdoc/> public async Task <int> RunAsync() { // Remove the synchronization context await default(SynchronizationContextRemover); // Populate the class catalog (if we haven't already) _classCatalog.Populate(); // Run bootstrapper configurators first Configurators.Configure <IConfigurableBootstrapper>(this); Configurators.Configure <IBootstrapper>(this); // Create the service collection IServiceCollection serviceCollection = CreateServiceCollection() ?? new ServiceCollection(); serviceCollection.TryAddSingleton <IConfigurableBootstrapper>(this); serviceCollection.TryAddSingleton <IBootstrapper>(this); serviceCollection.TryAddSingleton(_classCatalog); // The class catalog is retrieved later for deferred logging once a service provider is built // Run configurators on the service collection ConfigurableServices configurableServices = new ConfigurableServices(serviceCollection); Configurators.Configure(configurableServices); // Add simple logging to make sure it's available in commands before the engine adds in serviceCollection.AddLogging(); // Create the stand-alone command line service container and register a few types needed for the CLI CommandServiceTypeRegistrar registrar = new CommandServiceTypeRegistrar(); registrar.RegisterInstance(typeof(IServiceCollection), serviceCollection); registrar.RegisterInstance(typeof(IConfiguratorCollection), Configurators); // Create the command line parser and run the command ICommandApp app = _getCommandApp(registrar); app.Configure(x => { x.ValidateExamples(); ConfigurableCommands configurableCommands = new ConfigurableCommands(x); Configurators.Configure(configurableCommands); }); return(await app.RunAsync(Args)); }
public sealed override async Task <int> ExecuteAsync(CommandContext context, TSettings commandSettings) { // Set verbose tracing if (commandSettings.LogLevel != LogLevel.Information) { ServiceCollection.Configure <LoggerFilterOptions>(options => options.MinLevel = commandSettings.LogLevel); } // File logging if (!string.IsNullOrEmpty(commandSettings.LogFile)) { // Add the log provider (adding it to the service collection will get picked up by the logger factory) ServiceCollection.AddSingleton <ILoggerProvider, FileLoggerProvider>(); ServiceCollection.Configure <FileLoggerOptions>(options => { options.FileName = commandSettings.LogFile; options.LogDirectory = string.Empty; }); } // Build a temporary service provider so we can log // Make sure to place it in it's own scope so transient services get correctly disposed IServiceProvider services = ServiceCollection.BuildServiceProvider(); ClassCatalog classCatalog = services.GetService <ClassCatalog>(); using (IServiceScope serviceScope = services.CreateScope()) { // Log pending messages ILogger logger = serviceScope.ServiceProvider.GetRequiredService <ILogger <Bootstrapper> >(); logger.LogInformation($"Statiq version {Engine.Version}"); classCatalog?.LogDebugMessages(logger); // Attach if (commandSettings.Attach) { logger.LogInformation($"Waiting for a debugger to attach to process {Process.GetCurrentProcess().Id} (or press a key to continue)..."); while (!Debugger.IsAttached && !Console.KeyAvailable) { Thread.Sleep(100); } if (Console.KeyAvailable) { Console.ReadKey(true); logger.LogInformation("Key pressed, continuing execution"); } else { logger.LogInformation("Debugger attached, continuing execution"); } } } // Add settings if (commandSettings.Settings?.Length > 0) { foreach (KeyValuePair <string, string> setting in SettingsParser.Parse(commandSettings.Settings)) { ConfigurationSettings[setting.Key] = setting.Value; } } // Configure settings after other configuration so they can use the values ConfigurableSettings configurableSettings = new ConfigurableSettings(ConfigurationSettings); Configurators.Configure(configurableSettings); return(await ExecuteCommandAsync(context, commandSettings)); }
/// <inheritdoc/> public async Task <int> RunAsync() { // Remove the synchronization context await default(SynchronizationContextRemover); // Populate the class catalog (if we haven't already) _classCatalog.Populate(); // Run bootstrapper configurators first Configurators.Configure <IConfigurableBootstrapper>(this); Configurators.Configure <IBootstrapper>(this); // Run the configuration configurator and get the configuration root SettingsConfigurationProvider settingsProvider = new SettingsConfigurationProvider(); IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); ConfigurableConfiguration configurableConfiguration = new ConfigurableConfiguration(configurationBuilder); Configurators.Configure(configurableConfiguration); configurationBuilder.Add(settingsProvider); IConfigurationRoot configurationRoot = configurationBuilder.Build(); ConfigurationSettings configurationSettings = new ConfigurationSettings(settingsProvider, configurationRoot); // Create the service collection IServiceCollection serviceCollection = CreateServiceCollection() ?? new ServiceCollection(); serviceCollection.TryAddSingleton <IConfigurableBootstrapper>(this); serviceCollection.TryAddSingleton <IBootstrapper>(this); serviceCollection.TryAddSingleton(_classCatalog); // The class catalog is retrieved later for deferred logging once a service provider is built serviceCollection.TryAddSingleton <IConfiguration>(configurationRoot); // Run configurators on the service collection ConfigurableServices configurableServices = new ConfigurableServices(serviceCollection, configurationRoot); Configurators.Configure(configurableServices); // Add simple logging to make sure it's available in commands before the engine adds in, // but add it after the configurators have a chance to configure logging serviceCollection.AddLogging(); // Create the stand-alone command line service container and register a few types needed for the CLI CommandServiceTypeRegistrar registrar = new CommandServiceTypeRegistrar(); registrar.RegisterInstance(typeof(IConfigurationSettings), configurationSettings); registrar.RegisterInstance(typeof(IConfigurationRoot), configurationRoot); registrar.RegisterInstance(typeof(IServiceCollection), serviceCollection); registrar.RegisterInstance(typeof(IConfiguratorCollection), Configurators); registrar.RegisterInstance(typeof(IBootstrapper), this); // Create the command line parser and run the command ICommandApp app = _getCommandApp(registrar); app.Configure(commandConfigurator => { commandConfigurator.ValidateExamples(); ConfigurableCommands configurableCommands = new ConfigurableCommands(commandConfigurator); Configurators.Configure(configurableCommands); }); int exitCode = await app.RunAsync(Arguments); // Dispose all instances of the console logger to flush the message queue and stop the listening thread ConsoleLoggerProvider.DisposeAll(); return(exitCode); }