Skip to content

Commit 05ca5ee

Browse files
authored
Merge pull request #21 from Cabazure/aot
Add support for AOT
2 parents 05e9b82 + 495f069 commit 05ca5ee

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

src/Cabazure.Client/ClientInitializationGenerator.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1414
context.RegisterPostInitializationOutput(c =>
1515
c.AddSource(
1616
"ClientInitialization.g.cs",
17-
"""
17+
$$"""
1818
// <auto-generated/>
1919
#nullable enable
2020
using System;
@@ -51,15 +51,21 @@ internal static partial IServiceCollection AddCabazureClient(
5151
.Where(e => e != null)
5252
.Collect();
5353

54+
var aotSupport = context.CompilationProvider
55+
.Select((options, _) => options.GetTypeByMetadataName(
56+
TypeConstants.DynamicallyAccessedMembersAttribute) is { });
57+
5458
context.RegisterSourceOutput(
55-
endpoints,
59+
endpoints.Combine(aotSupport),
5660
GenerateInitialization);
5761
}
5862

5963
public static void GenerateInitialization(
60-
SourceProductionContext context,
61-
ImmutableArray<EndpointReferenceDescriptor> endpoints)
64+
SourceProductionContext context,
65+
(ImmutableArray<EndpointReferenceDescriptor>, bool) args)
6266
{
67+
var (endpoints, aotSupport) = args;
68+
6369
var source = new StringBuilder();
6470
source.AppendLine("// <auto-generated/>");
6571
source.AppendLine("#nullable enable");
@@ -68,6 +74,7 @@ public static void GenerateInitialization(
6874
.Select(e => e.Namespace)
6975
.OfType<string>()
7076
.Append("System")
77+
.Append("System.Diagnostics.CodeAnalysis")
7178
.Append("System.Net.Http")
7279
.Append("System.Text.Json")
7380
.Append("System.Collections.Generic")
@@ -90,12 +97,16 @@ public static void GenerateInitialization(
9097
source.AppendLine();
9198
}
9299

100+
var aotAttribute = aotSupport
101+
? "[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] "
102+
: string.Empty;
103+
93104
source.AppendLine($$"""
94105
namespace Microsoft.Extensions.DependencyInjection
95106
{
96107
internal static partial class ClientInitialization
97108
{
98-
internal static partial IServiceCollection AddCabazureClient<TOptions>(
109+
internal static partial IServiceCollection AddCabazureClient<{{aotAttribute}}TOptions>(
99110
this IServiceCollection services,
100111
string clientName,
101112
Action<JsonSerializerOptions>? jsonOptions,

src/Cabazure.Client/TypeConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ public static class TypeConstants
2020
public const string Task = "System.Threading.Tasks.Task";
2121
public const string EndpointResponse = "Cabazure.Client.EndpointResponse";
2222
public const string PagedResponse = "Cabazure.Client.PagedResponse";
23+
public const string DynamicallyAccessedMembersAttribute = "System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute";
2324
}

test/Cabazure.Client.IntegrationTests/Cabazure.Client.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<IsTestProject>true</IsTestProject>
1010
<EnableNETAnalyzers>true</EnableNETAnalyzers>
1111
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
12+
<IsAotCompatible>true</IsAotCompatible>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

test/Cabazure.Client.Tests/ClientInitializationGeneratorTests.CanGenerate_Initialization#ClientInitialization.Implementation.g.verified.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#nullable enable
44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Net.Http;
78
using System.Text.Json;
89
using Azure.Core;
@@ -16,7 +17,7 @@ namespace Microsoft.Extensions.DependencyInjection
1617
{
1718
internal static partial class ClientInitialization
1819
{
19-
internal static partial IServiceCollection AddCabazureClient<TOptions>(
20+
internal static partial IServiceCollection AddCabazureClient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TOptions>(
2021
this IServiceCollection services,
2122
string clientName,
2223
Action<JsonSerializerOptions>? jsonOptions,

0 commit comments

Comments
 (0)