Skip to content

Commit

Permalink
misc in-progress work
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Jan 5, 2024
1 parent bc318d2 commit cfad2b2
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -155,21 +161,40 @@ CancellationToken cancellationToken
})
.ToArray();

cancellationToken.ThrowIfCancellationRequested();
return ImmutableArray.Create(behaviors);
}

private static void RenderServiceCollectionExtension(SourceProductionContext context, (ImmutableArray<string> handlers, ImmutableArray<Behavior?> behaviors) node)
private static void RenderServiceCollectionExtension(SourceProductionContext context, ImmutableArray<string?> handlers, ImmutableArray<Behavior?> 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
)
Expand All @@ -179,12 +204,27 @@ CancellationToken cancellationToken

private static void RenderHandler(
SourceProductionContext context,
Handler handler,
Handler? handler,
ImmutableArray<Behavior?> behaviors,
ImmutableArray<RenderMode> 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;

}

Expand Down

0 comments on commit cfad2b2

Please sign in to comment.