mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 03:04:29 +08:00
- 添加AttributeData扩展方法用于获取命名参数和构造函数参数 - 引入GenericInfo记录结构体处理泛型信息 - 将INamedTypeSymbol扩展方法转换为扩展方法语法 - 添加ResolveGenerics方法解析泛型参数和约束条件 - 简化LoggerGenerator中的参数解析逻辑 - 移除不再需要的GetNamedArg私有方法 - 优化代码可读性和维护性
103 lines
3.4 KiB
C#
103 lines
3.4 KiB
C#
using System;
|
||
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";
|
||
}
|
||
} |