Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Nullean.Argh.Generator/CliParserGenerator.Analysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Nullean.Argh.Generator/CliParserGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public sealed class DeployCommands(IClock clock)

## Gotchas

- Use `b.Map<T>(ServiceLifetime.Scoped)`, not `MapScoped<T>()`. `MapScoped`, `MapTransient` and `MapSingleton`
compile, but the source generator only reads `Map<T>`, so their commands never exist (`unknown command`).
- `MapScoped<T>()`, `MapTransient<T>()` and `MapSingleton<T>()` are shorthand for `Map<T>(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
Expand Down
68 changes: 68 additions & 0 deletions tests/Nullean.Argh.Generator.Tests/HostingMapLifetimeTests.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// <c>MapTransient&lt;T&gt;</c>, <c>MapScoped&lt;T&gt;</c> and <c>MapSingleton&lt;T&gt;</c> on the hosting builder register
/// commands just like <c>Map&lt;T&gt;</c> (https://github.com/nullean/argh/issues/127).
/// </summary>
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<T>(this IArghBuilder b) where T : class => b.Map<T>();
public static IArghBuilder MapScoped<T>(this IArghBuilder b) where T : class => b.Map<T>();
public static IArghBuilder MapSingleton<T>(this IArghBuilder b) where T : class => b.Map<T>();
}

public static class CliRegistration
{
public static void Configure(IArghRootBuilder app) => app.{{call}}<Commands>();
}
}
""";

[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");
}
}