diff --git a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs index 6bf322e..6727f4a 100644 --- a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs +++ b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs @@ -14,11 +14,9 @@ public class ContextAwareGeneratorTests /// 测试ContextAware代码生成功能 /// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码 /// - /// 异步任务 [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 = """ // #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) + /// + /// 自动注入的架构上下文 + /// + 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.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 = """ // #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) + /// + /// 自动注入的架构上下文 + /// + 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.RunAsync( @@ -151,6 +160,7 @@ public class ContextAwareGeneratorTests ("MyRule.ContextAware.g.cs", expected) ); }); + return Task.CompletedTask; } } \ No newline at end of file diff --git a/GFramework.SourceGenerators/constants/PathContests.cs b/GFramework.SourceGenerators/constants/PathContests.cs index 9814aab..cafb5b8 100644 --- a/GFramework.SourceGenerators/constants/PathContests.cs +++ b/GFramework.SourceGenerators/constants/PathContests.cs @@ -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"; } \ No newline at end of file diff --git a/GFramework.SourceGenerators/logging/LoggerGenerator.cs b/GFramework.SourceGenerators/logging/LoggerGenerator.cs index 7d50aff..81d20d5 100644 --- a/GFramework.SourceGenerators/logging/LoggerGenerator.cs +++ b/GFramework.SourceGenerators/logging/LoggerGenerator.cs @@ -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("// "); - sb.AppendLine("using GFramework.Core.logging;"); + sb.AppendLine($"using {PathContests.CoreAbstractionsNamespace}.logging;"); + sb.AppendLine($"using {PathContests.CoreNamespace}.logging;"); if (ns is not null) { diff --git a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs index 5ab523a..cacd6ad 100644 --- a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs +++ b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs @@ -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("// "); 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(" /// "); + sb.AppendLine(" /// 自动注入的架构上下文"); + sb.AppendLine(" /// "); 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(); } + /// /// 自定义生成文件名 /// @@ -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"; } } \ No newline at end of file