-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for source generators in Razor compiler/SDK
- Loading branch information
1 parent
5cae9ad
commit 9a2b607
Showing
26 changed files
with
1,034 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/RazorSdk/SourceGenerators/ConfigureRazorCodeGenerationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// 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 Microsoft.AspNetCore.Razor.Language; | ||
|
||
namespace Microsoft.NET.Sdk.Razor.SourceGenerators | ||
{ | ||
internal class ConfigureRazorCodeGenerationOptions : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature | ||
{ | ||
private readonly Action<RazorCodeGenerationOptionsBuilder> _action; | ||
|
||
public ConfigureRazorCodeGenerationOptions(Action<RazorCodeGenerationOptionsBuilder> action) | ||
{ | ||
_action = action; | ||
} | ||
|
||
public int Order { get; set; } | ||
|
||
public void Configure(RazorCodeGenerationOptionsBuilder options) => _action(options); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/RazorSdk/SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<StrongNameKeyId>MicrosoftAspNetCore</StrongNameKeyId> | ||
|
||
<!-- This is not a package, it is part of Razor SDK. --> | ||
<IsPackable>false</IsPackable> | ||
<IsShipping>false</IsShipping> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\Tool\TagHelperDescriptorJsonConverter.cs" LinkBase="Shared" /> | ||
<Compile Include="..\Tool\RazorDiagnosticJsonConverter.cs" LinkBase="Shared" /> | ||
<Compile Include="..\Tool\JsonReaderExtensions.cs" LinkBase="Shared" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" GeneratePathProperty="true"/> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" GeneratePathProperty="true"/> | ||
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="$(MicrosoftAspNetCoreRazorLanguageVersion)" GeneratePathProperty="true" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Razor" Version="$(MicrosoftCodeAnalysisRazorVersion)" GeneratePathProperty="true" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.Extensions" Version="$(MicrosoftAspNetCoreMvcRazorExtensionsPackageVersion)" GeneratePathProperty="true" /> | ||
</ItemGroup> | ||
|
||
<!-- See https://github.com/dotnet/roslyn/discussions/47517#discussioncomment-64145 --> | ||
<PropertyGroup> | ||
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn> | ||
</PropertyGroup> | ||
|
||
<Target Name="GetDependencyTargetPaths"> | ||
<ItemGroup> | ||
<TargetPathWithTargetPlatformMoniker Include="$(PkgNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" /> | ||
<TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_AspNetCore_Razor_Language)\lib\netstandard2.0\Microsoft.AspNetCore.Razor.Language.dll" IncludeRuntimeDependency="false" /> | ||
<TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_CodeAnalysis_Razor)\lib\netstandard2.0\Microsoft.CodeAnalysis.Razor.dll" IncludeRuntimeDependency="false" /> | ||
<TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_CodeAnalysis_CSharp)\lib\netstandard2.0\Microsoft.CodeAnalysis.CSharp.dll" IncludeRuntimeDependency="false" /> | ||
<TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_AspNetCore_Mvc_Razor_Extensions)\lib\netstandard2.0\Microsoft.AspNetCore.Mvc.Razor.Extensions.dll" IncludeRuntimeDependency="false" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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.Globalization; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.CodeAnalysis.Razor; | ||
|
||
namespace Microsoft.NET.Sdk.Razor.SourceGenerators | ||
{ | ||
internal static class RazorDiagnostics | ||
{ | ||
public static readonly DiagnosticDescriptor InvalidRazorLangVersionDescriptor = new DiagnosticDescriptor( | ||
#pragma warning disable RS2008 // Enable analyzer release tracking | ||
"RZ3600", | ||
#pragma warning restore RS2008 // Enable analyzer release tracking | ||
"Invalid RazorLangVersion", | ||
"Invalid value {0} for RazorLangVersion. Valid values include 'Latest' or a valid version in range 1.0 to 5.0.", | ||
"Usage", | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true); | ||
|
||
public static Diagnostic AsDiagnostic(this RazorDiagnostic razorDiagnostic) | ||
{ | ||
var descriptor = new DiagnosticDescriptor( | ||
razorDiagnostic.Id, | ||
razorDiagnostic.GetMessage(CultureInfo.CurrentCulture), | ||
razorDiagnostic.GetMessage(CultureInfo.CurrentCulture), | ||
"Razor", | ||
razorDiagnostic.Severity switch | ||
{ | ||
RazorDiagnosticSeverity.Error => DiagnosticSeverity.Error, | ||
RazorDiagnosticSeverity.Warning => DiagnosticSeverity.Warning, | ||
_ => DiagnosticSeverity.Hidden, | ||
}, | ||
isEnabledByDefault: true); | ||
|
||
var span = razorDiagnostic.Span; | ||
var location = Location.Create( | ||
span.FilePath, | ||
span.AsTextSpan(), | ||
new LinePositionSpan( | ||
new LinePosition(span.LineIndex, span.CharacterIndex), | ||
new LinePosition(span.LineIndex, span.CharacterIndex + span.Length))); | ||
|
||
return Diagnostic.Create(descriptor, location); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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.IO; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Razor; | ||
|
||
namespace Microsoft.NET.Sdk.Razor.SourceGenerators | ||
{ | ||
internal readonly struct RazorInputItem | ||
{ | ||
public RazorInputItem(AdditionalText additionalText, string relativePath, string fileKind, string generatedOutputPath, string generatedDeclarationPath, string cssScope) | ||
{ | ||
AdditionalText = additionalText; | ||
RelativePath = relativePath; | ||
CssScope = cssScope; | ||
NormalizedPath = '/' + relativePath | ||
.Replace(Path.DirectorySeparatorChar, '/') | ||
.Replace("//", "/"); | ||
FileKind = fileKind; | ||
GeneratedOutputPath = generatedOutputPath; | ||
GeneratedDeclarationPath = generatedDeclarationPath; | ||
} | ||
|
||
public AdditionalText AdditionalText { get; } | ||
|
||
public string RelativePath { get; } | ||
|
||
public string NormalizedPath { get; } | ||
|
||
public string FileKind { get; } | ||
|
||
public string CssScope { get; } | ||
|
||
public string GeneratedOutputPath { get; } | ||
|
||
public string GeneratedDeclarationPath { get; } | ||
} | ||
} |
Oops, something went wrong.