Skip to content
Merged
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 Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.1.0-beta.6</Version>
<Version>2.2.0-beta.1</Version>
<!-- SPDX license identifier for MIT -->
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<!-- Other useful metadata -->
Expand Down
2 changes: 2 additions & 0 deletions examples/MinimalLambda.Example.ClassMiddleware/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

var lambda = builder.Build();

lambda.UseMiddleware<Middleware>();

lambda.MapHandler(
([FromEvent] Request request) => new Response($"Hello {request.Name}!", DateTime.UtcNow)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using MinimalLambda.SourceGenerators.Models;

namespace MinimalLambda.SourceGenerators.Emitters;

internal static class InvocationHandlerEmitter
{
private const string LambdaHostMapHandlerExtensionsTemplateFile =
"Templates/MapHandler.scriban";

internal static void Emit(
SourceProductionContext context,
ImmutableArray<MapHandlerMethodInfo> infos
)
{
if (infos.Length == 0)
return;

var code = TemplateHelper.Render(
LambdaHostMapHandlerExtensionsTemplateFile,
new { TemplateHelper.GeneratedCodeAttribute, MapHandlerCalls = infos }
);

context.AddSource("MinimalLambda.InvocationHandlers.g.cs", code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using MinimalLambda.SourceGenerators.Models;

namespace MinimalLambda.SourceGenerators.Emitters;

internal static class LifecycleHandlerEmitter
{
private const string GenericHandlerTemplateFile = "Templates/GenericHandler.scriban";

internal static void Emit(
SourceProductionContext context,
ImmutableArray<LifecycleMethodInfo> infos
)
{
if (infos.Length == 0)
return;

var name = infos.First().MethodType;

var code = TemplateHelper.Render(
GenericHandlerTemplateFile,
new
{
TemplateHelper.GeneratedCodeAttribute,
Name = name,
Calls = infos,
}
);

context.AddSource($"MinimalLambda.{name}Handlers.g.cs", code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using MinimalLambda.SourceGenerators.Models;

namespace MinimalLambda.SourceGenerators.Emitters;

internal static class MiddlewareClassEmitter
{
private const string UseMiddlewareTTemplateFile = "Templates/UseMiddlewareT.scriban";

internal static void Emit(
SourceProductionContext context,
ImmutableArray<UseMiddlewareTInfo> infos
)
{
if (infos.Length == 0)
return;

var code = TemplateHelper.Render(
UseMiddlewareTTemplateFile,
new { TemplateHelper.GeneratedCodeAttribute, Calls = infos }
);

context.AddSource("MinimalLambda.UseMiddleware.g.cs", code);
}
}
120 changes: 0 additions & 120 deletions src/MinimalLambda.SourceGenerators/Emitters/MinimalLambdaEmitter.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/MinimalLambda.SourceGenerators/Emitters/TemplateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ namespace MinimalLambda.SourceGenerators.Emitters;
/// </summary>
internal static class TemplateHelper
{
internal static readonly Lazy<string> GeneratedCodeAttribute = new(() =>
{
var assembly = Assembly.GetExecutingAssembly();
var generatorName = assembly.GetName().Name;
var generatorVersion = assembly.GetName().Version;

return $"""[global::System.CodeDom.Compiler.GeneratedCode("{generatorName}", "{generatorVersion}")]""";
});

private static readonly ConcurrentDictionary<string, Template> Cache = new();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,5 @@ internal static class FunctionalExtensions
extension<T>(T source)
{
public TResult Map<TResult>(Func<T, TResult> func) => func(source);

public T Tap(Action<T> action)
{
action(source);
return source;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Linq;
using MinimalLambda.SourceGenerators.Models;

namespace Microsoft.CodeAnalysis;

internal static class IncrementalValueProviderExtensions
Expand All @@ -8,4 +11,15 @@ internal static class IncrementalValueProviderExtensions
public IncrementalValuesProvider<T> WhereNotNull() =>
valueProviders.Where(static v => v is not null).Select(static (v, _) => v!);
}

extension<T>(IncrementalValuesProvider<T> valueProviders)
where T : IMethodInfo
{
public IncrementalValuesProvider<T> WhereNoErrors() =>
valueProviders.Where(static c =>
c.DiagnosticInfos.All(d =>
d.DiagnosticDescriptor.DefaultSeverity != DiagnosticSeverity.Error
)
);
}
}
21 changes: 0 additions & 21 deletions src/MinimalLambda.SourceGenerators/GeneratorConstants.cs

This file was deleted.

5 changes: 1 addition & 4 deletions src/MinimalLambda.SourceGenerators/GeneratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ internal class GeneratorContext
internal WellKnownTypes.WellKnownTypes WellKnownTypes { get; }
internal CancellationToken CancellationToken { get; }
internal SemanticModel SemanticModel { get; }
internal GeneratorSyntaxContext GeneratorAttributeSyntaxContext { get; }

internal SyntaxNode Node { get; }

internal GeneratorContext(GeneratorSyntaxContext context, CancellationToken cancellationToken)
{
GeneratorAttributeSyntaxContext = context;
Node = context.Node;
SemanticModel = GeneratorAttributeSyntaxContext.SemanticModel;
SemanticModel = context.SemanticModel;
CancellationToken = cancellationToken;
WellKnownTypes = SourceGenerators.WellKnownTypes.WellKnownTypes.GetOrCreate(
context.SemanticModel.Compilation
Expand Down
64 changes: 27 additions & 37 deletions src/MinimalLambda.SourceGenerators/MinimalLambdaGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using MinimalLambda.SourceGenerators.Emitters;
Expand Down Expand Up @@ -49,48 +48,39 @@ is CSharpCompilation
)
.WhereNotNull();

var registrationCallsCollected = registrationCalls.Collect();
var useMiddlewareTCallsCollected = useMiddlewareTCalls.Collect();
var invocationHandlerCalls = registrationCalls
.WhereNoErrors()
.Where(static c => c is MapHandlerMethodInfo)
.Select(static (c, _) => (MapHandlerMethodInfo)c)
.Collect();

// combine the compilation and map handler calls
var combined = registrationCallsCollected
.Combine(useMiddlewareTCallsCollected)
.Select(
CompilationInfo? (t, _) =>
{
var (handlerInfos, useMiddlewareInfo) = t;
var onInitHandlerCalls = registrationCalls
.WhereNoErrors()
.Where(static c => c is LifecycleMethodInfo { MethodType: MethodType.OnInit })
.Select(static (c, _) => (LifecycleMethodInfo)c)
.Collect();

if (handlerInfos.Length == 0 && useMiddlewareInfo.Length == 0)
return null;
var onShutdownHandlerCalls = registrationCalls
.WhereNoErrors()
.Where(static c => c is LifecycleMethodInfo { MethodType: MethodType.OnShutdown })
.Select(static (c, _) => (LifecycleMethodInfo)c)
.Collect();

return new CompilationInfo
{
MapHandlerInvocationInfos = handlerInfos
.OfType<MapHandlerMethodInfo>()
.ToEquatableArray(),
OnShutdownInvocationInfos = handlerInfos
.OfType<LifecycleMethodInfo>()
.Where(h => h.MethodType == MethodType.OnShutdown)
.ToEquatableArray(),
OnInitInvocationInfos = handlerInfos
.OfType<LifecycleMethodInfo>()
.Where(h => h.MethodType == MethodType.OnInit)
.ToEquatableArray(),
UseMiddlewareTInfos = useMiddlewareInfo.ToEquatableArray(),
};
}
);
var middlewareTCallsCollected = useMiddlewareTCalls.WhereNoErrors().Collect();

// Generate source when calls are found
context.RegisterSourceOutput(
combined,
(productionContext, info) =>
{
if (info is null)
return;
registrationCalls,
(ctx, call) => call.DiagnosticInfos.ForEach(d => d.ReportDiagnostic(ctx))
);

MinimalLambdaEmitter.Generate(productionContext, info.Value);
}
context.RegisterSourceOutput(
useMiddlewareTCalls,
(ctx, call) => call.DiagnosticInfos.ForEach(d => d.ReportDiagnostic(ctx))
);

context.RegisterSourceOutput(invocationHandlerCalls, InvocationHandlerEmitter.Emit);
context.RegisterSourceOutput(onInitHandlerCalls, LifecycleHandlerEmitter.Emit);
context.RegisterSourceOutput(onShutdownHandlerCalls, LifecycleHandlerEmitter.Emit);
context.RegisterSourceOutput(middlewareTCallsCollected, MiddlewareClassEmitter.Emit);
}
}
Loading
Loading