mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 将 ConsoleLogger 中的 ILogger.RootLoggerName 替换为 RootLoggerName - 将 NoopLogger 中的 ILogger.RootLoggerName 替换为 RootLoggerName - 将 GodotLogger 中的 ILogger.RootLoggerName 替换为 RootLoggerName - 更新 ContextAwareGenerator 生成的文件路径格式,包含完整命名空间路径
92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using System;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using GFramework.Core.Abstractions.rule;
|
||
using GFramework.SourceGenerators.Abstractions.rule;
|
||
using GFramework.SourceGenerators.Common.generator;
|
||
using GFramework.SourceGenerators.diagnostics;
|
||
using Microsoft.CodeAnalysis;
|
||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||
|
||
namespace GFramework.SourceGenerators.rule;
|
||
|
||
[Generator]
|
||
public sealed class ContextAwareGenerator : AttributeClassGeneratorBase
|
||
{
|
||
/// <summary>
|
||
/// 使用强类型 Attribute,替代字符串
|
||
/// </summary>
|
||
protected override Type AttributeType => typeof(ContextAwareAttribute);
|
||
|
||
/// <summary>
|
||
/// 仅用于 Syntax 粗筛选
|
||
/// </summary>
|
||
protected override string AttributeShortNameWithoutSuffix => "ContextAware";
|
||
|
||
/// <summary>
|
||
/// 额外语义校验:必须实现 IContextAware
|
||
/// </summary>
|
||
protected override bool ValidateSymbol(
|
||
SourceProductionContext context,
|
||
ClassDeclarationSyntax syntax,
|
||
INamedTypeSymbol symbol,
|
||
AttributeData attr)
|
||
{
|
||
if (!symbol.AllInterfaces.Any(i =>
|
||
i.ToDisplayString() == typeof(IContextAware).FullName))
|
||
{
|
||
context.ReportDiagnostic(Diagnostic.Create(
|
||
ContextAwareDiagnostic.ClassMustImplementIContextAware,
|
||
syntax.Identifier.GetLocation(),
|
||
symbol.Name));
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成源码
|
||
/// </summary>
|
||
protected override string Generate(
|
||
INamedTypeSymbol symbol,
|
||
AttributeData attr)
|
||
{
|
||
var ns = symbol.ContainingNamespace.IsGlobalNamespace
|
||
? null
|
||
: symbol.ContainingNamespace.ToDisplayString();
|
||
|
||
var sb = new StringBuilder();
|
||
sb.AppendLine("// <auto-generated/>");
|
||
sb.AppendLine("#nullable enable");
|
||
|
||
if (ns is not null)
|
||
{
|
||
sb.AppendLine($"namespace {ns};");
|
||
sb.AppendLine();
|
||
}
|
||
|
||
sb.AppendLine($"partial class {symbol.Name}");
|
||
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)");
|
||
sb.AppendLine(" {");
|
||
sb.AppendLine(" Context = context;");
|
||
sb.AppendLine(" }");
|
||
sb.AppendLine("}");
|
||
|
||
return sb.ToString().TrimEnd();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 自定义生成文件名
|
||
/// </summary>
|
||
protected override string GetHintName(INamedTypeSymbol symbol)
|
||
{
|
||
// 包含命名空间和生成器类名路径
|
||
return
|
||
$@"GFramework.SourceGenerators\GFramework.SourceGenerators.rule.ContextAwareGenerator\{symbol.Name}.ContextAware.g.cs";
|
||
}
|
||
} |