diff --git a/src/Nullean.Argh.Generator/CliParserGenerator.Analysis.cs b/src/Nullean.Argh.Generator/CliParserGenerator.Analysis.cs index 08cf037..e7fd602 100644 --- a/src/Nullean.Argh.Generator/CliParserGenerator.Analysis.cs +++ b/src/Nullean.Argh.Generator/CliParserGenerator.Analysis.cs @@ -58,7 +58,7 @@ public sealed partial class CliParserGenerator case "UseMiddleware": // Inline delegate — diagnostic will be reported by TryBuildAppEmitModel (option 2 from plan). return new AIUseMiddleware(filePath, spanStart, new GlobalMiddlewareRegistration("", false)); - case "Map" when method.IsGenericMethod && method.TypeArguments.Length > 0: + case "Map" or "MapTransient" or "MapScoped" or "MapSingleton" when method.IsGenericMethod && method.TypeArguments.Length > 0: { if (method.TypeArguments[0] is not INamedTypeSymbol named || named.TypeKind == TypeKind.Error) return null; diff --git a/src/Nullean.Argh.Generator/CliParserGenerator.cs b/src/Nullean.Argh.Generator/CliParserGenerator.cs index d7e15b1..64c8f6f 100644 --- a/src/Nullean.Argh.Generator/CliParserGenerator.cs +++ b/src/Nullean.Argh.Generator/CliParserGenerator.cs @@ -51,6 +51,10 @@ private static bool IsTrackedArghInvocation(InvocationExpressionSyntax inv, Memb case "UseNamespaceOptions": case "UseMiddleware": return true; + case "MapTransient": + case "MapScoped": + case "MapSingleton": + return ma.Name is GenericNameSyntax gnl && gnl.TypeArgumentList.Arguments.Count > 0; case "Map": if (ma.Name is GenericNameSyntax gn && gn.TypeArgumentList.Arguments.Count > 0) return true; diff --git a/src/Nullean.Argh.Hosting/skills/nullean-argh-hosting/SKILL.md b/src/Nullean.Argh.Hosting/skills/nullean-argh-hosting/SKILL.md index 844219e..ce14cca 100644 --- a/src/Nullean.Argh.Hosting/skills/nullean-argh-hosting/SKILL.md +++ b/src/Nullean.Argh.Hosting/skills/nullean-argh-hosting/SKILL.md @@ -50,8 +50,7 @@ public sealed class DeployCommands(IClock clock) ## Gotchas -- Use `b.Map(ServiceLifetime.Scoped)`, not `MapScoped()`. `MapScoped`, `MapTransient` and `MapSingleton` - compile, but the source generator only reads `Map`, so their commands never exist (`unknown command`). +- `MapScoped()`, `MapTransient()` and `MapSingleton()` are shorthand for `Map(ServiceLifetime.X)`. - The process exits through `Environment.Exit` with the command's exit code once the command finishes. Code after `RunAsync()` does not run, and `StopAsync` is not called on hosted services. - Call `AddArgh` before other `AddHostedService` registrations. Hosted services registered earlier start on every diff --git a/tests/Nullean.Argh.Generator.Tests/HostingMapLifetimeTests.cs b/tests/Nullean.Argh.Generator.Tests/HostingMapLifetimeTests.cs new file mode 100644 index 0000000..a8ab821 --- /dev/null +++ b/tests/Nullean.Argh.Generator.Tests/HostingMapLifetimeTests.cs @@ -0,0 +1,68 @@ +using System.Text; +using AwesomeAssertions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Text; + +namespace Nullean.Argh.Generator.Tests; + +/// +/// MapTransient<T>, MapScoped<T> and MapSingleton<T> on the hosting builder register +/// commands just like Map<T> (https://github.com/nullean/argh/issues/127). +/// +public class HostingMapLifetimeTests +{ + // Stand-in for the hosting builder's methods: the generator keys on the method name, not on the Hosting assembly. + private static string Source(string call) => + $$""" + using Nullean.Argh; + using Nullean.Argh.Builder; + + namespace App + { + public class Commands + { + public void Frobnicate() { } + } + + public static class HostingLike + { + public static IArghBuilder MapTransient(this IArghBuilder b) where T : class => b.Map(); + public static IArghBuilder MapScoped(this IArghBuilder b) where T : class => b.Map(); + public static IArghBuilder MapSingleton(this IArghBuilder b) where T : class => b.Map(); + } + + public static class CliRegistration + { + public static void Configure(IArghRootBuilder app) => app.{{call}}(); + } + } + """; + + [Test] + [Arguments("Map")] + [Arguments("MapTransient")] + [Arguments("MapScoped")] + [Arguments("MapSingleton")] + public void Command_type_is_registered(string call) + { + var tpaPaths = ((string)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES")!).Split(Path.PathSeparator); + var references = tpaPaths.Select(p => (MetadataReference)MetadataReference.CreateFromFile(p)).ToList(); + references.Add(MetadataReference.CreateFromFile(typeof(Nullean.Argh.Builder.IArghBuilder).Assembly.Location)); + references.Add(MetadataReference.CreateFromFile(typeof(Nullean.Argh.ArghApp).Assembly.Location)); + + var tree = CSharpSyntaxTree.ParseText(SourceText.From(Source(call), Encoding.UTF8), path: "App.cs"); + var compilation = CSharpCompilation.Create( + "App", + [tree], + references, + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: NullableContextOptions.Enable) + ); + + var driver = CSharpGeneratorDriver.Create(new CliParserGenerator()); + var result = driver.RunGenerators(compilation).GetRunResult(); + var generated = string.Join("\n", result.GeneratedTrees.Select(t => t.ToString())); + + generated.Should().Contain("frobnicate"); + } +}