|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 8 | +using Microsoft.CodeAnalysis.Operations; |
| 9 | +using Microsoft.AspNetCore.Http.Generators.StaticRouteHandlerModel; |
| 10 | + |
| 11 | +namespace Microsoft.AspNetCore.Http.Generators; |
| 12 | + |
| 13 | +[Generator] |
| 14 | +public sealed class RequestDelegateGenerator : IIncrementalGenerator |
| 15 | +{ |
| 16 | + private static readonly string[] _knownMethods = |
| 17 | + { |
| 18 | + "MapGet", |
| 19 | + "MapPost", |
| 20 | + "MapPut", |
| 21 | + "MapDelete", |
| 22 | + "MapPatch", |
| 23 | + "Map", |
| 24 | + }; |
| 25 | + |
| 26 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 27 | + { |
| 28 | + var endpoints = context.SyntaxProvider.CreateSyntaxProvider( |
| 29 | + predicate: (node, _) => node is InvocationExpressionSyntax |
| 30 | + { |
| 31 | + Expression: MemberAccessExpressionSyntax |
| 32 | + { |
| 33 | + Name: IdentifierNameSyntax |
| 34 | + { |
| 35 | + Identifier: { ValueText: var method } |
| 36 | + } |
| 37 | + }, |
| 38 | + ArgumentList: { Arguments: { Count: 2 } args } |
| 39 | + } && _knownMethods.Contains(method), |
| 40 | + transform: (context, token) => |
| 41 | + { |
| 42 | + var operation = context.SemanticModel.GetOperation(context.Node, token) as IInvocationOperation; |
| 43 | + return StaticRouteHandlerModelParser.GetEndpointFromOperation(operation); |
| 44 | + }) |
| 45 | + .Where(endpoint => endpoint.Response.ResponseType == "string") |
| 46 | + .WithTrackingName("EndpointModel"); |
| 47 | + |
| 48 | + var thunks = endpoints.Select((endpoint, _) => $$""" |
| 49 | + [{{StaticRouteHandlerModelEmitter.EmitSourceKey(endpoint)}}] = ( |
| 50 | + (del, builder) => |
| 51 | + { |
| 52 | + builder.Metadata.Add(new SourceKey{{StaticRouteHandlerModelEmitter.EmitSourceKey(endpoint)}}); |
| 53 | + }, |
| 54 | + (del, builder) => |
| 55 | + { |
| 56 | + var handler = ({{StaticRouteHandlerModelEmitter.EmitHandlerDelegateType(endpoint)}})del; |
| 57 | + EndpointFilterDelegate? filteredInvocation = null; |
| 58 | +
|
| 59 | + if (builder.FilterFactories.Count > 0) |
| 60 | + { |
| 61 | + filteredInvocation = BuildFilterDelegate(ic => |
| 62 | + { |
| 63 | + if (ic.HttpContext.Response.StatusCode == 400) |
| 64 | + { |
| 65 | + return System.Threading.Tasks.ValueTask.FromResult<object?>(Results.Empty); |
| 66 | + } |
| 67 | + {{StaticRouteHandlerModelEmitter.EmitFilteredInvocation()}} |
| 68 | + }, |
| 69 | + builder, |
| 70 | + handler.Method); |
| 71 | + } |
| 72 | +
|
| 73 | + {{StaticRouteHandlerModelEmitter.EmitRequestHandler()}} |
| 74 | + {{StaticRouteHandlerModelEmitter.EmitFilteredRequestHandler()}} |
| 75 | +
|
| 76 | + return filteredInvocation is null ? RequestHandler : RequestHandlerFiltered; |
| 77 | + }), |
| 78 | +"""); |
| 79 | + |
| 80 | + var stronglyTypedEndpointDefinitions = endpoints.Select((endpoint, _) => $$""" |
| 81 | +{{RequestDelegateGeneratorSources.GeneratedCodeAttribute}} |
| 82 | +internal static Microsoft.AspNetCore.Builder.RouteHandlerBuilder {{endpoint.HttpMethod}}( |
| 83 | + this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, |
| 84 | + [System.Diagnostics.CodeAnalysis.StringSyntax("Route")] string pattern, |
| 85 | + {{StaticRouteHandlerModelEmitter.EmitHandlerDelegateType(endpoint)}} handler, |
| 86 | + [System.Runtime.CompilerServices.CallerFilePath] string filePath = "", |
| 87 | + [System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0) |
| 88 | + { |
| 89 | + return MapCore(endpoints, pattern, handler, GetVerb, filePath, lineNumber); |
| 90 | + } |
| 91 | +"""); |
| 92 | + |
| 93 | + var thunksAndEndpoints = thunks.Collect().Combine(stronglyTypedEndpointDefinitions.Collect()); |
| 94 | + |
| 95 | + context.RegisterSourceOutput(thunksAndEndpoints, (context, sources) => |
| 96 | + { |
| 97 | + var (thunks, endpoints) = sources; |
| 98 | + |
| 99 | + var endpointsCode = new StringBuilder(); |
| 100 | + var thunksCode = new StringBuilder(); |
| 101 | + foreach (var endpoint in endpoints) |
| 102 | + { |
| 103 | + endpointsCode.AppendLine(endpoint); |
| 104 | + } |
| 105 | + foreach (var thunk in thunks) |
| 106 | + { |
| 107 | + thunksCode.AppendLine(thunk); |
| 108 | + } |
| 109 | + |
| 110 | + var code = RequestDelegateGeneratorSources.GetGeneratedRouteBuilderExtensionsSource( |
| 111 | + genericThunks: string.Empty, |
| 112 | + thunks: thunksCode.ToString(), |
| 113 | + endpoints: endpointsCode.ToString()); |
| 114 | + context.AddSource("GeneratedRouteBuilderExtensions.g.cs", code); |
| 115 | + }); |
| 116 | + } |
| 117 | +} |
0 commit comments