mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(source-generators): 重构命名空间引用和代码生成逻辑
- 将硬编码的命名空间路径替换为常量类中的动态引用 - 添加 PathContests 常量类统一管理框架命名空间 - 更新 LoggerGenerator 使用动态命名空间引用 - 重构 ContextAwareGenerator 生成的代码格式和命名空间引用 - 为 ContextAware 生成的属性添加 XML 文档注释 - 简化 ContextAware 生成文件的路径命名规则 - 更新测试代码以匹配新的命名空间引用方式
This commit is contained in:
parent
14c7dadee2
commit
a191228d85
@ -14,11 +14,9 @@ public class ContextAwareGeneratorTests
|
||||
/// 测试ContextAware代码生成功能
|
||||
/// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码
|
||||
/// </summary>
|
||||
/// <returns>异步任务</returns>
|
||||
[Test]
|
||||
public Task Generates_ContextAware_Code()
|
||||
{
|
||||
// 定义输入源代码,包含使用[ContextAware]特性的部分类
|
||||
const string source = """
|
||||
using System;
|
||||
|
||||
@ -35,37 +33,42 @@ public class ContextAwareGeneratorTests
|
||||
using GFramework.SourceGenerators.Abstractions.rule;
|
||||
|
||||
[ContextAware]
|
||||
public partial class MyRule: GFramework.Core.rule.IContextAware
|
||||
public partial class MyRule: GFramework.Core.Abstractions.rule.IContextAware
|
||||
{
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
const string frameworkStub = """
|
||||
namespace GFramework.Core.rule
|
||||
namespace GFramework.Core.Abstractions.rule
|
||||
{
|
||||
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 {}
|
||||
}
|
||||
""";
|
||||
// 定义期望的生成结果代码
|
||||
|
||||
const string expected = """
|
||||
// <auto-generated/>
|
||||
#nullable enable
|
||||
|
||||
namespace TestApp;
|
||||
|
||||
partial class MyRule
|
||||
{
|
||||
protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!;
|
||||
void GFramework.Core.rule.IContextAware.SetContext(
|
||||
GFramework.Core.architecture.IArchitectureContext context)
|
||||
/// <summary>
|
||||
/// 自动注入的架构上下文
|
||||
/// </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;
|
||||
}
|
||||
@ -74,12 +77,12 @@ public class ContextAwareGeneratorTests
|
||||
|
||||
Assert.DoesNotThrowAsync(async () =>
|
||||
{
|
||||
// 执行源代码生成器测试
|
||||
await GeneratorTest<ContextAwareGenerator>.RunAsync(
|
||||
source + "\n" + frameworkStub,
|
||||
("MyRule.ContextAware.g.cs", expected)
|
||||
);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@ -100,7 +103,7 @@ public class ContextAwareGeneratorTests
|
||||
namespace TestApp
|
||||
{
|
||||
using GFramework.SourceGenerators.Abstractions.rule;
|
||||
using GFramework.Core.rule;
|
||||
using GFramework.Core.Abstractions.rule;
|
||||
|
||||
// 间接接口:继承自 IContextAware
|
||||
public interface IMyRuleContextAware : IContextAware
|
||||
@ -115,15 +118,15 @@ public class ContextAwareGeneratorTests
|
||||
""";
|
||||
|
||||
const string frameworkStub = """
|
||||
namespace GFramework.Core.rule
|
||||
namespace GFramework.Core.Abstractions.rule
|
||||
{
|
||||
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 {}
|
||||
}
|
||||
@ -132,18 +135,24 @@ public class ContextAwareGeneratorTests
|
||||
const string expected = """
|
||||
// <auto-generated/>
|
||||
#nullable enable
|
||||
|
||||
namespace TestApp;
|
||||
|
||||
partial class MyRule
|
||||
{
|
||||
protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!;
|
||||
void GFramework.Core.rule.IContextAware.SetContext(
|
||||
GFramework.Core.architecture.IArchitectureContext context)
|
||||
/// <summary>
|
||||
/// 自动注入的架构上下文
|
||||
/// </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;
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
Assert.DoesNotThrowAsync(async () =>
|
||||
{
|
||||
await GeneratorTest<ContextAwareGenerator>.RunAsync(
|
||||
@ -151,6 +160,7 @@ public class ContextAwareGeneratorTests
|
||||
("MyRule.ContextAware.g.cs", expected)
|
||||
);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@ -2,5 +2,8 @@
|
||||
|
||||
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";
|
||||
}
|
||||
@ -3,6 +3,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using GFramework.SourceGenerators.Abstractions.logging;
|
||||
using GFramework.SourceGenerators.Common.generator;
|
||||
using GFramework.SourceGenerators.constants;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
@ -67,7 +68,8 @@ public sealed class LoggerGenerator : AttributeClassGeneratorBase
|
||||
|
||||
var sb = new StringBuilder();
|
||||
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)
|
||||
{
|
||||
|
||||
@ -4,6 +4,7 @@ using System.Text;
|
||||
using GFramework.Core.Abstractions.rule;
|
||||
using GFramework.SourceGenerators.Abstractions.rule;
|
||||
using GFramework.SourceGenerators.Common.generator;
|
||||
using GFramework.SourceGenerators.constants;
|
||||
using GFramework.SourceGenerators.diagnostics;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
@ -59,6 +60,7 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("// <auto-generated/>");
|
||||
sb.AppendLine("#nullable enable");
|
||||
sb.AppendLine();
|
||||
|
||||
if (ns is not null)
|
||||
{
|
||||
@ -68,18 +70,30 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase
|
||||
|
||||
sb.AppendLine($"partial class {symbol.Name}");
|
||||
sb.AppendLine("{");
|
||||
|
||||
// 属性
|
||||
sb.AppendLine(" /// <summary>");
|
||||
sb.AppendLine(" /// 自动注入的架构上下文");
|
||||
sb.AppendLine(" /// </summary>");
|
||||
sb.AppendLine(
|
||||
" protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!;");
|
||||
sb.AppendLine(" void GFramework.Core.rule.IContextAware.SetContext(");
|
||||
sb.AppendLine(" GFramework.Core.architecture.IArchitectureContext context)");
|
||||
$" protected {PathContests.CoreAbstractionsNamespace}.architecture.IArchitectureContext Context {{ get; private set; }} = null!;");
|
||||
sb.AppendLine();
|
||||
|
||||
// 方法
|
||||
sb.AppendLine(
|
||||
$" void {PathContests.CoreAbstractionsNamespace}.rule.IContextAware.SetContext(");
|
||||
sb.AppendLine(
|
||||
$" {PathContests.CoreAbstractionsNamespace}.architecture.IArchitectureContext context)");
|
||||
sb.AppendLine(" {");
|
||||
sb.AppendLine(" Context = context;");
|
||||
sb.AppendLine(" }");
|
||||
|
||||
sb.AppendLine("}");
|
||||
|
||||
return sb.ToString().TrimEnd();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 自定义生成文件名
|
||||
/// </summary>
|
||||
@ -87,6 +101,6 @@ public sealed class ContextAwareGenerator : AttributeClassGeneratorBase
|
||||
{
|
||||
// 包含命名空间和生成器类名路径
|
||||
return
|
||||
$@"GFramework.SourceGenerators\GFramework.SourceGenerators.rule.ContextAwareGenerator\{symbol.Name}.ContextAware.g.cs";
|
||||
$"{symbol.Name}.ContextAware.g.cs";
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user