GeWuYou fb14d7122c docs(style): 更新文档中的命名空间导入格式
- 将所有小写的命名空间导入更正为首字母大写格式
- 统一 GFramework 框架的命名空间引用规范
- 修复 core、ecs、godot 等模块的命名空间导入错误
- 标准化文档示例代码中的 using 语句格式
- 确保所有文档中的命名空间引用保持一致性
- 更新 global using 语句以匹配正确的命名空间格式
2026-03-10 07:18:49 +08:00

100 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text;
using GFramework.SourceGenerators.Abstractions.Logging;
using GFramework.SourceGenerators.Common.Constants;
using GFramework.SourceGenerators.Common.Extensions;
using GFramework.SourceGenerators.Common.Generator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace GFramework.SourceGenerators.Logging;
/// <summary>
/// 日志生成器用于为标记了LogAttribute的类自动生成日志字段
/// </summary>
[Generator]
public sealed class LoggerGenerator : TypeAttributeClassGeneratorBase
{
/// <summary>
/// 获取属性元数据的完整名称,用于标识日志属性的完全限定名
/// </summary>
protected override Type AttributeType => typeof(LogAttribute);
/// <summary>
/// 用于语法快速筛选
/// </summary>
protected override string AttributeShortNameWithoutSuffix => "Log";
/// <summary>
/// 对类进行额外语义校验(可选)
/// </summary>
protected override bool ValidateSymbol(
SourceProductionContext context,
Compilation compilation,
ClassDeclarationSyntax syntax,
INamedTypeSymbol symbol,
AttributeData attr)
{
// 可以加自定义规则,比如确保类是 public 或实现某接口
return true;
}
/// <summary>
/// 生成源代码
/// </summary>
/// <param name="context">源生产上下文</param>
/// <param name="compilation">编译对象</param>
/// <param name="symbol">命名类型符号</param>
/// <param name="attr">属性数据</param>
/// <returns>生成的源代码字符串</returns>
protected override string Generate(
SourceProductionContext context,
Compilation compilation,
INamedTypeSymbol symbol,
AttributeData attr)
{
var ns = symbol.GetNamespace();
var className = symbol.GetFullClassName();
var typeKind = symbol.ResolveTypeKind();
var generics = symbol.ResolveGenerics();
var logName = attr.GetFirstCtorString(className);
var fieldName = attr.GetNamedArgument("FieldName", "_log");
var access = attr.GetNamedArgument("AccessModifier", "private");
var isStatic = attr.GetNamedArgument("IsStatic", true);
var staticKeyword = isStatic ? "static " : "";
var sb = new StringBuilder()
.AppendLine("// <auto-generated />")
.AppendLine($"using {PathContests.CoreAbstractionsNamespace}.Logging;")
.AppendLine($"using {PathContests.CoreNamespace}.Logging;");
if (ns is not null)
sb.AppendLine()
.AppendLine($"namespace {ns};");
sb.AppendLine()
.AppendLine($"partial {typeKind} {className}{generics.Parameters}");
foreach (var c in generics.Constraints)
sb.AppendLine($" {c}");
sb.AppendLine("{")
.AppendLine(" /// <summary>Auto-generated logger</summary>")
.AppendLine(
$" {access} {staticKeyword}readonly ILogger {fieldName} = " +
$"LoggerFactoryResolver.Provider.CreateLogger(\"{logName}\");")
.AppendLine("}");
return sb.ToString();
}
/// <summary>
/// 可以自定义生成文件名
/// </summary>
protected override string GetHintName(INamedTypeSymbol symbol)
{
return $"{symbol.Name}.Logger.g.cs";
}
}