Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/KubeOps.Generator/Generators/AutoGeneratedSyntaxTrivia.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace KubeOps.Generator.Generators;

public static class AutoGeneratedSyntaxTrivia
{
public static readonly SyntaxTrivia Instance =
SyntaxFactory.Comment(
"""
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
""");
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public void Execute(GeneratorExecutionContext context)
var declaration = CompilationUnit()
.WithUsings(
List(
new List<UsingDirectiveSyntax> { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), }))
new List<UsingDirectiveSyntax>
{
UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")),
}))
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("ControllerRegistrations")
.WithModifiers(TokenList(
Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
Expand Down Expand Up @@ -80,6 +84,6 @@ public void Execute(GeneratorExecutionContext context)

context.AddSource(
"ControllerRegistrations.g.cs",
SourceText.From(declaration.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
SourceText.From(declaration.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void Execute(GeneratorExecutionContext context)
UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")),
UsingDirective(IdentifierName("KubeOps.Abstractions.Entities")),
}))
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("EntityDefinitions")
.WithModifiers(TokenList(
Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
Expand Down Expand Up @@ -83,6 +84,6 @@ public void Execute(GeneratorExecutionContext context)

context.AddSource(
"EntityDefinitions.g.cs",
SourceText.From(declaration.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
SourceText.From(declaration.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
}
}
27 changes: 22 additions & 5 deletions src/KubeOps.Generator/Generators/EntityInitializerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ public void Execute(GeneratorExecutionContext context)
ns.Add(FileScopedNamespaceDeclaration(IdentifierName(symbol.ContainingNamespace.ToDisplayString())));
}

var partialEntityInitializer = CompilationUnit()
.AddMembers(ns.ToArray())
var partialEntityInitializer = CompilationUnit();

if (ns.Count > 0)
{
partialEntityInitializer = partialEntityInitializer
.AddMembers(ns.ToArray())
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance);
}

partialEntityInitializer = partialEntityInitializer
.AddMembers(ClassDeclaration(entity.Class.Identifier)
.WithModifiers(entity.Class.Modifiers)
.AddMembers(ConstructorDeclaration(entity.Class.Identifier)
Expand All @@ -69,12 +77,20 @@ public void Execute(GeneratorExecutionContext context)
IdentifierName("Kind"),
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(entity.Kind))))))))
Literal(entity.Kind))))))));

if (ns.Count == 0)
{
partialEntityInitializer = partialEntityInitializer
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance);
}

partialEntityInitializer = partialEntityInitializer
.NormalizeWhitespace();

context.AddSource(
$"{entity.Class.Identifier}.init.g.cs",
SourceText.From(partialEntityInitializer.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
SourceText.From(partialEntityInitializer.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
}

// for each NON partial entity, generate a method extension that initializes the ApiVersion and Kind.
Expand Down Expand Up @@ -128,10 +144,11 @@ m is ConstructorDeclarationSyntax
SyntaxKind.StringLiteralExpression,
Literal(e.Entity.Kind)))),
ReturnStatement(IdentifierName("entity")))))))))
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
.NormalizeWhitespace();

context.AddSource(
"EntityInitializer.g.cs",
SourceText.From(staticInitializers.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
SourceText.From(staticInitializers.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void Execute(GeneratorExecutionContext context)
.WithUsings(
List(
new List<UsingDirectiveSyntax> { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), }))
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("FinalizerRegistrations")
.WithModifiers(TokenList(
Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
Expand Down Expand Up @@ -103,7 +104,7 @@ public void Execute(GeneratorExecutionContext context)

context.AddSource(
"FinalizerRegistrations.g.cs",
SourceText.From(declaration.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
SourceText.From(declaration.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
}

private static string FinalizerName((ClassDeclarationSyntax Finalizer, AttributedEntity Entity) finalizer)
Expand Down
3 changes: 2 additions & 1 deletion src/KubeOps.Generator/Generators/OperatorBuilderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void Execute(GeneratorExecutionContext context)
.WithUsings(
List(
new List<UsingDirectiveSyntax> { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), }))
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("OperatorBuilderExtensions")
.WithModifiers(TokenList(
Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
Expand Down Expand Up @@ -55,6 +56,6 @@ public void Execute(GeneratorExecutionContext context)

context.AddSource(
"OperatorBuilder.g.cs",
SourceText.From(declaration.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
SourceText.From(declaration.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public class ControllerRegistrationGeneratorTest
{
[Theory]
[InlineData("", """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;

public static class ControllerRegistrations
Expand All @@ -30,6 +34,10 @@ public class V1TestEntityController : IEntityController<V1TestEntity>
{
}
""", """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;

public static class ControllerRegistrations
Expand Down
12 changes: 12 additions & 0 deletions test/KubeOps.Generator.Test/EntityDefinitionGenerator.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public class EntityDefinitionGeneratorTest
{
[Theory]
[InlineData("", """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;
using KubeOps.Abstractions.Entities;

Expand All @@ -23,6 +27,10 @@ public class V1TestEntity : IKubernetesObject<V1ObjectMeta>
{
}
""", """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;
using KubeOps.Abstractions.Entities;

Expand All @@ -42,6 +50,10 @@ public class V1AnotherEntity : IKubernetesObject<V1ObjectMeta>
{
}
""", """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;
using KubeOps.Abstractions.Entities;

Expand Down
36 changes: 36 additions & 0 deletions test/KubeOps.Generator.Test/EntityInitializerGenerator.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public void Should_Generate_Empty_Initializer_Without_Input()
{
var inputCompilation = string.Empty.CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
public static class EntityInitializer
{
}
Expand Down Expand Up @@ -42,6 +46,10 @@ public class V2TestEntity : IKubernetesObject<V1ObjectMeta>
}
""".CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
public static class EntityInitializer
{
public static global::V1TestEntity Initialize(this global::V1TestEntity entity)
Expand Down Expand Up @@ -81,6 +89,10 @@ public class V1ConfigMap : IKubernetesObject<V1ObjectMeta>
}
""".CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
public static class EntityInitializer
{
public static global::V1ConfigMap Initialize(this global::V1ConfigMap entity)
Expand Down Expand Up @@ -113,6 +125,10 @@ public V1TestEntity(){}
}
""".CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
public static class EntityInitializer
{
public static global::V1TestEntity Initialize(this global::V1TestEntity entity)
Expand Down Expand Up @@ -144,6 +160,10 @@ public partial class V1TestEntity : IKubernetesObject<V1ObjectMeta>
}
""".CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
public static class EntityInitializer
{
}
Expand All @@ -170,6 +190,10 @@ public partial class V1TestEntity : IKubernetesObject<V1ObjectMeta>
}
""".CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
namespace Foo.Bar;
public partial class V1TestEntity
{
Expand Down Expand Up @@ -206,6 +230,10 @@ public partial class V1TestEntity : IKubernetesObject<V1ObjectMeta>
}
""".CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
namespace Foo.Bar.Baz;
public partial class V1TestEntity
{
Expand Down Expand Up @@ -236,6 +264,10 @@ public partial class V1TestEntity : IKubernetesObject<V1ObjectMeta>
}
""".CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
public partial class V1TestEntity
{
public V1TestEntity()
Expand Down Expand Up @@ -266,6 +298,10 @@ public V1TestEntity(string name){}
}
""".CreateCompilation();
var expectedResult = """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
public partial class V1TestEntity
{
public V1TestEntity()
Expand Down
12 changes: 12 additions & 0 deletions test/KubeOps.Generator.Test/FinalizerRegistrationGenerator.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public class FinalizerRegistrationGeneratorTest
{
[Theory]
[InlineData("", """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;

public static class FinalizerRegistrations
Expand All @@ -30,6 +34,10 @@ public class V1TestEntityFinalizer : IEntityFinalizer<V1TestEntity>
{
}
""", """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;

public static class FinalizerRegistrations
Expand All @@ -56,6 +64,10 @@ public class V1TestEntityCleanupOtherResourcesSuchThatThisFinalizerHasAVeryLongN
{
}
""", """
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;

public static class FinalizerRegistrations
Expand Down
4 changes: 4 additions & 0 deletions test/KubeOps.Generator.Test/OperatorBuilderGenerator.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public void Should_Generate_Correct_Code()
var inputCompilation = string.Empty.CreateCompilation();
var expectedResult =
"""
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using KubeOps.Abstractions.Builder;

public static class OperatorBuilderExtensions
Expand Down
Loading