Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement source generator #1

Merged
merged 1 commit into from
Apr 24, 2024
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
36 changes: 36 additions & 0 deletions Server.Core.SourceGenerators/RequestDelegateSourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.CodeAnalysis;

namespace Server.Core.SourceGenerators;

[Generator(LanguageNames.CSharp)]
public class RequestDelegateSourceGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
}

public void Execute(GeneratorExecutionContext context)
{
static string indent(int depth = 1) => new(' ', 4 * depth);

context.AddSource($"Generated.RequestDelegates.g.cs",
$$"""
// <auto-generated/>
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace {{context.Compilation.AssemblyName}};

public static partial class Generated
{
public static (string Route, Delegate Delegate)[] RequestDelegates =
[
{{string.Join("\n" + indent(2), Shared.Constants.Endpoints.Select(e =>
$"(\"{e.Route}\", ([FromQuery] {e.Type} q, HttpResponse r) => r.WriteAsJsonAsync(q)),"))}}
];
}
""");
}
}
19 changes: 19 additions & 0 deletions Server.Core.SourceGenerators/Server.Core.SourceGenerators.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" OutputItemType="Analyzer" />
</ItemGroup>

</Project>
9 changes: 2 additions & 7 deletions Server.Core/QueryBindingEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ namespace Server.Core;

public class QueryBindingEvaluator : IDisposable
{
private static Delegate CreateEndpointDelegate<T>() => ([FromQuery] T q, HttpResponse r) => r.WriteAsJsonAsync(q);
private static readonly MethodInfo createEndpointDelegateMethod = typeof(QueryBindingEvaluator)
.GetMethod(nameof(CreateEndpointDelegate), 1, BindingFlags.NonPublic | BindingFlags.Static, null, [], null)
?? throw new InvalidOperationException($"Could not find {nameof(CreateEndpointDelegate)} method.");

private readonly TestServer _minimalApiTestServer;
private readonly HttpClient _minimalApiTestServerClient;

Expand Down Expand Up @@ -46,8 +41,8 @@ private static TestServer CreateMinimalApiTestServer()
app.UseRouting();
app.UseEndpoints(endpoints =>
{
foreach (var endpoint in Constants.Endpoints)
endpoints.MapGet(endpoint.Route, (Delegate)createEndpointDelegateMethod.MakeGenericMethod(endpoint.ParamType).Invoke(null, null)!);
foreach (var (route, @delegate) in Generated.RequestDelegates)
endpoints.MapGet(route, @delegate);
});
});

Expand Down
4 changes: 4 additions & 0 deletions Server.Core/Server.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,6 +17,9 @@

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\Server.Core.SourceGenerators\Server.Core.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions dotnet-qs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Core.Tests", "Server.Core.Tests\Server.Core.Tests.csproj", "{5A856409-9FA3-4C23-B65B-3BCA7275EBC1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Core.SourceGenerators", "Server.Core.SourceGenerators\Server.Core.SourceGenerators.csproj", "{2B7D08BF-781C-421B-9C9A-E5B03F606A1A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -42,5 +44,9 @@ Global
{5A856409-9FA3-4C23-B65B-3BCA7275EBC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A856409-9FA3-4C23-B65B-3BCA7275EBC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A856409-9FA3-4C23-B65B-3BCA7275EBC1}.Release|Any CPU.Build.0 = Release|Any CPU
{2B7D08BF-781C-421B-9C9A-E5B03F606A1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2B7D08BF-781C-421B-9C9A-E5B03F606A1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B7D08BF-781C-421B-9C9A-E5B03F606A1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B7D08BF-781C-421B-9C9A-E5B03F606A1A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal