using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.Threading; using System.Threading.Tasks; using MfGames.Nitride.Pipelines; using Serilog; namespace MfGames.Nitride.Commands; /// /// The basic command to generate a website and run through the pipelines. /// public class BuildCommand : Command, ICommandHandler { private readonly ILogger logger; private readonly IList pipelineOptions; private readonly PipelineManager pipelines; public BuildCommand( ILogger logger, PipelineManager pipelines, IList pipelineOptions) : base("build", "Generate the website") { // Set up our simple member variables. this.pipelines = pipelines; this.pipelineOptions = pipelineOptions; this.logger = logger.ForContext(); this.Handler = this; // Handle any injected arguments into the command line. foreach (IPipelineCommandOption? option in pipelineOptions) { this.AddOption(option.Option); } } /// public int Invoke(InvocationContext context) { return this.InvokeAsync(context).Result; } /// public async Task InvokeAsync(InvocationContext context) { // Get the cancellation token so we can be interrupted. CancellationToken cancellationToken = context.GetCancellationToken(); // Process any injected options. this.logger.Debug( "Processing {Count:N0} pipeline options", this.pipelineOptions.Count); foreach (IPipelineCommandOption? option in this.pipelineOptions) { this.logger.Verbose("Processing pipeline option: {Option}", option); option.Handle(context); } // This is the main entry point into the `build` command. This runs // all the pipelines once and then quits when it finishes. this.logger.Information("Running pipelines"); int pipelinesResults = await this.pipelines.RunAsync(cancellationToken); return pipelinesResults; } }