mfgames-cil/src/MfGames.Nitride/Commands/BuildCommand.cs
D. Moonfire d258e52697
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
fix: allow Nitride.Temporal to be used twice
2023-09-03 18:51:56 -05:00

81 lines
2.1 KiB
C#

using System.CommandLine;
using System.CommandLine.Invocation;
using MfGames.Nitride.Pipelines;
using Serilog;
namespace MfGames.Nitride.Commands;
/// <summary>
/// The basic command to generate a website and run through the pipelines with
/// no attempt to monitor changes that happen while running.
/// </summary>
public class BuildCommand : Command, ICommandHandler
{
private readonly ILogger logger;
private readonly IList<IPipelineCommandOption> pipelineOptions;
private readonly PipelineManager pipelines;
public BuildCommand(
ILogger logger,
PipelineManager pipelines,
IList<IPipelineCommandOption> pipelineOptions)
: base("build", "Generate the website")
{
// Set up our simple member variables.
this.pipelines = pipelines;
this.pipelineOptions = pipelineOptions;
this.logger = logger.ForContext<BuildCommand>();
this.Handler = this;
// Handle any injected arguments into the command line.
foreach (IPipelineCommandOption option in pipelineOptions)
{
this.AddOption(option.Option);
}
}
/// <inheritdoc />
public int Invoke(InvocationContext context)
{
return this.InvokeAsync(context).Result;
}
/// <inheritdoc />
public async Task<int> InvokeAsync(InvocationContext context)
{
// Get the cancellation token so we can be interrupted.
CancellationToken cancellationToken = context.GetCancellationToken();
this.pipelines.CancellationToken = cancellationToken;
// 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.
DateTime start = DateTime.UtcNow;
this.logger.Information("Running pipelines");
int pipelinesResults = await this.pipelines.RunAsync();
this.logger.Information(
"Command took {Elapsed}",
DateTime.UtcNow - start);
return pipelinesResults;
}
}