public override void ConfigureCommand(IPluginConfigurationContext buildContext, CommandLineApplication command) { command.OnExecuteAsync(async cancellationToken => { var(commandConfigurationContext, commandConfiguration) = BuildCommandConfiguration(buildContext); var(commandServicesContext, serviceProvider) = BuildCommandServiceProvider( buildContext, commandConfiguration); var scopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>(); using (var scope = scopeFactory.CreateScope()) { var scopeServiceProvider = scope.ServiceProvider; return(await BuildAndRunCommand( buildContext, command, commandConfiguration, scopeServiceProvider, commandServicesContext, cancellationToken)); } }); }
public void ConfigureCommand(IPluginConfigurationContext context, CommandLineApplication command) { foreach (var configurator in CommandConfiguration) { configurator.Invoke(context, command); } }
protected virtual ICommonCommandPlugin CreateRootCommand(IPluginConfigurationContext pluginConfigurationContext) => new CommandPluginBuilder.CommandPluginBuilder() .WithName(this.Name) .WithDescription(this.Description) .ConfigureCommand((ctx, commandLineApp) => { commandLineApp.HelpOption(true); foreach (var commandLinePlugin in CommandPlugins) { var commandName = commandLinePlugin.Key; var plugin = commandLinePlugin.Value; commandLineApp.Command(commandName, childCommand => { childCommand.Description = plugin.Description; plugin.ConfigureCommand(pluginConfigurationContext, childCommand); }); } commandLineApp.OnExecute(() => { commandLineApp.ShowHelp(); return(1); }); }) .Build();
protected virtual (ICommandConfigurationContext, IConfigurationRoot) BuildCommandConfiguration( IPluginConfigurationContext buildContext) { var commandConfigurationContext = new CommandConfigurationContext( buildContext.HostConfiguration, buildContext.HostServices); var commandConfigurationBuilder = new ConfigurationBuilder(); buildContext.ConfigureCommonConfiguration(commandConfigurationContext, commandConfigurationBuilder); ConfigureCommandConfiguration(commandConfigurationContext, commandConfigurationBuilder); var commandConfiguration = commandConfigurationBuilder.Build(); return(commandConfigurationContext, commandConfiguration); }
#pragma warning disable CA1801 private async Task <int> BuildAndRunCommand( IPluginConfigurationContext buildContext, CommandLineApplication command, IConfigurationRoot commandConfiguration, IServiceProvider scopeServiceProvider, ICommandServicesContext commandServicesContext, CancellationToken cancellationToken) { var commandContext = new CommandContext( commandServicesContext.HostConfiguration, commandServicesContext.HostServices, commandConfiguration, scopeServiceProvider, command); var commandInstance = scopeServiceProvider.GetRequiredService <ICommand>(); return(await commandInstance.RunAsync(commandContext, cancellationToken)); }
protected (ICommandServicesContext, IServiceProvider) BuildCommandServiceProvider( IPluginConfigurationContext buildContext, IConfigurationRoot commandConfiguration) { var commandServicesContext = new CommandServicesContext( buildContext.HostConfiguration, buildContext.HostServices, commandConfiguration); var commandServices = new ServiceCollection(); commandServices.AddLogging(loggingBuilder => { buildContext.ConfigureLogBuilder(loggingBuilder); }); commandServices.AddSingleton <ICommand, TCommand>(); buildContext.ConfigureCommonCommandServices(commandServicesContext, commandServices); ConfigureCommandServices(commandServicesContext, commandServices); var serviceProvider = commandServices.BuildServiceProvider(); return(commandServicesContext, serviceProvider); }
public virtual void ConfigureCommand(IPluginConfigurationContext context, CommandLineApplication command) { }