-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi-target LoggerMessageGenerator between Roslyn v3.9 and v4.0
- Loading branch information
Showing
14 changed files
with
171 additions
and
17 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
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
63 changes: 63 additions & 0 deletions
63
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.3.9.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,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
[assembly: System.Resources.NeutralResourcesLanguage("en-us")] | ||
|
||
namespace Microsoft.Extensions.Logging.Generators | ||
{ | ||
[Generator] | ||
public partial class LoggerMessageGenerator : ISourceGenerator | ||
{ | ||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
context.RegisterForSyntaxNotifications(SyntaxContextReceiver.Create); | ||
} | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
if (context.SyntaxContextReceiver is not SyntaxContextReceiver receiver || receiver.ClassDeclarations.Count == 0) | ||
{ | ||
// nothing to do yet | ||
return; | ||
} | ||
|
||
var p = new Parser(context.Compilation, context.ReportDiagnostic, context.CancellationToken); | ||
IReadOnlyList<LoggerClass> logClasses = p.GetLogClasses(receiver.ClassDeclarations); | ||
if (logClasses.Count > 0) | ||
{ | ||
var e = new Emitter(); | ||
string result = e.Emit(logClasses, context.CancellationToken); | ||
|
||
context.AddSource("LoggerMessage.g.cs", SourceText.From(result, Encoding.UTF8)); | ||
} | ||
} | ||
|
||
private sealed class SyntaxContextReceiver : ISyntaxContextReceiver | ||
{ | ||
internal static ISyntaxContextReceiver Create() | ||
{ | ||
return new SyntaxContextReceiver(); | ||
} | ||
|
||
public HashSet<ClassDeclarationSyntax> ClassDeclarations { get; } = new(); | ||
|
||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context) | ||
{ | ||
if (Parser.IsSyntaxTargetForGeneration(context.Node)) | ||
{ | ||
ClassDeclarationSyntax classSyntax = Parser.GetSemanticTargetForGeneration(context); | ||
if (classSyntax != null) | ||
{ | ||
ClassDeclarations.Add(classSyntax); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
...bstractions/gen/LoggerMessageGenerator.cs → ...actions/gen/LoggerMessageGenerator.4.0.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
14 changes: 14 additions & 0 deletions
14
...ft.Extensions.Logging.Abstractions/gen/Microsoft.Extensions.Logging.Generators.3.9.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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AnalyzerRoslynVersion>3.9</AnalyzerRoslynVersion> | ||
<RoslynApiVersion>$(MicrosoftCodeAnalysisCSharpWorkspacesVersion_39)</RoslynApiVersion> | ||
</PropertyGroup> | ||
|
||
<Import Project="Microsoft.Extensions.Logging.Generators.targets" /> | ||
|
||
<ItemGroup> | ||
<Compile Remove="LoggerMessageGenerator.4.0.cs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
14 changes: 14 additions & 0 deletions
14
...ft.Extensions.Logging.Abstractions/gen/Microsoft.Extensions.Logging.Generators.4.0.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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AnalyzerRoslynVersion>4.0</AnalyzerRoslynVersion> | ||
<RoslynApiVersion>$(MicrosoftCodeAnalysisCSharpWorkspacesVersion)</RoslynApiVersion> | ||
</PropertyGroup> | ||
|
||
<Import Project="Microsoft.Extensions.Logging.Generators.targets" /> | ||
|
||
<ItemGroup> | ||
<Compile Remove="LoggerMessageGenerator.3.9.cs" /> | ||
</ItemGroup> | ||
|
||
</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
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
14 changes: 14 additions & 0 deletions
14
...ensions.Logging.Generators.Tests/Microsoft.Extensions.Logging.Generators.3.9.Tests.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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<RoslynApiVersion>$(MicrosoftCodeAnalysisCSharpWorkspacesVersion_39)</RoslynApiVersion> | ||
<DefineConstants>$(DefineConstants);ROSLYN_3_9</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<Import Project="Microsoft.Extensions.Logging.Generators.targets"/> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\gen\Microsoft.Extensions.Logging.Generators.3.9.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" /> | ||
</ItemGroup> | ||
|
||
</Project> |
14 changes: 14 additions & 0 deletions
14
...ensions.Logging.Generators.Tests/Microsoft.Extensions.Logging.Generators.4.0.Tests.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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<RoslynApiVersion>$(MicrosoftCodeAnalysisCSharpWorkspacesVersion)</RoslynApiVersion> | ||
<DefineConstants>$(DefineConstants);ROSLYN_4_0</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<Import Project="Microsoft.Extensions.Logging.Generators.targets"/> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\gen\Microsoft.Extensions.Logging.Generators.4.0.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" /> | ||
</ItemGroup> | ||
|
||
</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
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