Skip to content

Commit 6fbb081

Browse files
authored
Merge pull request #307 from JohannesWill/patch-1
Add GeneratedCodeAttribute to ThisAssembly
2 parents 1fab479 + f419a1f commit 6fbb081

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace AssemblyInfo
3636
[<assembly: System.Reflection.AssemblyFileVersionAttribute(""1.3.1.0"")>]
3737
[<assembly: System.Reflection.AssemblyInformationalVersionAttribute("""")>]
3838
do()
39+
[<System.CodeDom.Compiler.GeneratedCode(""" + AssemblyVersionInfo.GeneratorName + @""",""" + AssemblyVersionInfo.GeneratorVersion + @""")>]
3940
type internal ThisAssembly() =
4041
static member internal AssemblyVersion = ""1.3.0.0""
4142
static member internal AssemblyFileVersion = ""1.3.1.0""

src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
public class AssemblyVersionInfo : Task
1515
{
16+
public static readonly string GeneratorName = ThisAssembly.AssemblyName;
17+
public static readonly string GeneratorVersion = ThisAssembly.AssemblyVersion;
1618
#if NET461
1719
private static readonly CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions
1820
{
@@ -121,6 +123,12 @@ private CodeTypeDeclaration CreateThisAssemblyClass()
121123
IsPartial = true,
122124
TypeAttributes = TypeAttributes.NotPublic | TypeAttributes.Sealed,
123125
};
126+
127+
var codeAttributeDeclarationCollection = new CodeAttributeDeclarationCollection();
128+
codeAttributeDeclarationCollection.Add(new CodeAttributeDeclaration("System.CodeDom.Compiler.GeneratedCode",
129+
new CodeAttributeArgument(new CodePrimitiveExpression(GeneratorName)),
130+
new CodeAttributeArgument(new CodePrimitiveExpression(GeneratorVersion))));
131+
thisAssembly.CustomAttributes = codeAttributeDeclarationCollection;
124132

125133
// CodeDOM doesn't support static classes, so hide the constructor instead.
126134
thisAssembly.Members.Add(new CodeConstructor { Attributes = MemberAttributes.Private });
@@ -413,7 +421,9 @@ internal override void EndThisAssemblyClass()
413421

414422
internal override void StartThisAssemblyClass()
415423
{
416-
this.codeBuilder.AppendLine("do()\r\ntype internal ThisAssembly() =");
424+
this.codeBuilder.AppendLine("do()");
425+
this.codeBuilder.AppendLine($"[<System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")>]");
426+
this.codeBuilder.AppendLine("type internal ThisAssembly() =");
417427
}
418428
}
419429

@@ -431,6 +441,7 @@ internal override void DeclareAttribute(Type type, string arg)
431441

432442
internal override void StartThisAssemblyClass()
433443
{
444+
this.codeBuilder.AppendLine($"[System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")]");
434445
this.codeBuilder.AppendLine("internal static partial class ThisAssembly {");
435446
}
436447

@@ -459,6 +470,7 @@ internal override void DeclareAttribute(Type type, string arg)
459470

460471
internal override void StartThisAssemblyClass()
461472
{
473+
this.codeBuilder.AppendLine($"<System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")>");
462474
this.codeBuilder.AppendLine("Partial Friend NotInheritable Class ThisAssembly");
463475
}
464476

0 commit comments

Comments
 (0)