fix(temporal): simplifying the setup for the builder

This commit is contained in:
Dylan R. E. Moonfire 2022-06-06 21:40:31 -05:00
parent 2e952c84cd
commit e913818e6f
3 changed files with 100 additions and 68 deletions

View file

@ -11,18 +11,18 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="6.4.0"/>
<PackageReference Include="Gallium" Version="1.0.2"/>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0"/>
<PackageReference Include="NodaTime" Version="3.1.0"/>
<PackageReference Include="NodaTime.Testing" Version="3.1.0"/>
<PackageReference Include="Serilog" Version="2.11.0"/>
<PackageReference Include="TimeSpanParserUtil" Version="1.2.0"/>
<PackageReference Include="Zio" Version="0.15.0"/>
<PackageReference Include="Autofac" Version="6.4.0" />
<PackageReference Include="Gallium" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="NodaTime" Version="3.1.0" />
<PackageReference Include="NodaTime.Testing" Version="3.1.0" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="TimeSpanParserUtil" Version="1.2.0" />
<PackageReference Include="Zio" Version="0.15.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nitride\Nitride.csproj"/>
<ProjectReference Include="..\Nitride\Nitride.csproj" />
</ItemGroup>
<!-- Include the source generator -->

View file

@ -17,68 +17,50 @@ public static class NitrideTemporalBuilderExtensions
/// Extends the builder to allow for configuring the temporal
/// settings for generation.
/// </summary>
public static NitrideBuilder UseTemporal(this NitrideBuilder builder, Action<Timekeeper>? configure = null)
public static NitrideBuilder UseTemporal(
this NitrideBuilder builder,
Action<NitrideTemporalConfiguration>? configure = null)
{
builder.ConfigureContainer(x => x.RegisterModule<NitrideTemporalModule>());
// Get the configuration so we can set the various options.
var config = new NitrideTemporalConfiguration();
if (configure != null)
{
builder.ConfigureSite((_, scope) => configure(scope.Resolve<Timekeeper>()));
}
configure?.Invoke(config);
// Add in the module registration.
builder.ConfigureContainer(
x =>
{
// Register the module.
x.RegisterModule<NitrideTemporalModule>();
// Add in the CLI options.
if (config.AddDateOptionToCommandLine)
{
x.RegisterType<DatePipelineCommandOption>().As<IPipelineCommandOption>();
}
if (config.AddExpireOptionToCommandLine && config.Expiration != null)
{
x.Register(
context =>
{
ILogger logger = context.Resolve<ILogger>();
Timekeeper clock = context.Resolve<Timekeeper>();
return new Expires(logger, clock, config.Expiration);
})
.As<IPipelineCommandOption>();
}
});
builder.ConfigureSite(
(_, scope) =>
{
Timekeeper timekeeper = scope.Resolve<Timekeeper>();
timekeeper.DateTimeZone = timekeeper.DateTimeZone;
});
return builder;
}
/// <summary>
/// Adds the "--date=XXXX-XX-XX" option into the pipeline commands.
/// </summary>
/// <param name="builder">The host builder being configured.</param>
/// <returns>The builder passed in.</returns>
public static NitrideBuilder WithClockFromOptions(this NitrideBuilder builder)
{
return builder.UseTemporal()
.ConfigureContainer(
x =>
{
x.RegisterType<DatePipelineCommandOption>().As<IPipelineCommandOption>();
});
}
/// <summary>
/// Adds the "--expire=XXXX" option into the pipeline commands where
/// "XXX" is a format like "5y" or "500000:00:00.0". This is parsed by
/// TimeSpanParser which gives an easy format.
/// </summary>
/// <param name="builder">The host builder being configured.</param>
/// <param name="defaultValue">The default expiration, if one is provided.</param>
/// <returns>The builder passed in.</returns>
public static NitrideBuilder WithExpiresFromOptions(this NitrideBuilder builder, TimeSpan defaultValue)
{
return WithExpiresFromOptions(builder, defaultValue.ToString());
}
/// <summary>
/// Adds the "--expire=XXXX" option into the pipeline commands where
/// "XXX" is a format like "5y" or "500000:00:00.0". This is parsed by
/// TimeSpanParser which gives an easy format.
/// </summary>
/// <param name="builder">The host builder being configured.</param>
/// <param name="defaultValue">The default expiration, if one is provided.</param>
/// <returns>The builder passed in.</returns>
public static NitrideBuilder WithExpiresFromOptions(this NitrideBuilder builder, string? defaultValue = null)
{
return builder.ConfigureContainer(
x =>
{
x.Register(
context =>
{
ILogger? logger = context.Resolve<ILogger>();
Timekeeper? clock = context.Resolve<Timekeeper>();
return new Expires(logger, clock, defaultValue);
})
.As<IPipelineCommandOption>();
});
}
}

View file

@ -0,0 +1,50 @@
using System;
namespace Nitride.Temporal;
/// <summary>
/// Configures the temporal settings for use with `UseTemporal`.
/// </summary>
[WithProperties]
public partial class NitrideTemporalConfiguration
{
/// <summary>
/// Adds the "--date=XXXX-XX-XX" option into the pipeline commands.
/// </summary>
public bool AddDateOptionToCommandLine { get; set; }
public bool AddExpireOptionToCommandLine { get; set; }
/// <summary>
/// Gets or sets the time zone to use for date time operations. Examples would be
/// "America/Chicago".
/// </summary>
public string? DateTimeZone { get; set; }
public string? Expiration { get; set; }
/// <summary>
/// Adds the "--expire" option into the pipeline commands where with the value set
/// to
/// the current timestamp (or the one provided by --date) plus the given timespan
/// such
/// as "500000:00:00.0".
/// </summary>
public NitrideTemporalConfiguration WithExpiresCommandLineOption(TimeSpan? timeSpan)
{
return this.WithExpiresCommandLineOption(timeSpan?.ToString());
}
/// <summary>
/// Adds the "--expire=XXXX" option into the pipeline commands where
/// "XXX" is a format like "5y" or "500000:00:00.0". This is parsed by
/// TimeSpanParser which gives an easy format.
/// </summary>
public NitrideTemporalConfiguration WithExpiresCommandLineOption(string? defaultValue)
{
this.AddExpireOptionToCommandLine = defaultValue != null;
this.Expiration = defaultValue;
return this;
}
}