refactor(godot): 重构源代码生成器依赖结构

- 将CommonDiagnostics移至GFramework.SourceGenerators.Common模块
- 添加PathContests常量类统一管理路径常量
- 更新GodotLoggerGenerator使用新的路径常量和诊断引用
- 修改项目引用以包含GFramework.SourceGenerators.Common依赖
- 更新NuGet包配置以包含Common模块的DLL和XML文档
- 在解决方案文件中添加GFramework.SourceGenerators.Common项目引用
- 为Common模块创建Analyzer发布跟踪文件
This commit is contained in:
GwWuYou 2025-12-28 08:54:52 +08:00
parent 60a42a5f1e
commit 929ea6b2d6
19 changed files with 109 additions and 42 deletions

View File

@ -5,5 +5,4 @@
Rule ID | Category | Severity | Notes Rule ID | Category | Severity | Notes
----------------------|--------------------------|----------|------------------------ ----------------------|--------------------------|----------|------------------------
GF_Common_Class_001 | GFramework.Common | Error | CommonDiagnostics
GF_Godot_Logging_001 | GFramework.Godot.Logging | Warning | GodotLoggerDiagnostics GF_Godot_Logging_001 | GFramework.Godot.Logging | Warning | GodotLoggerDiagnostics

View File

@ -29,6 +29,7 @@
<!-- Generator 编译期引用 Attributes / Common但不打包 --> <!-- Generator 编译期引用 Attributes / Common但不打包 -->
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\GFramework.Godot.SourceGenerators.Abstractions\GFramework.Godot.SourceGenerators.Abstractions.csproj" PrivateAssets="all"/> <ProjectReference Include="..\GFramework.Godot.SourceGenerators.Abstractions\GFramework.Godot.SourceGenerators.Abstractions.csproj" PrivateAssets="all"/>
<ProjectReference Include="..\GFramework.SourceGenerators.Common\GFramework.SourceGenerators.Common.csproj" PrivateAssets="all"/>
</ItemGroup> </ItemGroup>
<!-- ★关键:只把 Generator DLL 放进 analyzers --> <!-- ★关键:只把 Generator DLL 放进 analyzers -->
@ -44,10 +45,17 @@
Pack="true" Pack="true"
PackagePath="analyzers/dotnet/cs" PackagePath="analyzers/dotnet/cs"
Visible="false"/> Visible="false"/>
<!-- ★ Generator 运行期依赖 -->
<None Include="$(OutputPath)\GFramework.SourceGenerators.Common.dll"
Pack="true"
PackagePath="analyzers/dotnet/cs"
Visible="false"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).Abstractions.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/> <None Include="$(OutputPath)\$(AssemblyName).Abstractions.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/>
<None Include="$(OutputPath)\$(AssemblyName).Abstractions.xml" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/> <None Include="$(OutputPath)\$(AssemblyName).Abstractions.xml" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/>
<None Include="$(OutputPath)\$(AssemblyName).Common.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/>
<None Include="$(OutputPath)\$(AssemblyName).Common.xml" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/>
<!-- 包含targets文件 --> <!-- 包含targets文件 -->
<None Include="GeWuYou.GFramework.Godot.SourceGenerators.targets" Pack="true" PackagePath="build" Visible="false"/> <None Include="GeWuYou.GFramework.Godot.SourceGenerators.targets" Pack="true" PackagePath="build" Visible="false"/>
</ItemGroup> </ItemGroup>

View File

@ -6,6 +6,7 @@
<ItemGroup> <ItemGroup>
<Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.Godot.SourceGenerators.dll"/> <Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.Godot.SourceGenerators.dll"/>
<Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.Godot.SourceGenerators.Abstractions.dll"/> <Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.Godot.SourceGenerators.Abstractions.dll"/>
<Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.SourceGenerators.Common.dll"/>
</ItemGroup> </ItemGroup>
<!-- Ensure the analyzers are loaded --> <!-- Ensure the analyzers are loaded -->

View File

@ -0,0 +1,6 @@
namespace GFramework.Godot.SourceGenerators.constants;
public static class PathContests
{
public const string RootAbstractionsPath = "GFramework.Godot.SourceGenerators.Abstractions";
}

View File

@ -1,7 +1,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using GFramework.Godot.SourceGenerators.diagnostics; using GFramework.Godot.SourceGenerators.constants;
using GFramework.SourceGenerators.Common.diagnostics;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -16,7 +17,7 @@ namespace GFramework.Godot.SourceGenerators.logging;
public sealed class GodotLoggerGenerator : IIncrementalGenerator public sealed class GodotLoggerGenerator : IIncrementalGenerator
{ {
private const string AttributeMetadataName = private const string AttributeMetadataName =
"GFramework.Godot.SourceGenerators.Abstractions.logging.GodotLogAttribute"; $"{PathContests.RootAbstractionsPath}.logging.GodotLogAttribute";
private const string AttributeShortName = "GodotLogAttribute"; private const string AttributeShortName = "GodotLogAttribute";
private const string AttributeShortNameWithoutSuffix = "GodotLog"; private const string AttributeShortNameWithoutSuffix = "GodotLog";

View File

@ -0,0 +1,3 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

View File

@ -0,0 +1,8 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
### New Rules
Rule ID | Category | Severity | Notes
---------------------|-------------------|----------|-------------------
GF_Common_Class_001 | GFramework.Common | Error | CommonDiagnostics

View File

@ -0,0 +1,24 @@
<Project>
<!-- import parent: https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build -->
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!--
we use a higher version than supported by the target framework to have nullable types and other nice features
(a lot of features get polyfilled by Meziantou.Polyfill)
however we need to be careful with the available features!
-->
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.264">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Polyfill" Version="1.0.71">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<!--
配置项目构建属性
设置项目不可打包、生成文档文件并包含特定的Polyfill
-->
<PropertyGroup>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<MeziantouPolyfill_IncludedPolyfills>T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute</MeziantouPolyfill_IncludedPolyfills>
</PropertyGroup>
<!-- 引入必要的命名空间 -->
<ItemGroup>
<Using Include="GFramework.SourceGenerators.Common"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.14.0" PrivateAssets="all"/>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="AnalyzerReleases.Shipped.md"/>
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md"/>
</ItemGroup>
</Project>

View File

@ -1,6 +1,6 @@
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
namespace GFramework.Godot.SourceGenerators.diagnostics; namespace GFramework.SourceGenerators.Common.diagnostics;
/// <summary> /// <summary>
/// 提供通用诊断描述符的静态类 /// 提供通用诊断描述符的静态类

View File

@ -5,6 +5,5 @@
Rule ID | Category | Severity | Notes Rule ID | Category | Severity | Notes
---------------------|----------------------------------|----------|------------------------ ---------------------|----------------------------------|----------|------------------------
GF_Common_Class_001 | GFramework.Common | Error | CommonDiagnostics
GF_Logging_001 | GFramework.Godot.Logging | Warning | LoggerDiagnostics GF_Logging_001 | GFramework.Godot.Logging | Warning | LoggerDiagnostics
GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic

View File

@ -29,6 +29,7 @@
<!-- Generator 编译期引用 Attributes / Common但不打包 --> <!-- Generator 编译期引用 Attributes / Common但不打包 -->
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\GFramework.SourceGenerators.Abstractions\GFramework.SourceGenerators.Abstractions.csproj" PrivateAssets="all"/> <ProjectReference Include="..\GFramework.SourceGenerators.Abstractions\GFramework.SourceGenerators.Abstractions.csproj" PrivateAssets="all"/>
<ProjectReference Include="..\GFramework.SourceGenerators.Common\GFramework.SourceGenerators.Common.csproj" PrivateAssets="all"/>
</ItemGroup> </ItemGroup>
<!-- ★关键:只把 Generator DLL 放进 analyzers --> <!-- ★关键:只把 Generator DLL 放进 analyzers -->
@ -44,12 +45,18 @@
Pack="true" Pack="true"
PackagePath="analyzers/dotnet/cs" PackagePath="analyzers/dotnet/cs"
Visible="false"/> Visible="false"/>
<!-- ★ Generator 运行期依赖 -->
<None Include="$(OutputPath)\GFramework.SourceGenerators.Common.dll"
Pack="true"
PackagePath="analyzers/dotnet/cs"
Visible="false"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).Abstractions.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/> <None Include="$(OutputPath)\$(AssemblyName).Abstractions.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/>
<None Include="$(OutputPath)\$(AssemblyName).Abstractions.xml" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/> <None Include="$(OutputPath)\$(AssemblyName).Abstractions.xml" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/>
<!-- 包含targets文件 --> <None Include="$(OutputPath)\$(AssemblyName).Common.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/>
<None Include="$(OutputPath)\$(AssemblyName).Common.xml" Pack="true" PackagePath="lib\netstandard2.0" Visible="true"/>
<None Include="GeWuYou.GFramework.SourceGenerators.targets" Pack="true" PackagePath="build" Visible="false"/> <None Include="GeWuYou.GFramework.SourceGenerators.targets" Pack="true" PackagePath="build" Visible="false"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -6,6 +6,7 @@
<ItemGroup> <ItemGroup>
<Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.SourceGenerators.dll"/> <Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.SourceGenerators.dll"/>
<Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.SourceGenerators.Abstractions.dll"/> <Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.SourceGenerators.Abstractions.dll"/>
<Analyzer Include="$(MSBuildThisFileDirectory)../analyzers/dotnet/cs/GFramework.SourceGenerators.Common.dll"/>
</ItemGroup> </ItemGroup>
<!-- Ensure the analyzers are loaded --> <!-- Ensure the analyzers are loaded -->

View File

@ -0,0 +1,6 @@
namespace GFramework.SourceGenerators.constants;
public static class PathContests
{
public const string RootAbstractionsPath = "GFramework.SourceGenerators.Abstractions";
}

View File

@ -1,29 +0,0 @@
using Microsoft.CodeAnalysis;
namespace GFramework.SourceGenerators.diagnostics;
/// <summary>
/// 提供通用诊断描述符的静态类
/// </summary>
public static class CommonDiagnostics
{
/// <summary>
/// 定义类必须为partial的诊断描述符
/// </summary>
/// <remarks>
/// 诊断ID: GF001
/// 诊断消息: "Class '{0}' must be declared partial for code generation"
/// 分类: GFramework.Common
/// 严重性: Error
/// 是否启用: true
/// </remarks>
public static readonly DiagnosticDescriptor ClassMustBePartial =
new(
"GF_Common_Class_001",
"Class must be partial",
"Class '{0}' must be declared partial for code generation",
"GFramework.Common",
DiagnosticSeverity.Error,
true
);
}

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using GFramework.SourceGenerators.constants;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
@ -11,7 +12,7 @@ namespace GFramework.SourceGenerators.enums;
public class EnumExtensionsGenerator : IIncrementalGenerator public class EnumExtensionsGenerator : IIncrementalGenerator
{ {
private const string AttributeFullName = private const string AttributeFullName =
"GFramework.SourceGenerators.Abstractions.generator.enums.GenerateEnumExtensionsAttribute"; $"{PathContests.RootAbstractionsPath}.enums.GenerateEnumExtensionsAttribute";
public void Initialize(IncrementalGeneratorInitializationContext context) public void Initialize(IncrementalGeneratorInitializationContext context)
{ {
@ -35,9 +36,8 @@ public class EnumExtensionsGenerator : IIncrementalGenerator
.Where(symbol => symbol != null) .Where(symbol => symbol != null)
.Select((symbol, _) => .Select((symbol, _) =>
{ {
var hasAttr = symbol.GetAttributes().Any(ad => var hasAttr = symbol!.GetAttributes().Any(ad =>
ad.AttributeClass?.ToDisplayString() == AttributeFullName || ad.AttributeClass?.ToDisplayString() == AttributeFullName);
ad.AttributeClass?.ToDisplayString().EndsWith(".GenerateEnumExtensionsAttribute") == true);
return (Symbol: symbol, HasAttr: hasAttr); return (Symbol: symbol, HasAttr: hasAttr);
}) })
.Where(x => x.HasAttr) .Where(x => x.HasAttr)

View File

@ -1,7 +1,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using GFramework.SourceGenerators.diagnostics; using GFramework.SourceGenerators.Common.diagnostics;
using GFramework.SourceGenerators.constants;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -15,7 +16,7 @@ namespace GFramework.SourceGenerators.logging;
[Generator] [Generator]
public sealed class LoggerGenerator : IIncrementalGenerator public sealed class LoggerGenerator : IIncrementalGenerator
{ {
private const string AttributeMetadataName = "GFramework.SourceGenerators.Abstractions.logging.LogAttribute"; private const string AttributeMetadataName = $"{PathContests.RootAbstractionsPath}.logging.LogAttribute";
private const string AttributeShortName = "LogAttribute"; private const string AttributeShortName = "LogAttribute";
private const string AttributeShortNameWithoutSuffix = "Log"; private const string AttributeShortNameWithoutSuffix = "Log";

View File

@ -1,5 +1,7 @@
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using GFramework.SourceGenerators.Common.diagnostics;
using GFramework.SourceGenerators.constants;
using GFramework.SourceGenerators.diagnostics; using GFramework.SourceGenerators.diagnostics;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
@ -11,7 +13,7 @@ namespace GFramework.SourceGenerators.rule;
public sealed class ContextAwareGenerator : IIncrementalGenerator public sealed class ContextAwareGenerator : IIncrementalGenerator
{ {
private const string AttributeMetadataName = private const string AttributeMetadataName =
"GFramework.SourceGenerators.Abstractions.rule.ContextAwareAttribute"; $"{PathContests.RootAbstractionsPath}.rule.ContextAwareAttribute";
public void Initialize(IncrementalGeneratorInitializationContext context) public void Initialize(IncrementalGeneratorInitializationContext context)
{ {

View File

@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Godot.SourceGene
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.SourceGenerators.Tests", "GFramework.SourceGenerators.Tests\GFramework.SourceGenerators.Tests.csproj", "{BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.SourceGenerators.Tests", "GFramework.SourceGenerators.Tests\GFramework.SourceGenerators.Tests.csproj", "{BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.SourceGenerators.Common", "GFramework.SourceGenerators.Common\GFramework.SourceGenerators.Common.csproj", "{B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -60,5 +62,9 @@ Global
{BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Debug|Any CPU.Build.0 = Debug|Any CPU {BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Release|Any CPU.ActiveCfg = Release|Any CPU {BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Release|Any CPU.Build.0 = Release|Any CPU {BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Release|Any CPU.Build.0 = Release|Any CPU
{B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal