/
RazorSourceGenerator.cs
254 lines (208 loc) · 11.6 KB
/
RazorSourceGenerator.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace Microsoft.NET.Sdk.Razor.SourceGenerators
{
[Generator]
public partial class RazorSourceGenerator : IIncrementalGenerator
{
private static RazorSourceGeneratorEventSource Log => RazorSourceGeneratorEventSource.Log;
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var analyzerConfigOptions = context.AnalyzerConfigOptionsProvider;
var parseOptions = context.ParseOptionsProvider;
var compilation = context.CompilationProvider;
// determine if we should suppress this run and filter out all the additional files if so
var isGeneratorSuppressed = context.AnalyzerConfigOptionsProvider.Select(GetSuppressionStatus);
var additionalTexts = context.AdditionalTextsProvider
.Combine(isGeneratorSuppressed)
.Where(pair => !pair.Right)
.Select((pair, _) => pair.Left);
var razorSourceGeneratorOptions = analyzerConfigOptions
.Combine(parseOptions)
.Select(ComputeRazorSourceGeneratorOptions)
.ReportDiagnostics(context);
var sourceItems = additionalTexts
.Where(static (file) => file.Path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase) || file.Path.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
.Combine(analyzerConfigOptions)
.Select(ComputeProjectItems)
.ReportDiagnostics(context);
var hasRazorFiles = sourceItems.Collect()
.Select(static (sourceItems, _) => sourceItems.Any());
var importFiles = sourceItems.Where(static file =>
{
var path = file.FilePath;
if (path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase))
{
var fileName = Path.GetFileNameWithoutExtension(path);
return string.Equals(fileName, "_Imports", StringComparison.OrdinalIgnoreCase);
}
else if (path.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
{
var fileName = Path.GetFileNameWithoutExtension(path);
return string.Equals(fileName, "_ViewImports", StringComparison.OrdinalIgnoreCase);
}
return false;
});
var componentFiles = sourceItems.Where(static file => file.FilePath.EndsWith(".razor", StringComparison.OrdinalIgnoreCase));
var generatedDeclarationCode = componentFiles
.Combine(importFiles.Collect())
.Combine(razorSourceGeneratorOptions)
.Select(static (pair, _) =>
{
var ((sourceItem, importFiles), razorSourceGeneratorOptions) = pair;
RazorSourceGeneratorEventSource.Log.GenerateDeclarationCodeStart(sourceItem.FilePath);
var projectEngine = GetDeclarationProjectEngine(sourceItem, importFiles, razorSourceGeneratorOptions);
var codeGen = projectEngine.Process(sourceItem);
var result = codeGen.GetCSharpDocument().GeneratedCode;
RazorSourceGeneratorEventSource.Log.GenerateDeclarationCodeStop(sourceItem.FilePath);
return result;
});
var generatedDeclarationSyntaxTrees = generatedDeclarationCode
.Combine(parseOptions)
.Select(static (pair, _) =>
{
var (generatedDeclarationCode, parseOptions) = pair;
return CSharpSyntaxTree.ParseText(generatedDeclarationCode, (CSharpParseOptions)parseOptions);
});
var tagHelpersFromCompilation = compilation
.Combine(generatedDeclarationSyntaxTrees.Collect())
.Combine(razorSourceGeneratorOptions)
.Select(static (pair, _) =>
{
RazorSourceGeneratorEventSource.Log.DiscoverTagHelpersFromCompilationStart();
var ((compilation, generatedDeclarationSyntaxTrees), razorSourceGeneratorOptions) = pair;
var tagHelperFeature = new StaticCompilationTagHelperFeature();
var discoveryProjectEngine = GetDiscoveryProjectEngine(compilation.References.ToImmutableArray(), tagHelperFeature);
var compilationWithDeclarations = compilation.AddSyntaxTrees(generatedDeclarationSyntaxTrees);
tagHelperFeature.Compilation = compilationWithDeclarations;
tagHelperFeature.TargetAssembly = compilationWithDeclarations.Assembly;
var result = (IList<TagHelperDescriptor>)tagHelperFeature.GetDescriptors();
RazorSourceGeneratorEventSource.Log.DiscoverTagHelpersFromCompilationStop();
return result;
})
.WithLambdaComparer(static (a, b) =>
{
if (a.Count != b.Count)
{
return false;
}
for (var i = 0; i < a.Count; i++)
{
if (!a[i].Equals(b[i]))
{
return false;
}
}
return true;
}, getHashCode: static a => a.Count);
var tagHelpersFromReferences = compilation
.Combine(razorSourceGeneratorOptions)
.Combine(hasRazorFiles)
.WithLambdaComparer(static (a, b) =>
{
var ((compilationA, razorSourceGeneratorOptionsA), hasRazorFilesA) = a;
var ((compilationB, razorSourceGeneratorOptionsB), hasRazorFilesB) = b;
if (!compilationA.References.SequenceEqual(compilationB.References))
{
return false;
}
if (razorSourceGeneratorOptionsA != razorSourceGeneratorOptionsB)
{
return false;
}
return hasRazorFilesA == hasRazorFilesB;
},
static item =>
{
// we'll use the number of references as a hashcode.
var ((compilationA, razorSourceGeneratorOptionsA), hasRazorFilesA) = item;
return compilationA.References.GetHashCode();
})
.Select(static (pair, _) =>
{
RazorSourceGeneratorEventSource.Log.DiscoverTagHelpersFromReferencesStart();
var ((compilation, razorSourceGeneratorOptions), hasRazorFiles) = pair;
if (!hasRazorFiles)
{
// If there's no razor code in this app, don't do anything.
RazorSourceGeneratorEventSource.Log.DiscoverTagHelpersFromReferencesStop();
return ImmutableArray<TagHelperDescriptor>.Empty;
}
var tagHelperFeature = new StaticCompilationTagHelperFeature();
var discoveryProjectEngine = GetDiscoveryProjectEngine(compilation.References.ToImmutableArray(), tagHelperFeature);
List<TagHelperDescriptor> descriptors = new();
tagHelperFeature.Compilation = compilation;
foreach (var reference in compilation.References)
{
if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
{
tagHelperFeature.TargetAssembly = assembly;
descriptors.AddRange(tagHelperFeature.GetDescriptors());
}
}
RazorSourceGeneratorEventSource.Log.DiscoverTagHelpersFromReferencesStop();
return (ICollection<TagHelperDescriptor>)descriptors;
});
var allTagHelpers = tagHelpersFromCompilation
.Combine(tagHelpersFromReferences)
.Select(static (pair, _) =>
{
var (tagHelpersFromCompilation, tagHelpersFromReferences) = pair;
var count = tagHelpersFromCompilation.Count + tagHelpersFromReferences.Count;
if (count == 0)
{
return Array.Empty<TagHelperDescriptor>();
}
var allTagHelpers = new TagHelperDescriptor[count];
tagHelpersFromCompilation.CopyTo(allTagHelpers, 0);
tagHelpersFromReferences.CopyTo(allTagHelpers, tagHelpersFromCompilation.Count);
return allTagHelpers;
});
var generatedOutput = sourceItems
.Combine(importFiles.Collect())
.Combine(allTagHelpers)
.Combine(razorSourceGeneratorOptions)
.Select(static (pair, _) =>
{
var (((sourceItem, imports), allTagHelpers), razorSourceGeneratorOptions) = pair;
RazorSourceGeneratorEventSource.Log.RazorCodeGenerateStart(sourceItem.FilePath);
// Add a generated suffix so tools, such as coverlet, consider the file to be generated
var hintName = GetIdentifierFromPath(sourceItem.RelativePhysicalPath) + ".g.cs";
var projectEngine = GetGenerationProjectEngine(allTagHelpers, sourceItem, imports, razorSourceGeneratorOptions);
var codeDocument = projectEngine.Process(sourceItem);
var csharpDocument = codeDocument.GetCSharpDocument();
RazorSourceGeneratorEventSource.Log.RazorCodeGenerateStop(sourceItem.FilePath);
return (hintName, csharpDocument);
})
.WithLambdaComparer(static (a, b) =>
{
if (a.csharpDocument.Diagnostics.Count > 0 || b.csharpDocument.Diagnostics.Count > 0)
{
// if there are any diagnostics, treat the documents as unequal and force RegisterSourceOutput to be called uncached.
return false;
}
return string.Equals(a.csharpDocument.GeneratedCode, b.csharpDocument.GeneratedCode, StringComparison.Ordinal);
}, static a => StringComparer.Ordinal.GetHashCode(a.csharpDocument));
context.RegisterSourceOutput(generatedOutput, static (context, pair) =>
{
var (hintName, csharpDocument) = pair;
RazorSourceGeneratorEventSource.Log.AddSyntaxTrees(hintName);
for (var i = 0; i < csharpDocument.Diagnostics.Count; i++)
{
var razorDiagnostic = csharpDocument.Diagnostics[i];
var csharpDiagnostic = razorDiagnostic.AsDiagnostic();
context.ReportDiagnostic(csharpDiagnostic);
}
context.AddSource(hintName, csharpDocument.GeneratedCode);
});
}
}
}