Skip to content

Commit

Permalink
Merge pull request #472 from paulomorgado/add-this-assembly-control
Browse files Browse the repository at this point in the history
Allow suppressing ThisAssembly class generation via EmitThisAssemblyClass msbuild property
  • Loading branch information
AArnott committed Apr 29, 2020
2 parents 7f4b4d9 + e41c54c commit d7a4eb4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 16 deletions.
100 changes: 86 additions & 14 deletions src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs
Expand Up @@ -8,18 +8,26 @@

public class AssemblyInfoTest : IClassFixture<MSBuildFixture>
{
[Fact]
public void FSharpGenerator()
[Theory]
[InlineData(false)]
[InlineData(true)]
[InlineData(null)]
public void FSharpGenerator(bool? thisAssemblyClass)
{
var info = new AssemblyVersionInfo();
info.AssemblyCompany = "company";
info.AssemblyFileVersion = "1.3.1.0";
info.AssemblyVersion = "1.3.0.0";
info.CodeLanguage = "f#";

if (thisAssemblyClass.HasValue)
{
info.EmitThisAssemblyClass = thisAssemblyClass.GetValueOrDefault();
}

var built = info.BuildCode();

var expected = @"//------------------------------------------------------------------------------
var expected = $@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
Expand All @@ -33,9 +41,9 @@ namespace AssemblyInfo
[<assembly: System.Reflection.AssemblyVersionAttribute(""1.3.0.0"")>]
[<assembly: System.Reflection.AssemblyFileVersionAttribute(""1.3.1.0"")>]
[<assembly: System.Reflection.AssemblyInformationalVersionAttribute("""")>]
do()
{(thisAssemblyClass.GetValueOrDefault(true) ? $@"do()
#if NETSTANDARD || NETFRAMEWORK || NETCOREAPP
[<System.CodeDom.Compiler.GeneratedCode(""" + AssemblyVersionInfo.GeneratorName + @""",""" + AssemblyVersionInfo.GeneratorVersion + @""")>]
[<System.CodeDom.Compiler.GeneratedCode(""{AssemblyVersionInfo.GeneratorName}"",""{AssemblyVersionInfo.GeneratorVersion}"")>]
#endif
#if NETFRAMEWORK || NETCOREAPP || NETSTANDARD2_0 || NETSTANDARD2_1
[<System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage>]
Expand All @@ -48,22 +56,31 @@ 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";
info.AssemblyFileVersion = "1.3.1.0";
info.AssemblyVersion = "1.3.0.0";
info.CodeLanguage = "c#";

if (thisAssemblyClass.HasValue)
{
info.EmitThisAssemblyClass = thisAssemblyClass.GetValueOrDefault();
}

var built = info.BuildCode();

var expected = @"//------------------------------------------------------------------------------
var expected = $@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
Expand All @@ -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 = $@"'------------------------------------------------------------------------------
' <auto-generated>
' 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.
' </auto-generated>
'------------------------------------------------------------------------------
<Assembly: System.Reflection.AssemblyVersionAttribute(""1.3.0.0"")>
<Assembly: System.Reflection.AssemblyFileVersionAttribute(""1.3.1.0"")>
<Assembly: System.Reflection.AssemblyInformationalVersionAttribute("""")>
{(thisAssemblyClass.GetValueOrDefault(true) ? $@"#If NETFRAMEWORK Or NETCOREAPP Or NETSTANDARD2_0 Or NETSTANDARD2_1 Then
<System.CodeDom.Compiler.GeneratedCode(""{AssemblyVersionInfo.GeneratorName}"",""{AssemblyVersionInfo.GeneratorVersion}"")>
<System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage>
Partial Friend NotInheritable Class ThisAssembly
#ElseIf NETSTANDARD Or NETFRAMEWORK Or NETCOREAPP Then
<System.CodeDom.Compiler.GeneratedCode(""{AssemblyVersionInfo.GeneratorName}"",""{AssemblyVersionInfo.GeneratorVersion}"")>
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);
}
}
15 changes: 13 additions & 2 deletions src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs
Expand Up @@ -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()
{
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Expand Up @@ -167,6 +167,7 @@
GitCommitId="$(GitCommitId)"
GitCommitDateTicks="$(GitCommitDateTicks)"
EmitNonVersionCustomAttributes="$(NBGV_EmitNonVersionCustomAttributes)"
EmitThisAssemblyClass="$(NBGV_EmitThisAssemblyClass)"
/>
<!-- Avoid applying the newly generated AssemblyVersionInfo.cs file to the build
unless it has changed in order to allow for incremental building. -->
Expand Down

0 comments on commit d7a4eb4

Please sign in to comment.