Skip to content

Commit 09aaef9

Browse files
authored
feat: Add auto-generated comment to generated code files (#848)
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.
1 parent 5928afd commit 09aaef9

11 files changed

+122
-10
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
4+
namespace KubeOps.Generator.Generators;
5+
6+
public static class AutoGeneratedSyntaxTrivia
7+
{
8+
public static readonly SyntaxTrivia Instance =
9+
SyntaxFactory.Comment(
10+
"""
11+
// <auto-generated>
12+
// This code was generated by a tool.
13+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
14+
// </auto-generated>
15+
""");
16+
}

src/KubeOps.Generator/Generators/ControllerRegistrationGenerator.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public void Execute(GeneratorExecutionContext context)
3232
var declaration = CompilationUnit()
3333
.WithUsings(
3434
List(
35-
new List<UsingDirectiveSyntax> { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), }))
35+
new List<UsingDirectiveSyntax>
36+
{
37+
UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")),
38+
}))
39+
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
3640
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("ControllerRegistrations")
3741
.WithModifiers(TokenList(
3842
Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
@@ -80,6 +84,6 @@ public void Execute(GeneratorExecutionContext context)
8084

8185
context.AddSource(
8286
"ControllerRegistrations.g.cs",
83-
SourceText.From(declaration.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
87+
SourceText.From(declaration.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
8488
}
8589
}

src/KubeOps.Generator/Generators/EntityDefinitionGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void Execute(GeneratorExecutionContext context)
3434
UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")),
3535
UsingDirective(IdentifierName("KubeOps.Abstractions.Entities")),
3636
}))
37+
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
3738
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("EntityDefinitions")
3839
.WithModifiers(TokenList(
3940
Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
@@ -83,6 +84,6 @@ public void Execute(GeneratorExecutionContext context)
8384

8485
context.AddSource(
8586
"EntityDefinitions.g.cs",
86-
SourceText.From(declaration.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
87+
SourceText.From(declaration.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
8788
}
8889
}

src/KubeOps.Generator/Generators/EntityInitializerGenerator.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ public void Execute(GeneratorExecutionContext context)
4646
ns.Add(FileScopedNamespaceDeclaration(IdentifierName(symbol.ContainingNamespace.ToDisplayString())));
4747
}
4848

49-
var partialEntityInitializer = CompilationUnit()
50-
.AddMembers(ns.ToArray())
49+
var partialEntityInitializer = CompilationUnit();
50+
51+
if (ns.Count > 0)
52+
{
53+
partialEntityInitializer = partialEntityInitializer
54+
.AddMembers(ns.ToArray())
55+
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance);
56+
}
57+
58+
partialEntityInitializer = partialEntityInitializer
5159
.AddMembers(ClassDeclaration(entity.Class.Identifier)
5260
.WithModifiers(entity.Class.Modifiers)
5361
.AddMembers(ConstructorDeclaration(entity.Class.Identifier)
@@ -69,12 +77,20 @@ public void Execute(GeneratorExecutionContext context)
6977
IdentifierName("Kind"),
7078
LiteralExpression(
7179
SyntaxKind.StringLiteralExpression,
72-
Literal(entity.Kind))))))))
80+
Literal(entity.Kind))))))));
81+
82+
if (ns.Count == 0)
83+
{
84+
partialEntityInitializer = partialEntityInitializer
85+
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance);
86+
}
87+
88+
partialEntityInitializer = partialEntityInitializer
7389
.NormalizeWhitespace();
7490

7591
context.AddSource(
7692
$"{entity.Class.Identifier}.init.g.cs",
77-
SourceText.From(partialEntityInitializer.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
93+
SourceText.From(partialEntityInitializer.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
7894
}
7995

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

133150
context.AddSource(
134151
"EntityInitializer.g.cs",
135-
SourceText.From(staticInitializers.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
152+
SourceText.From(staticInitializers.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
136153
}
137154
}

src/KubeOps.Generator/Generators/FinalizerRegistrationGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void Execute(GeneratorExecutionContext context)
4141
.WithUsings(
4242
List(
4343
new List<UsingDirectiveSyntax> { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), }))
44+
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
4445
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("FinalizerRegistrations")
4546
.WithModifiers(TokenList(
4647
Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
@@ -103,7 +104,7 @@ public void Execute(GeneratorExecutionContext context)
103104

104105
context.AddSource(
105106
"FinalizerRegistrations.g.cs",
106-
SourceText.From(declaration.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
107+
SourceText.From(declaration.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
107108
}
108109

109110
private static string FinalizerName((ClassDeclarationSyntax Finalizer, AttributedEntity Entity) finalizer)

src/KubeOps.Generator/Generators/OperatorBuilderGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void Execute(GeneratorExecutionContext context)
2222
.WithUsings(
2323
List(
2424
new List<UsingDirectiveSyntax> { UsingDirective(IdentifierName("KubeOps.Abstractions.Builder")), }))
25+
.WithLeadingTrivia(AutoGeneratedSyntaxTrivia.Instance)
2526
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("OperatorBuilderExtensions")
2627
.WithModifiers(TokenList(
2728
Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
@@ -55,6 +56,6 @@ public void Execute(GeneratorExecutionContext context)
5556

5657
context.AddSource(
5758
"OperatorBuilder.g.cs",
58-
SourceText.From(declaration.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
59+
SourceText.From(declaration.ToFullString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
5960
}
6061
}

test/KubeOps.Generator.Test/ControllerRegistrationGenerator.Test.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public class ControllerRegistrationGeneratorTest
1010
{
1111
[Theory]
1212
[InlineData("", """
13+
// <auto-generated>
14+
// This code was generated by a tool.
15+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
16+
// </auto-generated>
1317
using KubeOps.Abstractions.Builder;
1418
1519
public static class ControllerRegistrations
@@ -30,6 +34,10 @@ public class V1TestEntityController : IEntityController<V1TestEntity>
3034
{
3135
}
3236
""", """
37+
// <auto-generated>
38+
// This code was generated by a tool.
39+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
40+
// </auto-generated>
3341
using KubeOps.Abstractions.Builder;
3442
3543
public static class ControllerRegistrations

test/KubeOps.Generator.Test/EntityDefinitionGenerator.Test.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public class EntityDefinitionGeneratorTest
1010
{
1111
[Theory]
1212
[InlineData("", """
13+
// <auto-generated>
14+
// This code was generated by a tool.
15+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
16+
// </auto-generated>
1317
using KubeOps.Abstractions.Builder;
1418
using KubeOps.Abstractions.Entities;
1519
@@ -23,6 +27,10 @@ public class V1TestEntity : IKubernetesObject<V1ObjectMeta>
2327
{
2428
}
2529
""", """
30+
// <auto-generated>
31+
// This code was generated by a tool.
32+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
33+
// </auto-generated>
2634
using KubeOps.Abstractions.Builder;
2735
using KubeOps.Abstractions.Entities;
2836
@@ -42,6 +50,10 @@ public class V1AnotherEntity : IKubernetesObject<V1ObjectMeta>
4250
{
4351
}
4452
""", """
53+
// <auto-generated>
54+
// This code was generated by a tool.
55+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
56+
// </auto-generated>
4557
using KubeOps.Abstractions.Builder;
4658
using KubeOps.Abstractions.Entities;
4759

test/KubeOps.Generator.Test/EntityInitializerGenerator.Test.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public void Should_Generate_Empty_Initializer_Without_Input()
1313
{
1414
var inputCompilation = string.Empty.CreateCompilation();
1515
var expectedResult = """
16+
// <auto-generated>
17+
// This code was generated by a tool.
18+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
19+
// </auto-generated>
1620
public static class EntityInitializer
1721
{
1822
}
@@ -42,6 +46,10 @@ public class V2TestEntity : IKubernetesObject<V1ObjectMeta>
4246
}
4347
""".CreateCompilation();
4448
var expectedResult = """
49+
// <auto-generated>
50+
// This code was generated by a tool.
51+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
52+
// </auto-generated>
4553
public static class EntityInitializer
4654
{
4755
public static global::V1TestEntity Initialize(this global::V1TestEntity entity)
@@ -81,6 +89,10 @@ public class V1ConfigMap : IKubernetesObject<V1ObjectMeta>
8189
}
8290
""".CreateCompilation();
8391
var expectedResult = """
92+
// <auto-generated>
93+
// This code was generated by a tool.
94+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
95+
// </auto-generated>
8496
public static class EntityInitializer
8597
{
8698
public static global::V1ConfigMap Initialize(this global::V1ConfigMap entity)
@@ -113,6 +125,10 @@ public V1TestEntity(){}
113125
}
114126
""".CreateCompilation();
115127
var expectedResult = """
128+
// <auto-generated>
129+
// This code was generated by a tool.
130+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
131+
// </auto-generated>
116132
public static class EntityInitializer
117133
{
118134
public static global::V1TestEntity Initialize(this global::V1TestEntity entity)
@@ -144,6 +160,10 @@ public partial class V1TestEntity : IKubernetesObject<V1ObjectMeta>
144160
}
145161
""".CreateCompilation();
146162
var expectedResult = """
163+
// <auto-generated>
164+
// This code was generated by a tool.
165+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
166+
// </auto-generated>
147167
public static class EntityInitializer
148168
{
149169
}
@@ -170,6 +190,10 @@ public partial class V1TestEntity : IKubernetesObject<V1ObjectMeta>
170190
}
171191
""".CreateCompilation();
172192
var expectedResult = """
193+
// <auto-generated>
194+
// This code was generated by a tool.
195+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
196+
// </auto-generated>
173197
namespace Foo.Bar;
174198
public partial class V1TestEntity
175199
{
@@ -206,6 +230,10 @@ public partial class V1TestEntity : IKubernetesObject<V1ObjectMeta>
206230
}
207231
""".CreateCompilation();
208232
var expectedResult = """
233+
// <auto-generated>
234+
// This code was generated by a tool.
235+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
236+
// </auto-generated>
209237
namespace Foo.Bar.Baz;
210238
public partial class V1TestEntity
211239
{
@@ -236,6 +264,10 @@ public partial class V1TestEntity : IKubernetesObject<V1ObjectMeta>
236264
}
237265
""".CreateCompilation();
238266
var expectedResult = """
267+
// <auto-generated>
268+
// This code was generated by a tool.
269+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
270+
// </auto-generated>
239271
public partial class V1TestEntity
240272
{
241273
public V1TestEntity()
@@ -266,6 +298,10 @@ public V1TestEntity(string name){}
266298
}
267299
""".CreateCompilation();
268300
var expectedResult = """
301+
// <auto-generated>
302+
// This code was generated by a tool.
303+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
304+
// </auto-generated>
269305
public partial class V1TestEntity
270306
{
271307
public V1TestEntity()

test/KubeOps.Generator.Test/FinalizerRegistrationGenerator.Test.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public class FinalizerRegistrationGeneratorTest
1010
{
1111
[Theory]
1212
[InlineData("", """
13+
// <auto-generated>
14+
// This code was generated by a tool.
15+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
16+
// </auto-generated>
1317
using KubeOps.Abstractions.Builder;
1418
1519
public static class FinalizerRegistrations
@@ -30,6 +34,10 @@ public class V1TestEntityFinalizer : IEntityFinalizer<V1TestEntity>
3034
{
3135
}
3236
""", """
37+
// <auto-generated>
38+
// This code was generated by a tool.
39+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
40+
// </auto-generated>
3341
using KubeOps.Abstractions.Builder;
3442
3543
public static class FinalizerRegistrations
@@ -56,6 +64,10 @@ public class V1TestEntityCleanupOtherResourcesSuchThatThisFinalizerHasAVeryLongN
5664
{
5765
}
5866
""", """
67+
// <auto-generated>
68+
// This code was generated by a tool.
69+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
70+
// </auto-generated>
5971
using KubeOps.Abstractions.Builder;
6072
6173
public static class FinalizerRegistrations

0 commit comments

Comments
 (0)