-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for source generators in Razor compiler/SDK #15756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bcba240
5f18a1e
3ca1b1a
90f897a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} |
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"/> | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<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> |
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't the right pattern here. The compiler doesn't generate diagnostics based on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think that pattern should fit what you're doing here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice tip -- I've tracked this in dotnet/aspnetcore#30237 since I don't think this will happen for preview2 but we can sort out the diagnostics story in preview3. |
||
"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); | ||
} | ||
} | ||
} |
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; } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly relevant: dotnet/installer#9635 (comment).