refactor(source-generators): 重构命名空间引用和代码生成逻辑

- 将硬编码的命名空间路径替换为常量类中的动态引用
- 添加 PathContests 常量类统一管理框架命名空间
- 更新 LoggerGenerator 使用动态命名空间引用
- 重构 ContextAwareGenerator 生成的代码格式和命名空间引用
- 为 ContextAware 生成的属性添加 XML 文档注释
- 简化 ContextAware 生成文件的路径命名规则
- 更新测试代码以匹配新的命名空间引用方式
This commit is contained in:
GwWuYou 2025-12-28 12:39:11 +08:00
parent 14c7dadee2
commit a191228d85
4 changed files with 53 additions and 24 deletions

View File

@ -14,11 +14,9 @@ public class ContextAwareGeneratorTests
/// 测试ContextAware代码生成功能 /// 测试ContextAware代码生成功能
/// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码 /// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码
/// </summary> /// </summary>
/// <returns>异步任务</returns>
[Test] [Test]
public Task Generates_ContextAware_Code() public Task Generates_ContextAware_Code()
{ {
// 定义输入源代码,包含使用[ContextAware]特性的部分类
const string source = """ const string source = """
using System; using System;
@ -35,37 +33,42 @@ public class ContextAwareGeneratorTests
using GFramework.SourceGenerators.Abstractions.rule; using GFramework.SourceGenerators.Abstractions.rule;
[ContextAware] [ContextAware]
public partial class MyRule: GFramework.Core.rule.IContextAware public partial class MyRule: GFramework.Core.Abstractions.rule.IContextAware
{ {
} }
} }
"""; """;
const string frameworkStub = """ const string frameworkStub = """
namespace GFramework.Core.rule namespace GFramework.Core.Abstractions.rule
{ {
public interface IContextAware public interface IContextAware
{ {
void SetContext(GFramework.Core.architecture.IArchitectureContext context); void SetContext(GFramework.Core.Abstractions.architecture.IArchitectureContext context);
} }
} }
namespace GFramework.Core.architecture namespace GFramework.Core.Abstractions.architecture
{ {
public interface IArchitectureContext {} public interface IArchitectureContext {}
} }
"""; """;
// 定义期望的生成结果代码
const string expected = """ const string expected = """
// <auto-generated/> // <auto-generated/>
#nullable enable #nullable enable
namespace TestApp; namespace TestApp;
partial class MyRule partial class MyRule
{ {
protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!; /// <summary>
void GFramework.Core.rule.IContextAware.SetContext( /// 自动注入的架构上下文
GFramework.Core.architecture.IArchitectureContext context) /// </summary>
protected GFramework.Core.Abstractions.architecture.IArchitectureContext Context { get; private set; } = null!;
void GFramework.Core.Abstractions.rule.IContextAware.SetContext(
GFramework.Core.Abstractions.architecture.IArchitectureContext context)
{ {
Context = context; Context = context;
} }
@ -74,12 +77,12 @@ public class ContextAwareGeneratorTests
Assert.DoesNotThrowAsync(async () => Assert.DoesNotThrowAsync(async () =>
{ {
// 执行源代码生成器测试
await GeneratorTest<ContextAwareGenerator>.RunAsync( await GeneratorTest<ContextAwareGenerator>.RunAsync(
source + "\n" + frameworkStub, source + "\n" + frameworkStub,
("MyRule.ContextAware.g.cs", expected) ("MyRule.ContextAware.g.cs", expected)
); );
}); });
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -100,7 +103,7 @@ public class ContextAwareGeneratorTests
namespace TestApp namespace TestApp
{ {
using GFramework.SourceGenerators.Abstractions.rule; using GFramework.SourceGenerators.Abstractions.rule;
using GFramework.Core.rule; using GFramework.Core.Abstractions.rule;
// 间接接口:继承自 IContextAware // 间接接口:继承自 IContextAware
public interface IMyRuleContextAware : IContextAware public interface IMyRuleContextAware : IContextAware
@ -115,15 +118,15 @@ public class ContextAwareGeneratorTests
"""; """;
const string frameworkStub = """ const string frameworkStub = """
namespace GFramework.Core.rule namespace GFramework.Core.Abstractions.rule
{ {
public interface IContextAware public interface IContextAware
{ {
void SetContext(GFramework.Core.architecture.IArchitectureContext context); void SetContext(GFramework.Core.Abstractions.architecture.IArchitectureContext context);
} }
} }
namespace GFramework.Core.architecture namespace GFramework.Core.Abstractions.architecture
{ {
public interface IArchitectureContext {} public interface IArchitectureContext {}
} }
@ -132,18 +135,24 @@ public class ContextAwareGeneratorTests
const string expected = """ const string expected = """
// <auto-generated/> // <auto-generated/>
#nullable enable #nullable enable
namespace TestApp; namespace TestApp;
partial class MyRule partial class MyRule
{ {
protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!; /// <summary>
void GFramework.Core.rule.IContextAware.SetContext( /// 自动注入的架构上下文
GFramework.Core.architecture.IArchitectureContext context) /// </summary>
protected GFramework.Core.Abstractions.architecture.IArchitectureContext Context { get; private set; } = null!;
void GFramework.Core.Abstractions.rule.IContextAware.SetContext(
GFramework.Core.Abstractions.architecture.IArchitectureContext context)
{ {
Context = context; Context = context;
} }
} }
"""; """;
Assert.DoesNotThrowAsync(async () => Assert.DoesNotThrowAsync(async () =>
{ {
await GeneratorTest<ContextAwareGenerator>.RunAsync( await GeneratorTest<ContextAwareGenerator>.RunAsync(
@ -151,6 +160,7 @@ public class ContextAwareGeneratorTests
("MyRule.ContextAware.g.cs", expected) ("MyRule.ContextAware.g.cs", expected)
); );
}); });
return Task.CompletedTask; return Task.CompletedTask;
} }
} }

View File

@ -2,5 +2,8 @@
public static class PathContests public static class PathContests
{ {
public const string RootAbstractionsPath = "GFramework.SourceGenerators.Abstractions"; public const string BaseNamespace = "GFramework";
public const string CoreNamespace = $"{BaseNamespace}.Core";
public const string SourceGeneratorsAbstractionsPath = $"{BaseNamespace}.SourceGenerators.Abstractions";
public const string CoreAbstractionsNamespace = $"{CoreNamespace}.Abstractions";
} }

View File

@ -3,6 +3,7 @@ using System.Linq;
using System.Text; using System.Text;
using GFramework.SourceGenerators.Abstractions.logging; using GFramework.SourceGenerators.Abstractions.logging;
using GFramework.SourceGenerators.Common.generator; using GFramework.SourceGenerators.Common.generator;
using GFramework.SourceGenerators.constants;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -67,7 +68,8 @@ public sealed class LoggerGenerator : AttributeClassGeneratorBase
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine("// <auto-generated />"); sb.AppendLine("// <auto-generated />");
sb.AppendLine("using GFramework.Core.logging;"); sb.AppendLine($"using {PathContests.CoreAbstractionsNamespace}.logging;");
sb.AppendLine($"using {PathContests.CoreNamespace}.logging;");
if (ns is not null) if (ns is not null)
{ {

View File

@ -4,6 +4,7 @@ using System.Text;
using GFramework.Core.Abstractions.rule; using GFramework.Core.Abstractions.rule;
using GFramework.SourceGenerators.Abstractions.rule; using GFramework.SourceGenerators.Abstractions.rule;
using GFramework.SourceGenerators.Common.generator; using GFramework.SourceGenerators.Common.generator;
using GFramework.SourceGenerators.constants;
using GFramework.SourceGenerators.diagnostics; using GFramework.SourceGenerators.diagnostics;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -59,6 +60,7 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine("// <auto-generated/>"); sb.AppendLine("// <auto-generated/>");
sb.AppendLine("#nullable enable"); sb.AppendLine("#nullable enable");
sb.AppendLine();
if (ns is not null) if (ns is not null)
{ {
@ -68,18 +70,30 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase
sb.AppendLine($"partial class {symbol.Name}"); sb.AppendLine($"partial class {symbol.Name}");
sb.AppendLine("{"); sb.AppendLine("{");
// 属性
sb.AppendLine(" /// <summary>");
sb.AppendLine(" /// 自动注入的架构上下文");
sb.AppendLine(" /// </summary>");
sb.AppendLine( sb.AppendLine(
" protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!;"); $" protected {PathContests.CoreAbstractionsNamespace}.architecture.IArchitectureContext Context {{ get; private set; }} = null!;");
sb.AppendLine(" void GFramework.Core.rule.IContextAware.SetContext("); sb.AppendLine();
sb.AppendLine(" GFramework.Core.architecture.IArchitectureContext context)");
// 方法
sb.AppendLine(
$" void {PathContests.CoreAbstractionsNamespace}.rule.IContextAware.SetContext(");
sb.AppendLine(
$" {PathContests.CoreAbstractionsNamespace}.architecture.IArchitectureContext context)");
sb.AppendLine(" {"); sb.AppendLine(" {");
sb.AppendLine(" Context = context;"); sb.AppendLine(" Context = context;");
sb.AppendLine(" }"); sb.AppendLine(" }");
sb.AppendLine("}"); sb.AppendLine("}");
return sb.ToString().TrimEnd(); return sb.ToString().TrimEnd();
} }
/// <summary> /// <summary>
/// 自定义生成文件名 /// 自定义生成文件名
/// </summary> /// </summary>
@ -87,6 +101,6 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase
{ {
// 包含命名空间和生成器类名路径 // 包含命名空间和生成器类名路径
return return
$@"GFramework.SourceGenerators\GFramework.SourceGenerators.rule.ContextAwareGenerator\{symbol.Name}.ContextAware.g.cs"; $"{symbol.Name}.ContextAware.g.cs";
} }
} }