Skip to content

Commit

Permalink
merged OaktonOptions into JasperFx options
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Oct 30, 2024
1 parent acbebe8 commit f257b9a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 70 deletions.
5 changes: 3 additions & 2 deletions src/CommandLineTests/HostedCommandsTester.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JasperFx.CommandLine;
using JasperFx;
using JasperFx.CommandLine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand All @@ -13,7 +14,7 @@ public async Task CanInjectServicesIntoCommands()
.ConfigureServices(services =>
{
services.AddScoped<TestDependency>();
services.AddJasperFxCommands(options =>
services.AddJasperFx(options =>
{
options.Factory = factory =>
{
Expand Down
12 changes: 6 additions & 6 deletions src/CoreTests/registering_the_options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class registering_the_options
public void register_all_defaults()
{
var services = new ServiceCollection();
services.JasperFxDefaults();
services.AddJasperFx();
services.AddSingleton(theEnvironment);

theEnvironment.EnvironmentName.Returns("Production");
Expand All @@ -34,7 +34,7 @@ public void register_all_defaults()
public void pick_up_development_mode()
{
var services = new ServiceCollection();
services.JasperFxDefaults(x =>
services.AddJasperFx(x =>
{
x.Development.AutoCreate = AutoCreate.All;
});
Expand All @@ -56,7 +56,7 @@ public void pick_up_development_mode()
public void pick_up_development_mode_with_alternative_environment_name()
{
var services = new ServiceCollection();
services.JasperFxDefaults(x =>
services.AddJasperFx(x =>
{
x.Development.AutoCreate = AutoCreate.All;
x.DevelopmentEnvironmentName = "weird";
Expand All @@ -81,7 +81,7 @@ public void pick_up_development_mode_with_alternative_environment_name()
public void pick_up_production_mode()
{
var services = new ServiceCollection();
services.JasperFxDefaults(x =>
services.AddJasperFx(x =>
{
x.Development.AutoCreate = AutoCreate.All;

Expand Down Expand Up @@ -110,7 +110,7 @@ public void application_assembly_and_content_directory_from_StoreOptions()
using var host = Host.CreateDefaultBuilder(Array.Empty<string>())
.ConfigureServices(services =>
{
services.JasperFxDefaults(opts =>
services.AddJasperFx(opts =>
{
opts.SetApplicationProject(GetType().Assembly);
});
Expand All @@ -130,7 +130,7 @@ public void build_from_host()
var builder = Host.CreateApplicationBuilder();

// This would apply to both Marten, Wolverine, and future critters....
builder.Services.JasperFxDefaults(x =>
builder.Services.AddJasperFx(x =>
{
// This expands in importance to be the master "AutoCreate"
// over every resource at runtime and not just databases
Expand Down
28 changes: 2 additions & 26 deletions src/JasperFx/CommandLine/HostedCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,6 @@ namespace JasperFx.CommandLine;

public static class HostedCommandExtensions
{
/// <summary>
/// Register JasperFx commands and services with the application's service collection.
/// </summary>
/// <param name="services"></param>
public static void AddJasperFxCommands(this IServiceCollection services, Action<OaktonOptions>? options = null)
{
services.Configure(options);

services.TryAddScoped<ICommandCreator, DependencyInjectionCommandCreator>();

services.TryAddScoped<ICommandFactory>(ctx =>
{
var creator = ctx.GetRequiredService<ICommandCreator>();
var oaktonOptions = ctx.GetRequiredService<IOptions<OaktonOptions>>().Value;

var factory = new CommandFactory(creator);
factory.ApplyFactoryDefaults(Assembly.GetEntryAssembly());
oaktonOptions.Factory?.Invoke(factory);
return factory;
});

services.TryAddScoped<CommandExecutor>();
}

/// <summary>
/// Execute the extended Oakton command line support for your configured IHost.
/// This method would be called within the Task&lt;int&gt; Program.Main(string[] args) method
Expand All @@ -47,7 +23,7 @@ public static async Task<int> RunJasperFxCommands(this IHost host, string[] args
try
{
using var scope = host.Services.CreateScope();
var options = scope.ServiceProvider.GetRequiredService<IOptions<OaktonOptions>>().Value;
var options = scope.ServiceProvider.GetRequiredService<IOptions<JasperFxOptions>>().Value;
args = ApplyArgumentDefaults(args, options);

var executor = scope.ServiceProvider.GetRequiredService<CommandExecutor>();
Expand All @@ -74,7 +50,7 @@ public static async Task<int> RunJasperFxCommands(this IHost host, string[] args
}
}

private static string[] ApplyArgumentDefaults(string[] args, OaktonOptions options)
private static string[] ApplyArgumentDefaults(string[] args, JasperFxOptions options)
{
// Workaround for IISExpress / VS2019 erroneously putting crap arguments
args = args.FilterLauncherArgs();
Expand Down
8 changes: 0 additions & 8 deletions src/JasperFx/CommandLine/OaktonOptions.cs

This file was deleted.

43 changes: 15 additions & 28 deletions src/JasperFx/JasperFxOptions.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
using System.Reflection;
using JasperFx.CodeGeneration;
using JasperFx.CommandLine;
using JasperFx.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace JasperFx;

public static class JasperFxServiceCollectionExtensions
{
/// <summary>
/// Configure JasperFx and Critter Stack tool behavior for resource management at runtime
/// </summary>
/// <param name="services"></param>
/// <param name="configure">Optional configuration of the JasperFxDefaults for resource management</param>
/// <returns></returns>
public static IServiceCollection JasperFxDefaults(this IServiceCollection services, Action<JasperFxOptions>? configure = null)
{
var optionsBuilder = services.AddOptions<JasperFxOptions>();
if (configure != null)
{
optionsBuilder.Configure(configure!);
}

optionsBuilder.PostConfigure<IHostEnvironment>((o, e) => o.ReadHostEnvironment(e));
services.AddSingleton<JasperFxOptions>(s =>
{
return s.GetRequiredService<IOptions<JasperFxOptions>>().Value;
});

return services;
}
}

public class JasperFxOptions
{
public JasperFxOptions()
Expand Down Expand Up @@ -147,6 +120,20 @@ public void SetApplicationProject(Assembly assembly,

GeneratedCodeOutputPath = path.AppendPath("Internal", "Generated");
}


public string? OptionsFile { get; set; }

/// <summary>
/// Optional
/// </summary>
public Action<CommandFactory>? Factory { get; set; }

/// <summary>
/// Default command name to execute from just "dotnet run" or if you omit the
/// JasperFx command name. The default is "run"
/// </summary>
public string DefaultCommand { get; set; } = "run";
}

public class Profile
Expand Down
49 changes: 49 additions & 0 deletions src/JasperFx/JasperFxServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Reflection;
using JasperFx.CommandLine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace JasperFx;

public static class JasperFxServiceCollectionExtensions
{
/// <summary>
/// Configure JasperFx and Critter Stack tool behavior for resource management at runtime
/// </summary>
/// <param name="services"></param>
/// <param name="configure">Optional configuration of the JasperFxDefaults for resource management</param>
/// <returns></returns>
public static IServiceCollection AddJasperFx(this IServiceCollection services, Action<JasperFxOptions>? configure = null)
{
var optionsBuilder = services.AddOptions<JasperFxOptions>();
if (configure != null)
{
optionsBuilder.Configure(configure!);
}

optionsBuilder.PostConfigure<IHostEnvironment>((o, e) => o.ReadHostEnvironment(e));
services.AddSingleton<JasperFxOptions>(s =>
{
return s.GetRequiredService<IOptions<JasperFxOptions>>().Value;
});

services.TryAddScoped<ICommandCreator, DependencyInjectionCommandCreator>();

services.TryAddScoped<ICommandFactory>(ctx =>
{
var creator = ctx.GetRequiredService<ICommandCreator>();
var oaktonOptions = ctx.GetRequiredService<IOptions<JasperFxOptions>>().Value;

var factory = new CommandFactory(creator);
factory.ApplyFactoryDefaults(Assembly.GetEntryAssembly());
oaktonOptions.Factory?.Invoke(factory);
return factory;
});

services.TryAddScoped<CommandExecutor>();

return services;
}
}

0 comments on commit f257b9a

Please sign in to comment.