2022-06-05 18:44:51 +00:00
|
|
|
using System;
|
|
|
|
using System.CommandLine;
|
|
|
|
using System.CommandLine.Invocation;
|
|
|
|
using System.Globalization;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Nitride.Commands;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
using NodaTime.Testing;
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
namespace MfGames.Nitride.Temporal.Cli;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A factory to inject the "--date=XXXX-XX-XX" argument into the build
|
|
|
|
/// and other pipeline commands.
|
|
|
|
/// </summary>
|
|
|
|
public class DatePipelineCommandOption : IPipelineCommandOption
|
|
|
|
{
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
2022-06-07 14:40:31 +00:00
|
|
|
private readonly Timekeeper timekeeper;
|
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
public DatePipelineCommandOption(
|
|
|
|
ILogger logger,
|
|
|
|
Timekeeper timekeeper)
|
2022-06-05 18:44:51 +00:00
|
|
|
{
|
|
|
|
this.logger = logger.ForContext<Instant>();
|
2022-06-07 13:55:13 +00:00
|
|
|
this.timekeeper = timekeeper;
|
2022-07-09 04:52:10 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
this.Option = new Option<DateTime>("--date")
|
|
|
|
{
|
|
|
|
Description = "Sets the date to something other than now",
|
|
|
|
ArgumentHelpName = "DATE",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public Option Option { get; }
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public void Handle(InvocationContext context)
|
|
|
|
{
|
|
|
|
// If we got a date, then use NodaTime's fake clock to set it so
|
|
|
|
// everything will use that.
|
|
|
|
var value = (DateTime?)context.ParseResult.GetValueForOption(this.Option);
|
2022-06-07 14:40:31 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
if (value.HasValue && value.Value != DateTime.MinValue)
|
|
|
|
{
|
|
|
|
// We have a date, so we need to create a fake clock that has this
|
|
|
|
// date for the entire run.
|
2022-06-07 14:40:31 +00:00
|
|
|
var local = LocalDateTime.FromDateTime(value.Value);
|
|
|
|
ZonedDateTime zoned = local.InZoneStrictly(this.timekeeper.DateTimeZone);
|
|
|
|
var instant = zoned.ToInstant();
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2022-06-07 13:55:13 +00:00
|
|
|
this.timekeeper.Clock = new FakeClock(instant);
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report the date we are processing.
|
2022-06-07 13:55:13 +00:00
|
|
|
Instant now = this.timekeeper.Clock.GetCurrentInstant();
|
|
|
|
ZonedDateTime dateTime = now.InZone(this.timekeeper.DateTimeZone);
|
2022-06-07 14:40:31 +00:00
|
|
|
string formatted = dateTime.ToString("G", CultureInfo.InvariantCulture);
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2022-06-07 15:05:20 +00:00
|
|
|
this.logger.Information("Setting date/time to {When:l}", formatted);
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
}
|