From cfad2b2404c588e7cc63705c2959b9818acddccb Mon Sep 17 00:00:00 2001 From: Stuart Turner Date: Fri, 5 Jan 2024 16:32:07 -0600 Subject: [PATCH] misc in-progress work --- .../Generators/ImmediateHandlersGenerator.cs | 60 +++++++++++++++---- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs b/src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs index 36d11f85..1615d28e 100644 --- a/src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs +++ b/src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs @@ -60,14 +60,20 @@ public void Initialize(IncrementalGeneratorInitializationContext context) (spc, node) => RenderHandler(spc, node.Left.Left, node.Left.Right, node.Right, template) ); + var hasMsDi = context + .MetadataReferencesProvider + .Collect() + .Select((refs, _) => refs.Any(r => (r.Display ?? "").Contains("Microsoft.Extensions.DependencyInjection.Abstractions"))); + var registrationNodes = handlers - .Select((h, _) => h.FullTypeName) + .Select((h, _) => h?.FullTypeName) .Collect() - .Combine(behaviors); + .Combine(behaviors) + .Combine(hasMsDi); context.RegisterSourceOutput( registrationNodes, - RenderServiceCollectionExtension + (spc, node) => RenderServiceCollectionExtension(spc, node.Left.Left, node.Left.Right, node.Right) ); } @@ -77,12 +83,12 @@ private RenderMode TransformRenderMode(GeneratorAttributeSyntaxContext context, if (attr.ConstructorArguments.Length > 0) { var ca = attr.ConstructorArguments[0]; - return (RenderMode?)(int?)ca.Value ?? RenderMode.Normal; + return (RenderMode?)(int?)ca.Value ?? RenderMode.None; } else { var pa = attr.NamedArguments[0]; - return (RenderMode?)(int?)pa.Value.Value ?? RenderMode.Normal; + return (RenderMode?)(int?)pa.Value.Value ?? RenderMode.None; } } @@ -155,21 +161,40 @@ CancellationToken cancellationToken }) .ToArray(); + cancellationToken.ThrowIfCancellationRequested(); return ImmutableArray.Create(behaviors); } - private static void RenderServiceCollectionExtension(SourceProductionContext context, (ImmutableArray handlers, ImmutableArray behaviors) node) + private static void RenderServiceCollectionExtension(SourceProductionContext context, ImmutableArray handlers, ImmutableArray behaviors, bool hasDi) { + context.CancellationToken.ThrowIfCancellationRequested(); + + if (!hasDi) + return; + + if (handlers.Any(h => h is null)) + return; + + if (behaviors.Any(b => b is null)) + return; + + var cancellationToken = context.CancellationToken; + cancellationToken.ThrowIfCancellationRequested(); + var template = GetTemplate("ServiceCollectionExtensions"); + cancellationToken.ThrowIfCancellationRequested(); + var source = template.Render(new { - Handlers = node.handlers, - Behaviors = node.behaviors, + Handlers = handlers, + Behaviors = behaviors, }); + cancellationToken.ThrowIfCancellationRequested(); + context.AddSource("Immediate.Handlers.ServiceCollectionExtensions.cs", source); } - private static Handler TransformHandler( + private static Handler? TransformHandler( GeneratorAttributeSyntaxContext context, CancellationToken cancellationToken ) @@ -179,12 +204,27 @@ CancellationToken cancellationToken private static void RenderHandler( SourceProductionContext context, - Handler handler, + Handler? handler, ImmutableArray behaviors, ImmutableArray renderModes, Template template ) { + if (handler == null) + return; + + if (behaviors.Any(b => b is null)) + return; + + if (renderModes.Length > 1) + return; + + var cancellationToken = context.CancellationToken; + cancellationToken.ThrowIfCancellationRequested(); + + var renderMode = renderModes.Length == 0 ? RenderMode.Normal : renderModes[0]; + if (renderMode is not RenderMode.Normal) + return; }