From e9f1cb2ae9cf5ba04ad76584664b8e2eb2aed9b3 Mon Sep 17 00:00:00 2001 From: Marcus Kimpenhaus Date: Fri, 28 Feb 2025 11:02:03 +0100 Subject: [PATCH] Add auto-generated comment to generated code files Introduced a reusable syntax trivia for auto-generated comments and applied it consistently in all relevant code generators. This ensures clearer distinction between manually written and tool-generated code, reducing potential confusion when modifying files. Updated test cases accordingly to reflect these changes. --- .../Generators/AutoGeneratedSyntaxTrivia.cs | 16 +++++++++ .../ControllerRegistrationGenerator.cs | 8 +++-- .../Generators/EntityDefinitionGenerator.cs | 3 +- .../Generators/EntityInitializerGenerator.cs | 27 +++++++++++--- .../FinalizerRegistrationGenerator.cs | 3 +- .../Generators/OperatorBuilderGenerator.cs | 3 +- .../ControllerRegistrationGenerator.Test.cs | 8 +++++ .../EntityDefinitionGenerator.Test.cs | 12 +++++++ .../EntityInitializerGenerator.Test.cs | 36 +++++++++++++++++++ .../FinalizerRegistrationGenerator.Test.cs | 12 +++++++ .../OperatorBuilderGenerator.Test.cs | 4 +++ 11 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 src/KubeOps.Generator/Generators/AutoGeneratedSyntaxTrivia.cs diff --git a/src/KubeOps.Generator/Generators/AutoGeneratedSyntaxTrivia.cs b/src/KubeOps.Generator/Generators/AutoGeneratedSyntaxTrivia.cs new file mode 100644 index 00000000..69a1db67 --- /dev/null +++ b/src/KubeOps.Generator/Generators/AutoGeneratedSyntaxTrivia.cs @@ -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( + """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // + """); +} diff --git a/src/KubeOps.Generator/Generators/ControllerRegistrationGenerator.cs b/src/KubeOps.Generator/Generators/ControllerRegistrationGenerator.cs index 6792cde2..8ccf5445 100644 --- a/src/KubeOps.Generator/Generators/ControllerRegistrationGenerator.cs +++ b/src/KubeOps.Generator/Generators/ControllerRegistrationGenerator.cs @@ -32,7 +32,11 @@ public void Execute(GeneratorExecutionContext context) var declaration = CompilationUnit() .WithUsings( List( - new List { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), })) + new List + { + UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), + })) + .WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance) .WithMembers(SingletonList(ClassDeclaration("ControllerRegistrations") .WithModifiers(TokenList( Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) @@ -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)); } } diff --git a/src/KubeOps.Generator/Generators/EntityDefinitionGenerator.cs b/src/KubeOps.Generator/Generators/EntityDefinitionGenerator.cs index a643dca3..bbbab123 100644 --- a/src/KubeOps.Generator/Generators/EntityDefinitionGenerator.cs +++ b/src/KubeOps.Generator/Generators/EntityDefinitionGenerator.cs @@ -34,6 +34,7 @@ public void Execute(GeneratorExecutionContext context) UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), UsingDirective(IdentifierName("KubeOps.Abstractions.Entities")), })) + .WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance) .WithMembers(SingletonList(ClassDeclaration("EntityDefinitions") .WithModifiers(TokenList( Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) @@ -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)); } } diff --git a/src/KubeOps.Generator/Generators/EntityInitializerGenerator.cs b/src/KubeOps.Generator/Generators/EntityInitializerGenerator.cs index d1b1fbdf..ca48ac71 100644 --- a/src/KubeOps.Generator/Generators/EntityInitializerGenerator.cs +++ b/src/KubeOps.Generator/Generators/EntityInitializerGenerator.cs @@ -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) @@ -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. @@ -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)); } } diff --git a/src/KubeOps.Generator/Generators/FinalizerRegistrationGenerator.cs b/src/KubeOps.Generator/Generators/FinalizerRegistrationGenerator.cs index 8bef2d6b..f74a5638 100644 --- a/src/KubeOps.Generator/Generators/FinalizerRegistrationGenerator.cs +++ b/src/KubeOps.Generator/Generators/FinalizerRegistrationGenerator.cs @@ -41,6 +41,7 @@ public void Execute(GeneratorExecutionContext context) .WithUsings( List( new List { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), })) + .WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance) .WithMembers(SingletonList(ClassDeclaration("FinalizerRegistrations") .WithModifiers(TokenList( Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) @@ -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) diff --git a/src/KubeOps.Generator/Generators/OperatorBuilderGenerator.cs b/src/KubeOps.Generator/Generators/OperatorBuilderGenerator.cs index 4bea12db..4dbb044d 100644 --- a/src/KubeOps.Generator/Generators/OperatorBuilderGenerator.cs +++ b/src/KubeOps.Generator/Generators/OperatorBuilderGenerator.cs @@ -22,6 +22,7 @@ public void Execute(GeneratorExecutionContext context) .WithUsings( List( new List { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), })) + .WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance) .WithMembers(SingletonList(ClassDeclaration("OperatorBuilderExtensions") .WithModifiers(TokenList( Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) @@ -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)); } } diff --git a/test/KubeOps.Generator.Test/ControllerRegistrationGenerator.Test.cs b/test/KubeOps.Generator.Test/ControllerRegistrationGenerator.Test.cs index 63ec20cc..43999513 100644 --- a/test/KubeOps.Generator.Test/ControllerRegistrationGenerator.Test.cs +++ b/test/KubeOps.Generator.Test/ControllerRegistrationGenerator.Test.cs @@ -10,6 +10,10 @@ public class ControllerRegistrationGeneratorTest { [Theory] [InlineData("", """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; public static class ControllerRegistrations @@ -30,6 +34,10 @@ public class V1TestEntityController : IEntityController { } """, """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; public static class ControllerRegistrations diff --git a/test/KubeOps.Generator.Test/EntityDefinitionGenerator.Test.cs b/test/KubeOps.Generator.Test/EntityDefinitionGenerator.Test.cs index d9fa67b2..556ebb9d 100644 --- a/test/KubeOps.Generator.Test/EntityDefinitionGenerator.Test.cs +++ b/test/KubeOps.Generator.Test/EntityDefinitionGenerator.Test.cs @@ -10,6 +10,10 @@ public class EntityDefinitionGeneratorTest { [Theory] [InlineData("", """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; using KubeOps.Abstractions.Entities; @@ -23,6 +27,10 @@ public class V1TestEntity : IKubernetesObject { } """, """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; using KubeOps.Abstractions.Entities; @@ -42,6 +50,10 @@ public class V1AnotherEntity : IKubernetesObject { } """, """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; using KubeOps.Abstractions.Entities; diff --git a/test/KubeOps.Generator.Test/EntityInitializerGenerator.Test.cs b/test/KubeOps.Generator.Test/EntityInitializerGenerator.Test.cs index b719e951..9b849e80 100644 --- a/test/KubeOps.Generator.Test/EntityInitializerGenerator.Test.cs +++ b/test/KubeOps.Generator.Test/EntityInitializerGenerator.Test.cs @@ -13,6 +13,10 @@ public void Should_Generate_Empty_Initializer_Without_Input() { var inputCompilation = string.Empty.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // public static class EntityInitializer { } @@ -42,6 +46,10 @@ public class V2TestEntity : IKubernetesObject } """.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // public static class EntityInitializer { public static global::V1TestEntity Initialize(this global::V1TestEntity entity) @@ -81,6 +89,10 @@ public class V1ConfigMap : IKubernetesObject } """.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // public static class EntityInitializer { public static global::V1ConfigMap Initialize(this global::V1ConfigMap entity) @@ -113,6 +125,10 @@ public V1TestEntity(){} } """.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // public static class EntityInitializer { public static global::V1TestEntity Initialize(this global::V1TestEntity entity) @@ -144,6 +160,10 @@ public partial class V1TestEntity : IKubernetesObject } """.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // public static class EntityInitializer { } @@ -170,6 +190,10 @@ public partial class V1TestEntity : IKubernetesObject } """.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // namespace Foo.Bar; public partial class V1TestEntity { @@ -206,6 +230,10 @@ public partial class V1TestEntity : IKubernetesObject } """.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // namespace Foo.Bar.Baz; public partial class V1TestEntity { @@ -236,6 +264,10 @@ public partial class V1TestEntity : IKubernetesObject } """.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // public partial class V1TestEntity { public V1TestEntity() @@ -266,6 +298,10 @@ public V1TestEntity(string name){} } """.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // public partial class V1TestEntity { public V1TestEntity() diff --git a/test/KubeOps.Generator.Test/FinalizerRegistrationGenerator.Test.cs b/test/KubeOps.Generator.Test/FinalizerRegistrationGenerator.Test.cs index da88e296..debc46cd 100644 --- a/test/KubeOps.Generator.Test/FinalizerRegistrationGenerator.Test.cs +++ b/test/KubeOps.Generator.Test/FinalizerRegistrationGenerator.Test.cs @@ -10,6 +10,10 @@ public class FinalizerRegistrationGeneratorTest { [Theory] [InlineData("", """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; public static class FinalizerRegistrations @@ -30,6 +34,10 @@ public class V1TestEntityFinalizer : IEntityFinalizer { } """, """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; public static class FinalizerRegistrations @@ -56,6 +64,10 @@ public class V1TestEntityCleanupOtherResourcesSuchThatThisFinalizerHasAVeryLongN { } """, """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; public static class FinalizerRegistrations diff --git a/test/KubeOps.Generator.Test/OperatorBuilderGenerator.Test.cs b/test/KubeOps.Generator.Test/OperatorBuilderGenerator.Test.cs index 5da30164..94340498 100644 --- a/test/KubeOps.Generator.Test/OperatorBuilderGenerator.Test.cs +++ b/test/KubeOps.Generator.Test/OperatorBuilderGenerator.Test.cs @@ -14,6 +14,10 @@ public void Should_Generate_Correct_Code() var inputCompilation = string.Empty.CreateCompilation(); var expectedResult = """ + // + // This code was generated by a tool. + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // using KubeOps.Abstractions.Builder; public static class OperatorBuilderExtensions