Skip to content

Commit

Permalink
Update RDG to use interceptors feature
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia committed May 19, 2023
1 parent 4be2661 commit 709c0d8
Show file tree
Hide file tree
Showing 64 changed files with 6,654 additions and 6,589 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>Default</AnalysisMode>
<RestoreAdditionalProjectSources>/Users/captainsafia/repos/roslyn/artifacts/packages/Release/Shipping</RestoreAdditionalProjectSources>
</PropertyGroup>

<!-- Defines project type conventions. -->
Expand Down
20 changes: 11 additions & 9 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@
<MicrosoftEntityFrameworkCoreVersion>8.0.0-preview.5.23268.2</MicrosoftEntityFrameworkCoreVersion>
<MicrosoftEntityFrameworkCoreDesignVersion>8.0.0-preview.5.23268.2</MicrosoftEntityFrameworkCoreDesignVersion>
<!-- Packages from dotnet/roslyn -->
<MicrosoftCodeAnalysisCommonVersion>4.4.0-4.22520.2</MicrosoftCodeAnalysisCommonVersion>
<MicrosoftCodeAnalysisExternalAccessAspNetCoreVersion>4.4.0-4.22520.2</MicrosoftCodeAnalysisExternalAccessAspNetCoreVersion>
<MicrosoftCodeAnalysisCSharpVersion>4.4.0-4.22520.2</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.4.0-4.22520.2</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisCommonVersion>4.7.0-dev</MicrosoftCodeAnalysisCommonVersion>
<MicrosoftCodeAnalysisExternalAccessAspNetCoreVersion>4.7.0-dev</MicrosoftCodeAnalysisExternalAccessAspNetCoreVersion>
<MicrosoftCodeAnalysisCSharpVersion>4.7.0-dev</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.7.0-dev</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<!-- Packages from NuGet/Nuget.client -->
<!-- If you update these versions, make sure to also update https://github.com/dotnet/aspnetcore/blob/main/eng/SourceBuildPrebuiltBaseline.xml -->
<NuGetPackagingVersion>6.2.2</NuGetPackagingVersion>
Expand Down Expand Up @@ -231,11 +231,13 @@
<Analyzer_MicrosoftCodeAnalysisCSharpWorkspacesVersion>3.3.1</Analyzer_MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<!-- Pin the version of the M.CA dependencies that we utilize with a cutom version property $(MicrosoftCodeAnalysisVersion_LatestVS) to avoid automatically
consuming the newest version of the packages when using the $(MicrosoftCodeAnalysisCSharpVersion) properties in source-build. -->
<MicrosoftCodeAnalysisVersion_LatestVS>4.5.0</MicrosoftCodeAnalysisVersion_LatestVS>
<MicrosoftCodeAnalysisExternalAccessAspNetCoreVersion>4.4.0-4.22520.2</MicrosoftCodeAnalysisExternalAccessAspNetCoreVersion>
<MicrosoftCodeAnalysisCommonVersion>4.4.0-4.22520.2</MicrosoftCodeAnalysisCommonVersion>
<MicrosoftCodeAnalysisCSharpVersion>4.4.0-4.22520.2</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.4.0-4.22520.2</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisVersion_LatestVS>4.7.0-dev</MicrosoftCodeAnalysisVersion_LatestVS>
<MicrosoftCodeAnalysisExternalAccessAspNetCoreVersion>4.7.0-dev</MicrosoftCodeAnalysisExternalAccessAspNetCoreVersion>
<MicrosoftCodeAnalysisCommonVersion>4.7.0-dev</MicrosoftCodeAnalysisCommonVersion>
<MicrosoftCodeAnalysisCSharpVersion>4.7.0-dev</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.7.0-dev</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<UsingToolMicrosoftNetCompilers>true</UsingToolMicrosoftNetCompilers>
<MicrosoftNetCompilersToolsetVersion>4.7.0-dev</MicrosoftNetCompilersToolsetVersion>
<MicrosoftCodeAnalysisPublicApiAnalyzersVersion>3.3.3</MicrosoftCodeAnalysisPublicApiAnalyzersVersion>
<MicrosoftCodeAnalysisCSharpAnalyzerTestingXUnitVersion>1.1.2-beta1.22531.1</MicrosoftCodeAnalysisCSharpAnalyzerTestingXUnitVersion>
<MicrosoftCodeAnalysisCSharpCodeFixTestingXUnitVersion>1.1.2-beta1.22531.1</MicrosoftCodeAnalysisCSharpCodeFixTestingXUnitVersion>
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Http.Extensions/gen/GeneratorSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator;
internal class GeneratorSteps
{
internal const string EndpointModelStep = nameof(EndpointModelStep);
internal const string EndpointsWithoutDiagnosicsStep = nameof(EndpointsWithoutDiagnosicsStep);
internal const string EndpointsWithoutDiagnosticsStep = nameof(EndpointsWithoutDiagnosticsStep);
internal const string EndpointsInterceptorsStep = nameof(EndpointsInterceptorsStep);
internal const string EndpointsHelperMethodsStep = nameof(EndpointsHelperMethodsStep);
internal const string EndpointsHelperTypesStep = nameof(EndpointsHelperTypesStep);
}
224 changes: 105 additions & 119 deletions src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs

Large diffs are not rendered by default.

98 changes: 35 additions & 63 deletions src/Http/Http.Extensions/gen/RequestDelegateGeneratorSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
}
""";

public static string GetGeneratedRouteBuilderExtensionsSource(string genericThunks, string thunks, string endpoints, string helperMethods, string helperTypes) => $$"""
public static string GetGeneratedRouteBuilderExtensionsSource(string endpoints, string helperMethods, string helperTypes) => $$"""
{{SourceHeader}}

namespace Microsoft.AspNetCore.Builder
Expand All @@ -513,8 +513,18 @@ public SourceKey(string path, int line)
Line = line;
}
}
}

{{GetEndpoints(endpoints)}}
namespace System.Runtime.CompilerServices
{
{{GeneratedCodeAttribute}}
[AttributeUsage(AttributeTargets.Method)]
file sealed class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(string filePath, int line, int column)
{
}
}
}

namespace Microsoft.AspNetCore.Http.Generated
Expand Down Expand Up @@ -549,8 +559,29 @@ namespace Microsoft.AspNetCore.Http.Generated

file static class GeneratedRouteBuilderExtensionsCore
{
{{GetGenericThunks(genericThunks)}}
{{GetThunks(thunks)}}
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

{{endpoints}}

internal static RouteHandlerBuilder MapCore(
this IEndpointRouteBuilder routes,
string pattern,
Delegate handler,
IEnumerable<string>? httpMethods,
MetadataPopulator populateMetadata,
RequestDelegateFactoryFunc createRequestDelegate)
{
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate);
}

private static T Cast<T>(Delegate d, T _) where T : Delegate
{
return (T)d;
}

private static EndpointFilterDelegate BuildFilterDelegate(EndpointFilterDelegate filteredInvocation, EndpointBuilder builder, MethodInfo mi)
{
Expand Down Expand Up @@ -595,63 +626,4 @@ private static Task ExecuteObjectResult(object? obj, HttpContext httpContext)
{{LogOrThrowExceptionHelperClass}}
}
""";
private static string GetGenericThunks(string genericThunks) => genericThunks != string.Empty ? $$"""
private static class GenericThunks<T>
{
public static readonly Dictionary<(string, int), (MetadataPopulator, RequestDelegateFactoryFunc)> map = new()
{
{{genericThunks}}
};
}

internal static RouteHandlerBuilder MapCore<T>(
this IEndpointRouteBuilder routes,
string pattern,
Delegate handler,
IEnumerable<string> httpMethods,
string filePath,
int lineNumber)
{
var (populateMetadata, createRequestDelegate) = GenericThunks<T>.map[(filePath, lineNumber)];
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate);
}
""" : string.Empty;

private static string GetThunks(string thunks) => thunks != string.Empty ? $$"""
private static readonly Dictionary<(string, int), (MetadataPopulator, RequestDelegateFactoryFunc)> map = new()
{
{{thunks}}
};

internal static RouteHandlerBuilder MapCore(
this IEndpointRouteBuilder routes,
string pattern,
Delegate handler,
IEnumerable<string>? httpMethods,
string filePath,
int lineNumber)
{
var (populateMetadata, createRequestDelegate) = map[(filePath, lineNumber)];
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate);
}
""" : string.Empty;
private static string GetEndpoints(string endpoints) => endpoints != string.Empty ? $$"""
// This class needs to be internal so that the compiled application
// has access to the strongly-typed endpoint definitions that are
// generated by the compiler so that they will be favored by
// overload resolution and opt the runtime in to the code generated
// implementation produced here.
{{GeneratedCodeAttribute}}
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };
{{endpoints}}
}
""" : string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.CodeAnalysis;
namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel.Emitters;

namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandler.Emitters;

internal static class EmitterConstants
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel.Emitters;

namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandler.Emitters;

internal sealed class EmitterContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Analyzers.Infrastructure;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandler.Model;
using Microsoft.CodeAnalysis;

namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel;
namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandler.Emitters;

internal static class EmitterExtensions
{
Expand Down
Loading

0 comments on commit 709c0d8

Please sign in to comment.