Skip to content

Commit 059e3f0

Browse files
committed
Add support for source generators in Razor compiler/SDK
1 parent 5cae9ad commit 059e3f0

27 files changed

+1032
-216
lines changed

sdk.sln

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.WorkloadM
264264
EndProject
265265
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BuiltInTools", "BuiltInTools", "{71A9F549-0EB6-41F9-BC16-4A6C5007FC91}"
266266
EndProject
267-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-watch", "src\BuiltInTools\dotnet-watch\dotnet-watch.csproj", "{445EFBD5-6730-4F09-943D-278E77501FFD}"
267+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-watch", "src\BuiltInTools\dotnet-watch\dotnet-watch.csproj", "{445EFBD5-6730-4F09-943D-278E77501FFD}"
268268
EndProject
269-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.Watch.BrowserRefresh", "src\BuiltInTools\BrowserRefresh\Microsoft.AspNetCore.Watch.BrowserRefresh.csproj", "{A82EF2B9-24BC-4569-8FE5-9EF51017F4CB}"
269+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Watch.BrowserRefresh", "src\BuiltInTools\BrowserRefresh\Microsoft.AspNetCore.Watch.BrowserRefresh.csproj", "{A82EF2B9-24BC-4569-8FE5-9EF51017F4CB}"
270270
EndProject
271-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-watch.Tests", "src\Tests\dotnet-watch.Tests\dotnet-watch.Tests.csproj", "{CCE1A328-9CFE-44D3-B68F-FE84A039ACEA}"
271+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-watch.Tests", "src\Tests\dotnet-watch.Tests\dotnet-watch.Tests.csproj", "{CCE1A328-9CFE-44D3-B68F-FE84A039ACEA}"
272272
EndProject
273-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.Watch.BrowserRefresh.Tests", "src\Tests\Microsoft.AspNetCore.Watch.BrowserRefresh.Tests\Microsoft.AspNetCore.Watch.BrowserRefresh.Tests.csproj", "{81ADA3FA-AC26-4149-8CFC-EC7808ECB820}"
273+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Watch.BrowserRefresh.Tests", "src\Tests\Microsoft.AspNetCore.Watch.BrowserRefresh.Tests\Microsoft.AspNetCore.Watch.BrowserRefresh.Tests.csproj", "{81ADA3FA-AC26-4149-8CFC-EC7808ECB820}"
274274
EndProject
275-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetWatchTasks", "src\BuiltInTools\DotNetWatchTasks\DotNetWatchTasks.csproj", "{A41DF752-6F21-4036-AD02-DD37B11A2723}"
275+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetWatchTasks", "src\BuiltInTools\DotNetWatchTasks\DotNetWatchTasks.csproj", "{A41DF752-6F21-4036-AD02-DD37B11A2723}"
276276
EndProject
277277
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.BlazorWebAssembly.Tasks", "src\BlazorWasmSdk\Tasks\Microsoft.NET.Sdk.BlazorWebAssembly.Tasks.csproj", "{4AE60971-C960-4DDF-B671-8B3E32C1AFD2}"
278278
EndProject
@@ -330,6 +330,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Rules", "Rules", "{A117DF29
330330
src\RazorSdk\Targets\Rules\RazorGenerateWithTargetPath.xaml = src\RazorSdk\Targets\Rules\RazorGenerateWithTargetPath.xaml
331331
EndProjectSection
332332
EndProject
333+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SourceGenerators", "SourceGenerators", "{C2B15A41-A9C0-456A-A9FF-649E9237D850}"
334+
EndProject
335+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.Razor.SourceGenerators", "src\RazorSdk\SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.csproj", "{56C34654-DE8F-4F14-B2F8-6C37285B786E}"
336+
EndProject
333337
Global
334338
GlobalSection(SolutionConfigurationPlatforms) = preSolution
335339
Debug|Any CPU = Debug|Any CPU
@@ -588,6 +592,10 @@ Global
588592
{08C9E634-39F3-4B24-BCEA-D0B21971EBBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
589593
{08C9E634-39F3-4B24-BCEA-D0B21971EBBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
590594
{08C9E634-39F3-4B24-BCEA-D0B21971EBBE}.Release|Any CPU.Build.0 = Release|Any CPU
595+
{56C34654-DE8F-4F14-B2F8-6C37285B786E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
596+
{56C34654-DE8F-4F14-B2F8-6C37285B786E}.Debug|Any CPU.Build.0 = Debug|Any CPU
597+
{56C34654-DE8F-4F14-B2F8-6C37285B786E}.Release|Any CPU.ActiveCfg = Release|Any CPU
598+
{56C34654-DE8F-4F14-B2F8-6C37285B786E}.Release|Any CPU.Build.0 = Release|Any CPU
591599
EndGlobalSection
592600
GlobalSection(SolutionProperties) = preSolution
593601
HideSolutionNode = FALSE
@@ -696,6 +704,8 @@ Global
696704
{4ACCAC39-8ECD-45F8-B5AF-EB7E37CD36CC} = {E9BDA8A6-1AD2-4D0E-A37C-9EC10D7FE773}
697705
{D64884FD-A3F3-4082-A814-A9377B661509} = {E9BDA8A6-1AD2-4D0E-A37C-9EC10D7FE773}
698706
{A117DF29-1D72-453B-A24C-3B53F47609F2} = {D64884FD-A3F3-4082-A814-A9377B661509}
707+
{C2B15A41-A9C0-456A-A9FF-649E9237D850} = {E9BDA8A6-1AD2-4D0E-A37C-9EC10D7FE773}
708+
{56C34654-DE8F-4F14-B2F8-6C37285B786E} = {C2B15A41-A9C0-456A-A9FF-649E9237D850}
699709
EndGlobalSection
700710
GlobalSection(ExtensibilityGlobals) = postSolution
701711
SolutionGuid = {FB8F26CE-4DE6-433F-B32A-79183020BBD6}

src/Assets/TestProjects/RazorMvcWithComponents/MvcWithComponents.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,4 @@
99
<UseRazorBuildServer>false</UseRazorBuildServer>
1010
</PropertyGroup>
1111

12-
<ItemGroup>
13-
<FrameworkReference Include="Microsoft.AspNetCore.App" />
14-
</ItemGroup>
15-
1612
</Project>

src/Layout/redist/targets/GenerateLayout.targets

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,22 +236,31 @@
236236
Targets="Publish"
237237
Projects="$(RepoRoot)/src/RazorSdk/Tool/Microsoft.NET.Sdk.Razor.Tool.csproj"
238238
Properties="Configuration=$(Configuration)" />
239+
<MSBuild
240+
Targets="Publish"
241+
Projects="$(RepoRoot)/src/RazorSdk/SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.csproj"
242+
Properties="Configuration=$(Configuration)" />
239243
</Target>
240244

241245
<Target Name="MoveRazorSdkTools" AfterTargets="PublishRazorSdkTools">
242246
<ItemGroup>
243247
<_RazorToolOutput Include="$(ArtifactsBinDir)Microsoft.NET.Sdk.Razor.Tool\$(Configuration)\$(SdkTargetFramework)\publish\*.*" />
244248
<_MvcRazorExtensionOutput Include="$(ArtifactsBinDir)$(Configuration)\Sdks\Microsoft.NET.Sdk.Razor\tasks\$(SdkTargetFramework)\Microsoft.AspNetCore.Mvc.Razor.Extensions.dll" />
249+
<_RazorSourceGeneratorsOutput Include="$(ArtifactsBinDir)Microsoft.NET.Sdk.Razor.SourceGenerators\$(Configuration)\netstandard2.0\publish\*.*" />
245250
</ItemGroup>
246251

247252
<Copy
248253
SourceFiles="@(_RazorToolOutput)"
249254
DestinationFolder="$(ArtifactsBinDir)$(Configuration)\Sdks\Microsoft.NET.Sdk.Razor\tools\"
250255
SkipUnchangedFiles="true" />
251-
<Copy
256+
<Copy
252257
SourceFiles="@(_MvcRazorExtensionOutput)"
253258
DestinationFolder="$(ArtifactsBinDir)$(Configuration)\Sdks\Microsoft.NET.Sdk.Razor\extensions\mvc-3-0\"
254259
SkipUnchangedFiles="true" />
260+
<Copy
261+
SourceFiles="@(_RazorSourceGeneratorsOutput)"
262+
DestinationFolder="$(ArtifactsBinDir)$(Configuration)\Sdks\Microsoft.NET.Sdk.Razor\source-generators\"
263+
SkipUnchangedFiles="true" />
255264
</Target>
256265

257266
<Target Name="PublishTargetExtensions"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Razor.Language;
6+
7+
namespace Microsoft.NET.Sdk.Razor.SourceGenerators
8+
{
9+
internal class ConfigureRazorCodeGenerationOptions : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
10+
{
11+
private readonly Action<RazorCodeGenerationOptionsBuilder> _action;
12+
13+
public ConfigureRazorCodeGenerationOptions(Action<RazorCodeGenerationOptionsBuilder> action)
14+
{
15+
_action = action;
16+
}
17+
18+
public int Order { get; set; }
19+
20+
public void Configure(RazorCodeGenerationOptionsBuilder options) => _action(options);
21+
}
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<StrongNameKeyId>MicrosoftAspNetCore</StrongNameKeyId>
6+
7+
<!-- This is not a package, it is part of Razor SDK. -->
8+
<IsPackable>false</IsPackable>
9+
<IsShipping>false</IsShipping>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<Compile Include="..\Tool\TagHelperDescriptorJsonConverter.cs" LinkBase="Shared" />
14+
<Compile Include="..\Tool\RazorDiagnosticJsonConverter.cs" LinkBase="Shared" />
15+
<Compile Include="..\Tool\JsonReaderExtensions.cs" LinkBase="Shared" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" GeneratePathProperty="true"/>
20+
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" GeneratePathProperty="true" />
21+
22+
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="$(MicrosoftAspNetCoreRazorLanguageVersion)" GeneratePathProperty="true" />
23+
<PackageReference Include="Microsoft.CodeAnalysis.Razor" Version="$(MicrosoftCodeAnalysisRazorVersion)" GeneratePathProperty="true" />
24+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.Extensions" Version="$(MicrosoftAspNetCoreMvcRazorExtensionsPackageVersion)" GeneratePathProperty="true" />
25+
</ItemGroup>
26+
27+
<PropertyGroup>
28+
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
29+
</PropertyGroup>
30+
31+
<Target Name="GetDependencyTargetPaths">
32+
<ItemGroup>
33+
<TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_AspNetCore_Razor_Language)\lib\netstandard2.0\Microsoft.AspNetCore.Razor.Language.dll" IncludeRuntimeDependency="false" />
34+
<TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_CodeAnalysis_Razor)\lib\netstandard2.0\Microsoft.CodeAnalysis.Razor.dll" IncludeRuntimeDependency="false" />
35+
<TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_CodeAnalysis_CSharp)\lib\netstandard2.0\Microsoft.CodeAnalysis.CSharp.dll" IncludeRuntimeDependency="false" />
36+
</ItemGroup>
37+
</Target>
38+
39+
</Project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Globalization;
5+
using Microsoft.AspNetCore.Razor.Language;
6+
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.Text;
8+
using Microsoft.CodeAnalysis.Razor;
9+
10+
namespace Microsoft.NET.Sdk.Razor.SourceGenerators
11+
{
12+
internal static class RazorDiagnostics
13+
{
14+
public static readonly DiagnosticDescriptor InvalidRazorLangVersionDescriptor = new DiagnosticDescriptor(
15+
#pragma warning disable RS2008 // Enable analyzer release tracking
16+
"RZ3600",
17+
#pragma warning restore RS2008 // Enable analyzer release tracking
18+
"Invalid RazorLangVersion",
19+
"Invalid value {0} for RazorLangVersion. Valid values include 'Latest' or a valid version in range 1.0 to 5.0.",
20+
"Usage",
21+
DiagnosticSeverity.Error,
22+
isEnabledByDefault: true);
23+
24+
public static Diagnostic AsDiagnostic(this RazorDiagnostic razorDiagnostic)
25+
{
26+
var descriptor = new DiagnosticDescriptor(
27+
razorDiagnostic.Id,
28+
razorDiagnostic.GetMessage(CultureInfo.CurrentCulture),
29+
razorDiagnostic.GetMessage(CultureInfo.CurrentCulture),
30+
"Razor",
31+
razorDiagnostic.Severity switch
32+
{
33+
RazorDiagnosticSeverity.Error => DiagnosticSeverity.Error,
34+
RazorDiagnosticSeverity.Warning => DiagnosticSeverity.Warning,
35+
_ => DiagnosticSeverity.Hidden,
36+
},
37+
isEnabledByDefault: true);
38+
39+
var span = razorDiagnostic.Span;
40+
var location = Location.Create(
41+
span.FilePath,
42+
span.AsTextSpan(),
43+
new LinePositionSpan(
44+
new LinePosition(span.LineIndex, span.CharacterIndex),
45+
new LinePosition(span.LineIndex, span.CharacterIndex + span.Length)));
46+
47+
return Diagnostic.Create(descriptor, location);
48+
}
49+
}
50+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.Razor;
7+
8+
namespace Microsoft.NET.Sdk.Razor.SourceGenerators
9+
{
10+
internal readonly struct RazorInputItem
11+
{
12+
public RazorInputItem(AdditionalText additionalText, string relativePath, string fileKind, string generatedOutputPath, string generatedDeclarationPath, string cssScope)
13+
{
14+
AdditionalText = additionalText;
15+
RelativePath = relativePath;
16+
CssScope = cssScope;
17+
NormalizedPath = '/' + relativePath
18+
.Replace(Path.DirectorySeparatorChar, '/')
19+
.Replace("//", "/");
20+
FileKind = fileKind;
21+
GeneratedOutputPath = generatedOutputPath;
22+
GeneratedDeclarationPath = generatedDeclarationPath;
23+
}
24+
25+
public AdditionalText AdditionalText { get; }
26+
27+
public string RelativePath { get; }
28+
29+
public string NormalizedPath { get; }
30+
31+
public string FileKind { get; }
32+
33+
public string CssScope { get; }
34+
35+
public string GeneratedOutputPath { get; }
36+
37+
public string GeneratedDeclarationPath { get; }
38+
}
39+
}

0 commit comments

Comments
 (0)