diff --git a/src/CommandLineTests/HostedCommandsTester.cs b/src/CommandLineTests/HostedCommandsTester.cs index 4843d09..becf653 100644 --- a/src/CommandLineTests/HostedCommandsTester.cs +++ b/src/CommandLineTests/HostedCommandsTester.cs @@ -1,4 +1,5 @@ -using JasperFx.CommandLine; +using JasperFx; +using JasperFx.CommandLine; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -13,7 +14,7 @@ public async Task CanInjectServicesIntoCommands() .ConfigureServices(services => { services.AddScoped(); - services.AddJasperFxCommands(options => + services.AddJasperFx(options => { options.Factory = factory => { diff --git a/src/CoreTests/registering_the_options.cs b/src/CoreTests/registering_the_options.cs index f047561..2eb0034 100644 --- a/src/CoreTests/registering_the_options.cs +++ b/src/CoreTests/registering_the_options.cs @@ -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"); @@ -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; }); @@ -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"; @@ -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; @@ -110,7 +110,7 @@ public void application_assembly_and_content_directory_from_StoreOptions() using var host = Host.CreateDefaultBuilder(Array.Empty()) .ConfigureServices(services => { - services.JasperFxDefaults(opts => + services.AddJasperFx(opts => { opts.SetApplicationProject(GetType().Assembly); }); @@ -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 diff --git a/src/JasperFx/CommandLine/HostedCommandExtensions.cs b/src/JasperFx/CommandLine/HostedCommandExtensions.cs index ddcb1db..9e5bb98 100644 --- a/src/JasperFx/CommandLine/HostedCommandExtensions.cs +++ b/src/JasperFx/CommandLine/HostedCommandExtensions.cs @@ -10,30 +10,6 @@ namespace JasperFx.CommandLine; public static class HostedCommandExtensions { - /// - /// Register JasperFx commands and services with the application's service collection. - /// - /// - public static void AddJasperFxCommands(this IServiceCollection services, Action? options = null) - { - services.Configure(options); - - services.TryAddScoped(); - - services.TryAddScoped(ctx => - { - var creator = ctx.GetRequiredService(); - var oaktonOptions = ctx.GetRequiredService>().Value; - - var factory = new CommandFactory(creator); - factory.ApplyFactoryDefaults(Assembly.GetEntryAssembly()); - oaktonOptions.Factory?.Invoke(factory); - return factory; - }); - - services.TryAddScoped(); - } - /// /// Execute the extended Oakton command line support for your configured IHost. /// This method would be called within the Task<int> Program.Main(string[] args) method @@ -47,7 +23,7 @@ public static async Task RunJasperFxCommands(this IHost host, string[] args try { using var scope = host.Services.CreateScope(); - var options = scope.ServiceProvider.GetRequiredService>().Value; + var options = scope.ServiceProvider.GetRequiredService>().Value; args = ApplyArgumentDefaults(args, options); var executor = scope.ServiceProvider.GetRequiredService(); @@ -74,7 +50,7 @@ public static async Task 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(); diff --git a/src/JasperFx/CommandLine/OaktonOptions.cs b/src/JasperFx/CommandLine/OaktonOptions.cs deleted file mode 100644 index 425a76a..0000000 --- a/src/JasperFx/CommandLine/OaktonOptions.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace JasperFx.CommandLine; - -public class OaktonOptions -{ - public string OptionsFile { get; set; } - public Action Factory { get; set; } - public string DefaultCommand { get; set; } = "run"; -} \ No newline at end of file diff --git a/src/JasperFx/JasperFxOptions.cs b/src/JasperFx/JasperFxOptions.cs index eb43118..97b2222 100644 --- a/src/JasperFx/JasperFxOptions.cs +++ b/src/JasperFx/JasperFxOptions.cs @@ -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 -{ - /// - /// Configure JasperFx and Critter Stack tool behavior for resource management at runtime - /// - /// - /// Optional configuration of the JasperFxDefaults for resource management - /// - public static IServiceCollection JasperFxDefaults(this IServiceCollection services, Action? configure = null) - { - var optionsBuilder = services.AddOptions(); - if (configure != null) - { - optionsBuilder.Configure(configure!); - } - - optionsBuilder.PostConfigure((o, e) => o.ReadHostEnvironment(e)); - services.AddSingleton(s => - { - return s.GetRequiredService>().Value; - }); - - return services; - } -} - public class JasperFxOptions { public JasperFxOptions() @@ -147,6 +120,20 @@ public void SetApplicationProject(Assembly assembly, GeneratedCodeOutputPath = path.AppendPath("Internal", "Generated"); } + + + public string? OptionsFile { get; set; } + + /// + /// Optional + /// + public Action? Factory { get; set; } + + /// + /// Default command name to execute from just "dotnet run" or if you omit the + /// JasperFx command name. The default is "run" + /// + public string DefaultCommand { get; set; } = "run"; } public class Profile diff --git a/src/JasperFx/JasperFxServiceCollectionExtensions.cs b/src/JasperFx/JasperFxServiceCollectionExtensions.cs new file mode 100644 index 0000000..1050275 --- /dev/null +++ b/src/JasperFx/JasperFxServiceCollectionExtensions.cs @@ -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 +{ + /// + /// Configure JasperFx and Critter Stack tool behavior for resource management at runtime + /// + /// + /// Optional configuration of the JasperFxDefaults for resource management + /// + public static IServiceCollection AddJasperFx(this IServiceCollection services, Action? configure = null) + { + var optionsBuilder = services.AddOptions(); + if (configure != null) + { + optionsBuilder.Configure(configure!); + } + + optionsBuilder.PostConfigure((o, e) => o.ReadHostEnvironment(e)); + services.AddSingleton(s => + { + return s.GetRequiredService>().Value; + }); + + services.TryAddScoped(); + + services.TryAddScoped(ctx => + { + var creator = ctx.GetRequiredService(); + var oaktonOptions = ctx.GetRequiredService>().Value; + + var factory = new CommandFactory(creator); + factory.ApplyFactoryDefaults(Assembly.GetEntryAssembly()); + oaktonOptions.Factory?.Invoke(factory); + return factory; + }); + + services.TryAddScoped(); + + return services; + } +} \ No newline at end of file