diff --git a/src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs b/src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs index 09da627d..4137da6b 100644 --- a/src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs +++ b/src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs @@ -8,8 +8,11 @@ public class AssemblyInfoTest : IClassFixture { - [Fact] - public void FSharpGenerator() + [Theory] + [InlineData(false)] + [InlineData(true)] + [InlineData(null)] + public void FSharpGenerator(bool? thisAssemblyClass) { var info = new AssemblyVersionInfo(); info.AssemblyCompany = "company"; @@ -17,9 +20,14 @@ public void FSharpGenerator() info.AssemblyVersion = "1.3.0.0"; info.CodeLanguage = "f#"; + if (thisAssemblyClass.HasValue) + { + info.EmitThisAssemblyClass = thisAssemblyClass.GetValueOrDefault(); + } + var built = info.BuildCode(); - var expected = @"//------------------------------------------------------------------------------ + var expected = $@"//------------------------------------------------------------------------------ // // This code was generated by a tool. // Runtime Version:4.0.30319.42000 @@ -33,9 +41,9 @@ namespace AssemblyInfo [] [] [] -do() +{(thisAssemblyClass.GetValueOrDefault(true) ? $@"do() #if NETSTANDARD || NETFRAMEWORK || NETCOREAPP -[] +[] #endif #if NETFRAMEWORK || NETCOREAPP || NETSTANDARD2_0 || NETSTANDARD2_1 [] @@ -48,12 +56,16 @@ namespace AssemblyInfo static member internal IsPrerelease = false static member internal RootNamespace = """" do() -"; +" : "")}"; + Assert.Equal(expected, built); } - [Fact] - public void CSharpGenerator() + [Theory] + [InlineData(false)] + [InlineData(true)] + [InlineData(null)] + public void CSharpGenerator(bool? thisAssemblyClass) { var info = new AssemblyVersionInfo(); info.AssemblyCompany = "company"; @@ -61,9 +73,14 @@ public void CSharpGenerator() info.AssemblyVersion = "1.3.0.0"; info.CodeLanguage = "c#"; + if (thisAssemblyClass.HasValue) + { + info.EmitThisAssemblyClass = thisAssemblyClass.GetValueOrDefault(); + } + var built = info.BuildCode(); - var expected = @"//------------------------------------------------------------------------------ + var expected = $@"//------------------------------------------------------------------------------ // // This code was generated by a tool. // Runtime Version:4.0.30319.42000 @@ -76,21 +93,76 @@ public void CSharpGenerator() [assembly: System.Reflection.AssemblyVersionAttribute(""1.3.0.0"")] [assembly: System.Reflection.AssemblyFileVersionAttribute(""1.3.1.0"")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("""")] -#if NETSTANDARD || NETFRAMEWORK || NETCOREAPP -[System.CodeDom.Compiler.GeneratedCode(""" + AssemblyVersionInfo.GeneratorName + @""",""" + AssemblyVersionInfo.GeneratorVersion + @""")] +{(thisAssemblyClass.GetValueOrDefault(true) ? $@"#if NETSTANDARD || NETFRAMEWORK || NETCOREAPP +[System.CodeDom.Compiler.GeneratedCode(""{AssemblyVersionInfo.GeneratorName}"",""{AssemblyVersionInfo.GeneratorVersion}"")] #endif #if NETFRAMEWORK || NETCOREAPP || NETSTANDARD2_0 || NETSTANDARD2_1 [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] #endif -internal static partial class ThisAssembly { +internal static partial class ThisAssembly {{ internal const string AssemblyVersion = ""1.3.0.0""; internal const string AssemblyFileVersion = ""1.3.1.0""; internal const string AssemblyCompany = ""company""; internal const bool IsPublicRelease = false; internal const bool IsPrerelease = false; internal const string RootNamespace = """"; -} -"; +}} +" : "")}"; + + Assert.Equal(expected, built); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + [InlineData(null)] + public void VisualBasicGenerator(bool? thisAssemblyClass) + { + var info = new AssemblyVersionInfo(); + info.AssemblyCompany = "company"; + info.AssemblyFileVersion = "1.3.1.0"; + info.AssemblyVersion = "1.3.0.0"; + info.CodeLanguage = "vb"; + + if (thisAssemblyClass.HasValue) + { + info.EmitThisAssemblyClass = thisAssemblyClass.GetValueOrDefault(); + } + + var built = info.BuildCode(); + + var expected = $@"'------------------------------------------------------------------------------ +' +' This code was generated by a tool. +' Runtime Version:4.0.30319.42000 +' +' Changes to this file may cause incorrect behavior and will be lost if +' the code is regenerated. +' +'------------------------------------------------------------------------------ + + + + +{(thisAssemblyClass.GetValueOrDefault(true) ? $@"#If NETFRAMEWORK Or NETCOREAPP Or NETSTANDARD2_0 Or NETSTANDARD2_1 Then + + +Partial Friend NotInheritable Class ThisAssembly +#ElseIf NETSTANDARD Or NETFRAMEWORK Or NETCOREAPP Then + +Partial Friend NotInheritable Class ThisAssembly +#Else +Partial Friend NotInheritable Class ThisAssembly +#End If + Friend Const AssemblyVersion As String = ""1.3.0.0"" + Friend Const AssemblyFileVersion As String = ""1.3.1.0"" + Friend Const AssemblyCompany As String = ""company"" + Friend Const IsPublicRelease As Boolean = False + Friend Const IsPrerelease As Boolean = False + Friend Const RootNamespace As String = """" +End Class +" : "")}"; + Assert.Equal(expected, built); } } diff --git a/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs b/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs index ef023a0e..642193b6 100644 --- a/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs +++ b/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs @@ -86,6 +86,8 @@ public class AssemblyVersionInfo : Task public string GitCommitDateTicks { get; set; } + public bool EmitThisAssemblyClass { get; set; } = true; + #if NET461 public override bool Execute() { @@ -105,7 +107,11 @@ public override bool Execute() var ns = new CodeNamespace(); this.generatedFile.Namespaces.Add(ns); - ns.Types.Add(this.CreateThisAssemblyClass()); + + if (this.EmitThisAssemblyClass) + { + ns.Types.Add(this.CreateThisAssemblyClass()); + } Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile)); FileStream file = null; @@ -319,7 +325,12 @@ public string BuildCode() this.generator.AddBlankLine(); this.generator.EmitNamespaceIfRequired(this.RootNamespace ?? "AssemblyInfo"); this.GenerateAssemblyAttributes(); - this.GenerateThisAssemblyClass(); + + if (this.EmitThisAssemblyClass) + { + this.GenerateThisAssemblyClass(); + } + return this.generator.GetCode(); } diff --git a/src/Nerdbank.GitVersioning.Tasks/build/Nerdbank.GitVersioning.targets b/src/Nerdbank.GitVersioning.Tasks/build/Nerdbank.GitVersioning.targets index 695d03e4..6d9ddeaf 100644 --- a/src/Nerdbank.GitVersioning.Tasks/build/Nerdbank.GitVersioning.targets +++ b/src/Nerdbank.GitVersioning.Tasks/build/Nerdbank.GitVersioning.targets @@ -167,6 +167,7 @@ GitCommitId="$(GitCommitId)" GitCommitDateTicks="$(GitCommitDateTicks)" EmitNonVersionCustomAttributes="$(NBGV_EmitNonVersionCustomAttributes)" + EmitThisAssemblyClass="$(NBGV_EmitThisAssemblyClass)" />