33 lines
1.4 KiB
C#
33 lines
1.4 KiB
C#
using System.Linq;
|
|
using System.Text;
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
namespace Polonium.Generators.Generators;
|
|
[Generator]
|
|
public class RegistryEntityGenerator : AssetProcessGenerator
|
|
{
|
|
public override void Execute(GeneratorExecutionContext context)
|
|
{
|
|
if (!(context.SyntaxReceiver is ClassSyntaxReceiver receiver))
|
|
return;
|
|
Compilation compilation = context.Compilation;
|
|
INamedTypeSymbol? regEntity = compilation.GetTypeByMetadataName("Polonium.Attributes.RegistryEntity");
|
|
foreach (var derivedClassDeclaration in receiver.Classes)
|
|
{
|
|
SemanticModel model = compilation.GetSemanticModel(derivedClassDeclaration.SyntaxTree);
|
|
if (
|
|
model.GetDeclaredSymbol(derivedClassDeclaration) is INamedTypeSymbol symbol &&
|
|
symbol.GetAttributes().Any(x => SymbolEqualityComparer.Default.Equals(x.AttributeClass, regEntity))
|
|
)
|
|
{
|
|
StringBuilder sb = new();
|
|
sb
|
|
.AppendLine("public static partial class GlobalRegistry")
|
|
.AppendLine("{")
|
|
.AppendLine($" public static {symbol.ToDisplayString()} {symbol.Name} {{ get; set; }} ")
|
|
.AppendLine("}");
|
|
context.AddSource($"RegistryEntity_{symbol.Name}.g.cs", sb.ToString());
|
|
}
|
|
}
|
|
}
|
|
} |