-
Notifications
You must be signed in to change notification settings - Fork 53
/
ServiceProviderGenerator.Execute.cs
343 lines (306 loc) · 20.4 KB
/
ServiceProviderGenerator.Execute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using CommunityToolkit.Extensions.DependencyInjection.SourceGenerators.Extensions;
using CommunityToolkit.Extensions.DependencyInjection.SourceGenerators.Helpers;
using CommunityToolkit.Extensions.DependencyInjection.SourceGenerators.Models;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace CommunityToolkit.Extensions.DependencyInjection.SourceGenerators;
/// <inheritdoc/>
partial class ServiceProviderGenerator : IIncrementalGenerator
{
/// <summary>
/// Helpers to generate the service registrations.
/// </summary>
private static class Execute
{
/// <summary>
/// A shared annotation used to track arguments.
/// </summary>
public static readonly SyntaxAnnotation ArgumentAnnotation = new();
/// <summary>
/// Checks whether the input <see cref="SyntaxNode"/> is a valid target for generation.
/// </summary>
/// <param name="syntaxNode">The input <see cref="SyntaxNode"/> instance to analyze.</param>
/// <param name="token">The cancellation token to use.</param>
/// <returns>Whether <paramref name="syntaxNode"/> is a valid generation target.</returns>
public static bool IsSyntaxTarget(SyntaxNode syntaxNode, CancellationToken token)
{
return syntaxNode.IsKind(SyntaxKind.MethodDeclaration);
}
/// <summary>
/// Gathers the info on all registered singleton services.
/// </summary>
/// <param name="context">The current <see cref="GeneratorAttributeSyntaxContext"/> instance with the provided info.</param>
/// <param name="token">The cancellation token to use.</param>
/// <returns>The gathered info for the current service collection, if available.</returns>
public static ServiceCollectionInfo? GetSingletonInfo(GeneratorAttributeSyntaxContext context, CancellationToken token)
{
return GetInfo(ServiceRegistrationKind.Singleton, context, token);
}
/// <summary>
/// Gathers the info on all registered transient services.
/// </summary>
/// <param name="context">The current <see cref="GeneratorAttributeSyntaxContext"/> instance with the provided info.</param>
/// <param name="token">The cancellation token to use.</param>
/// <returns>The gathered info for the current service collection, if available.</returns>
public static ServiceCollectionInfo? GetTransientInfo(GeneratorAttributeSyntaxContext context, CancellationToken token)
{
return GetInfo(ServiceRegistrationKind.Transient, context, token);
}
/// <summary>
/// Gathers the info on all registered services.
/// </summary>
/// <param name="registrationKind">The registration kind to use.</param>
/// <param name="context">The current <see cref="GeneratorAttributeSyntaxContext"/> instance with the provided info.</param>
/// <param name="token">The cancellation token to use.</param>
/// <returns>The gathered info for the current service collection, if available.</returns>
private static ServiceCollectionInfo? GetInfo(ServiceRegistrationKind registrationKind, GeneratorAttributeSyntaxContext context, CancellationToken token)
{
// Ensure that the target syntax node is valid:
// - It has to be a method declaration
// - The method has a single parameter of type Microsoft.Extensions.DependencyInjection.IServiceCollection
// - The method returns void or Microsoft.Extensions.DependencyInjection.IServiceCollection
if (context.TargetNode is not MethodDeclarationSyntax methodDeclaration ||
context.TargetSymbol is not IMethodSymbol { Parameters: [{ } parameterSymbol] } methodSymbol ||
!parameterSymbol.Type.HasFullyQualifiedMetadataName("Microsoft.Extensions.DependencyInjection.IServiceCollection") ||
!(methodSymbol.ReturnsVoid || methodSymbol.ReturnType.HasFullyQualifiedMetadataName("Microsoft.Extensions.DependencyInjection.IServiceCollection")))
{
return null;
}
// Gather the basic method info
HierarchyInfo hierarchy = HierarchyInfo.From(methodSymbol.ContainingType);
string methodName = methodSymbol.Name;
token.ThrowIfCancellationRequested();
using ImmutableArrayBuilder<ushort> methodModifiers = ImmutableArrayBuilder<ushort>.Rent();
// Gather all method modifiers
foreach (SyntaxToken modifier in methodDeclaration.Modifiers)
{
methodModifiers.Add((ushort)modifier.Kind());
}
token.ThrowIfCancellationRequested();
using ImmutableArrayBuilder<RegisteredServiceInfo> serviceInfo = ImmutableArrayBuilder<RegisteredServiceInfo>.Rent();
// Gather all registered services
foreach (AttributeData attributeData in context.Attributes)
{
token.ThrowIfCancellationRequested();
if (attributeData.ConstructorArguments is [
{ Kind: TypedConstantKind.Type, Value: INamedTypeSymbol { InstanceConstructors: [IMethodSymbol implementationConstructor, ..] } implementationType },
{ Kind: TypedConstantKind.Array, Values: ImmutableArray<TypedConstant> serviceTypes }])
{
// Gather all dependent services for the implementation type
ImmutableArray<string> constructorArgumentTypes = ImmutableArray.CreateRange(
items: implementationConstructor.Parameters,
selector: static parameter => parameter.Type.GetFullyQualifiedName());
string implementationTypeName = implementationType.GetFullyQualifiedName();
ImmutableArray<string> serviceTypeNames = ImmutableArray<string>.Empty;
// If there are no specified service types, use the implementation type itself as service type. This is pretty
// common for eg. factory types, which don't need to be mocked and are just registered as concrete types.
if (serviceTypes.IsEmpty)
{
serviceTypeNames = ImmutableArray.Create(implementationTypeName);
}
else
{
using ImmutableArrayBuilder<string> builder = ImmutableArrayBuilder<string>.Rent();
// Otherwise, simply gather all service types for the current service registration
foreach (TypedConstant serviceType in serviceTypes)
{
if (serviceType is { Kind: TypedConstantKind.Type, Value: INamedTypeSymbol serviceTypeSymbol })
{
builder.Add(serviceTypeSymbol.GetFullyQualifiedName());
}
}
serviceTypeNames = builder.ToImmutable();
}
// Create the model fully describing the current service registration
serviceInfo.Add(new RegisteredServiceInfo(
RegistrationKind: registrationKind,
ImplementationFullyQualifiedTypeName: implementationTypeName,
ServiceFullyQualifiedTypeNames: serviceTypeNames,
RequiredServiceFullyQualifiedTypeNames: constructorArgumentTypes));
}
}
ServiceProviderMethodInfo methodInfo = new(
hierarchy,
methodName,
parameterSymbol.Name,
methodSymbol.ReturnsVoid,
methodModifiers.ToImmutable());
return new(methodInfo, serviceInfo.ToImmutable());
}
/// <summary>
/// Gets a <see cref="CompilationUnitSyntax"/> instance with the gathered info.
/// </summary>
/// <param name="info">The input <see cref="ServiceCollectionInfo"/> instance with the services info.</param>
/// <returns>A <see cref="CompilationUnitSyntax"/> instance with the gathered info.</returns>
public static CompilationUnitSyntax GetSyntax(ServiceCollectionInfo info)
{
using ImmutableArrayBuilder<StatementSyntax> registrationStatements = ImmutableArrayBuilder<StatementSyntax>.Rent();
foreach (RegisteredServiceInfo serviceInfo in info.Services)
{
// The first service type always acts as "main" registration, and should always be present
if (serviceInfo.ServiceFullyQualifiedTypeNames.AsSpan() is not [string rootServiceTypeName, ..ReadOnlySpan<string> dependentServiceTypeNames])
{
continue;
}
using ImmutableArrayBuilder<ArgumentSyntax> constructorArguments = ImmutableArrayBuilder<ArgumentSyntax>.Rent();
// Prepare the dependent services for the implementation type
foreach (string constructorServiceType in serviceInfo.RequiredServiceFullyQualifiedTypeNames)
{
// Create an argument for each constructor parameter:
//
// global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredServices<SERVICE_TYPE>(<PARAMETER_NAME>);
constructorArguments.Add(
Argument(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions"),
GenericName(Identifier("GetRequiredService"))
.AddTypeArgumentListArguments(IdentifierName(constructorServiceType))))
.AddArgumentListArguments(Argument(IdentifierName(info.Method.ServiceCollectionParameterName))))
.WithAdditionalAnnotations(ArgumentAnnotation));
}
// Prepare the method name, either AddSingleton or AddTransient
string registrationMethod = $"Add{serviceInfo.RegistrationKind}";
// Special case when the service is a singleton and no dependent services are present, just use eager instantiation instead:
//
// global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton(<PARAMETER_NAME>, typeof(<ROOT_SERVICE_TYPE>), new <IMPLEMENTATION_TYPE>());
if (serviceInfo.RegistrationKind == ServiceRegistrationKind.Singleton && constructorArguments.Count == 0)
{
registrationStatements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions"),
IdentifierName("AddSingleton")))
.AddArgumentListArguments(
Argument(IdentifierName(info.Method.ServiceCollectionParameterName)),
Argument(TypeOfExpression(IdentifierName(rootServiceTypeName))),
Argument(
ObjectCreationExpression(IdentifierName(serviceInfo.ImplementationFullyQualifiedTypeName))
.WithArgumentList(ArgumentList())))));
}
else
{
// Register the main implementation type when at least a dependent service is needed:
//
// global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.<REGISTRATION_METHOD>(<PARAMETER_NAME>, typeof(<ROOT_SERVICE_TYPE>), static services => new <IMPLEMENTATION_TYPE>(<CONSTRUCTOR_ARGUMENTS>));
registrationStatements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions"),
IdentifierName(registrationMethod)))
.AddArgumentListArguments(
Argument(IdentifierName(info.Method.ServiceCollectionParameterName)),
Argument(TypeOfExpression(IdentifierName(rootServiceTypeName))),
Argument(
SimpleLambdaExpression(Parameter(Identifier("services")))
.AddModifiers(Token(SyntaxKind.StaticKeyword))
.WithExpressionBody(
ObjectCreationExpression(IdentifierName(serviceInfo.ImplementationFullyQualifiedTypeName))
.AddArgumentListArguments(constructorArguments.ToArray()))))));
}
// Register all secondary services, if any
foreach (string dependentServiceType in dependentServiceTypeNames)
{
// Register the main implementation type:
//
// global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.<REGISTRATION_METHOD>(<PARAMETER_NAME>, typeof(<DEPENDENT_SERVICE_TYPE>), static services => global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredServices<ROOT_SERVICE_TYPE>(services));
registrationStatements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions"),
IdentifierName(registrationMethod)))
.AddArgumentListArguments(
Argument(IdentifierName(info.Method.ServiceCollectionParameterName)),
Argument(TypeOfExpression(IdentifierName(dependentServiceType))),
Argument(
SimpleLambdaExpression(Parameter(Identifier("services")))
.AddModifiers(Token(SyntaxKind.StaticKeyword))
.WithExpressionBody(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions"),
GenericName(Identifier("GetRequiredService"))
.AddTypeArgumentListArguments(IdentifierName(rootServiceTypeName))))
.AddArgumentListArguments(Argument(IdentifierName("services"))))))));
}
}
// Return the input service provider, if needed:
//
// return <PARAMETER_NAME>;
if (!info.Method.ReturnsVoid)
{
registrationStatements.Add(ReturnStatement(IdentifierName(info.Method.ServiceCollectionParameterName)).WithLeadingTrivia(Comment(" ")));
}
// Prepare the return type: either void or IServiceCollection
TypeSyntax returnType = info.Method.ReturnsVoid switch
{
true => PredefinedType(Token(SyntaxKind.VoidKeyword)),
false => IdentifierName("global::Microsoft.Extensions.DependencyInjection.IServiceCollection")
};
// Get the service collection configuration method declaration:
//
// /// <inheritdoc/>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.DebuggerNonUserCode]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// <MODIFIERS> <RETURN_TYPE> <METHOD_NAME>(global::Microsoft.Extensions.DependencyInjection.IServiceCollection <PARAMETER_NAME>)
// {
// <REGISTRATION_STATEMENTS>
// }
MethodDeclarationSyntax configureServicesMethodDeclaration =
MethodDeclaration(returnType, Identifier(info.Method.MethodName))
.AddModifiers(info.Method.Modifiers.AsImmutableArray().Select(static m => Token((SyntaxKind)m)).ToArray())
.AddParameterListParameters(
Parameter(Identifier(info.Method.ServiceCollectionParameterName))
.WithType(IdentifierName("global::Microsoft.Extensions.DependencyInjection.IServiceCollection")))
.AddBodyStatements(registrationStatements.ToArray())
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName($"global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ServiceProviderGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ServiceProviderGenerator).Assembly.GetName().Version.ToString())))))),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.WithLeadingTrivia(Comment("/// <inheritdoc/>"));
// Create the compilation unit with the generated members:
CompilationUnitSyntax compilationUnit = info.Method.Hierarchy.GetCompilationUnit(ImmutableArray.Create<MemberDeclarationSyntax>(configureServicesMethodDeclaration));
// Format the annotations
FormatArgumentNodes(ref compilationUnit, info.Method.Hierarchy.Hierarchy.AsSpan().Length + 3);
return compilationUnit;
}
/// <summary>
/// Formats the arguments with a given annotation adding leading whitespace.
/// </summary>
/// <param name="compilationUnit">The target <see cref="CompilationUnitSyntax"/> instance to modify.</param>
/// <param name="indentationLevel">The indentation level to format arguments for.</param>
private static void FormatArgumentNodes(ref CompilationUnitSyntax compilationUnit, int indentationLevel)
{
string whitespace = new(' ', indentationLevel * 4);
while (compilationUnit.GetAnnotatedNodes(ArgumentAnnotation).FirstOrDefault() is SyntaxNode annotatedNode)
{
// For each argument node, remove the annotation and add a CRLF and the leading whitespace. We can't
// loop over the annotated nodes on the target unit directly, as it's changed for every iteration.
compilationUnit = compilationUnit.ReplaceNode(
annotatedNode,
annotatedNode.WithoutAnnotations(ArgumentAnnotation).WithLeadingTrivia(CarriageReturnLineFeed, Whitespace(whitespace)));
}
}
}
}