|
| 1 | +using System.Text; |
| 2 | + |
| 3 | +using KubeOps.Generator.SyntaxReceiver; |
| 4 | + |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 8 | +using Microsoft.CodeAnalysis.Text; |
| 9 | + |
| 10 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 11 | + |
| 12 | +namespace KubeOps.Generator.Generators; |
| 13 | + |
| 14 | +[Generator] |
| 15 | +internal class EntityInitializerGenerator : ISourceGenerator |
| 16 | +{ |
| 17 | + public void Initialize(GeneratorInitializationContext context) |
| 18 | + { |
| 19 | + context.RegisterForSyntaxNotifications(() => new KubernetesEntitySyntaxReceiver()); |
| 20 | + } |
| 21 | + |
| 22 | + public void Execute(GeneratorExecutionContext context) |
| 23 | + { |
| 24 | + if (context.SyntaxContextReceiver is not KubernetesEntitySyntaxReceiver receiver) |
| 25 | + { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + // for each partial defined entity, create a partial class that |
| 30 | + // introduces a default constructor that initializes the ApiVersion and Kind. |
| 31 | + // But only, if there is no default constructor defined. |
| 32 | + foreach (var entity in receiver.Entities |
| 33 | + .Where(e => e.Class.Modifiers.Any(SyntaxKind.PartialKeyword)) |
| 34 | + .Where(e => !e.Class.Members.Any(m => m is ConstructorDeclarationSyntax |
| 35 | + { |
| 36 | + ParameterList.Parameters.Count: 0, |
| 37 | + }))) |
| 38 | + { |
| 39 | + var symbol = context.Compilation |
| 40 | + .GetSemanticModel(entity.Class.SyntaxTree) |
| 41 | + .GetDeclaredSymbol(entity.Class)!; |
| 42 | + |
| 43 | + var ns = new List<MemberDeclarationSyntax>(); |
| 44 | + if (!symbol.ContainingNamespace.IsGlobalNamespace) |
| 45 | + { |
| 46 | + ns.Add(FileScopedNamespaceDeclaration(IdentifierName(symbol.ContainingNamespace.ToDisplayString()))); |
| 47 | + } |
| 48 | + |
| 49 | + var partialEntityInitializer = CompilationUnit() |
| 50 | + .AddMembers(ns.ToArray()) |
| 51 | + .AddMembers(ClassDeclaration(entity.Class.Identifier) |
| 52 | + .WithModifiers(entity.Class.Modifiers) |
| 53 | + .AddMembers(ConstructorDeclaration(entity.Class.Identifier) |
| 54 | + .WithModifiers( |
| 55 | + TokenList( |
| 56 | + Token(SyntaxKind.PublicKeyword))) |
| 57 | + .WithBody( |
| 58 | + Block( |
| 59 | + ExpressionStatement( |
| 60 | + AssignmentExpression( |
| 61 | + SyntaxKind.SimpleAssignmentExpression, |
| 62 | + IdentifierName("ApiVersion"), |
| 63 | + LiteralExpression( |
| 64 | + SyntaxKind.StringLiteralExpression, |
| 65 | + Literal($"{entity.Group}/{entity.Version}".TrimStart('/'))))), |
| 66 | + ExpressionStatement( |
| 67 | + AssignmentExpression( |
| 68 | + SyntaxKind.SimpleAssignmentExpression, |
| 69 | + IdentifierName("Kind"), |
| 70 | + LiteralExpression( |
| 71 | + SyntaxKind.StringLiteralExpression, |
| 72 | + Literal(entity.Kind)))))))) |
| 73 | + .NormalizeWhitespace(); |
| 74 | + |
| 75 | + context.AddSource( |
| 76 | + $"{entity.Class.Identifier}.init.g.cs", |
| 77 | + SourceText.From(partialEntityInitializer.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256)); |
| 78 | + } |
| 79 | + |
| 80 | + // for each NON partial entity, generate a method extension that initializes the ApiVersion and Kind. |
| 81 | + var staticInitializers = CompilationUnit() |
| 82 | + .WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("EntityInitializer") |
| 83 | + .WithModifiers(TokenList( |
| 84 | + Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) |
| 85 | + .WithMembers(List<MemberDeclarationSyntax>(receiver.Entities |
| 86 | + .Where(e => !e.Class.Modifiers.Any(SyntaxKind.PartialKeyword) || e.Class.Members.Any(m => |
| 87 | + m is ConstructorDeclarationSyntax |
| 88 | + { |
| 89 | + ParameterList.Parameters.Count: 0, |
| 90 | + })) |
| 91 | + .Select(e => (Entity: e, |
| 92 | + ClassIdentifier: context.Compilation.GetSemanticModel(e.Class.SyntaxTree) |
| 93 | + .GetDeclaredSymbol(e.Class)!.ToDisplayString(SymbolDisplayFormat |
| 94 | + .FullyQualifiedFormat))) |
| 95 | + .Select(e => |
| 96 | + MethodDeclaration( |
| 97 | + IdentifierName(e.ClassIdentifier), |
| 98 | + "Initialize") |
| 99 | + .WithModifiers( |
| 100 | + TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) |
| 101 | + .WithParameterList(ParameterList( |
| 102 | + SingletonSeparatedList( |
| 103 | + Parameter( |
| 104 | + Identifier("entity")) |
| 105 | + .WithModifiers( |
| 106 | + TokenList( |
| 107 | + Token(SyntaxKind.ThisKeyword))) |
| 108 | + .WithType(IdentifierName(e.ClassIdentifier))))) |
| 109 | + .WithBody(Block( |
| 110 | + ExpressionStatement( |
| 111 | + AssignmentExpression( |
| 112 | + SyntaxKind.SimpleAssignmentExpression, |
| 113 | + MemberAccessExpression( |
| 114 | + SyntaxKind.SimpleMemberAccessExpression, |
| 115 | + IdentifierName("entity"), |
| 116 | + IdentifierName("ApiVersion")), |
| 117 | + LiteralExpression( |
| 118 | + SyntaxKind.StringLiteralExpression, |
| 119 | + Literal($"{e.Entity.Group}/{e.Entity.Version}".TrimStart('/'))))), |
| 120 | + ExpressionStatement( |
| 121 | + AssignmentExpression( |
| 122 | + SyntaxKind.SimpleAssignmentExpression, |
| 123 | + MemberAccessExpression( |
| 124 | + SyntaxKind.SimpleMemberAccessExpression, |
| 125 | + IdentifierName("entity"), |
| 126 | + IdentifierName("Kind")), |
| 127 | + LiteralExpression( |
| 128 | + SyntaxKind.StringLiteralExpression, |
| 129 | + Literal(e.Entity.Kind)))), |
| 130 | + ReturnStatement(IdentifierName("entity"))))))))) |
| 131 | + .NormalizeWhitespace(); |
| 132 | + |
| 133 | + context.AddSource( |
| 134 | + "EntityInitializer.g.cs", |
| 135 | + SourceText.From(staticInitializers.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256)); |
| 136 | + } |
| 137 | +} |
0 commit comments