Skip to content

Commit

Permalink
Add support for NBGV_ThisAssemblyNamespace property.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Mar 20, 2023
1 parent f66943e commit adb0bcd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
46 changes: 35 additions & 11 deletions src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class AssemblyVersionInfo : Microsoft.Build.Utilities.Task

public string RootNamespace { get; set; }

public string ThisAssemblyNamespace { get; set; }

public string AssemblyOriginatorKeyFile { get; set; }

public string AssemblyKeyContainerName { get; set; }
Expand Down Expand Up @@ -119,7 +121,8 @@ public string BuildCode()
this.generator.AddBlankLine();
this.generator.AddAnalysisSuppressions();
this.generator.AddBlankLine();
this.generator.EmitNamespaceIfRequired(this.RootNamespace ?? "AssemblyInfo");
this.generator.EmitNamespaceIfRequired(this.ThisAssemblyNamespace ?? this.RootNamespace ?? "AssemblyInfo");

this.GenerateAssemblyAttributes();

if (this.EmitThisAssemblyClass)
Expand Down Expand Up @@ -520,7 +523,7 @@ private void GenerateAssemblyAttributes()

private void GenerateThisAssemblyClass()
{
this.generator.StartThisAssemblyClass();
this.generator.StartThisAssemblyClass(this.ThisAssemblyNamespace);

List<KeyValuePair<string, (object Value, bool EmitIfEmpty)>> fields = this.GetFieldsForThisAssembly();

Expand Down Expand Up @@ -557,7 +560,7 @@ private void GenerateThisAssemblyClass()
}
}

this.generator.EndThisAssemblyClass();
this.generator.EndThisAssemblyClass(this.ThisAssemblyNamespace is not null);
}

private CodeGenerator CreateGenerator()
Expand Down Expand Up @@ -644,15 +647,15 @@ internal CodeGenerator()

internal abstract void DeclareAttribute(Type type, string arg);

internal abstract void StartThisAssemblyClass();
internal abstract void StartThisAssemblyClass(string ns);

internal abstract void AddThisAssemblyMember(string name, string value);

internal abstract void AddThisAssemblyMember(string name, bool value);

internal abstract void AddThisAssemblyMember(string name, DateTime value);

internal abstract void EndThisAssemblyClass();
internal abstract void EndThisAssemblyClass(bool ns);

/// <summary>
/// Gives languages that *require* a namespace a chance to emit such.
Expand Down Expand Up @@ -718,13 +721,14 @@ internal override void DeclareAttribute(Type type, string arg)
this.CodeBuilder.AppendLine($"[<assembly: {type.FullName}(\"{arg}\")>]");
}

internal override void EndThisAssemblyClass()
internal override void EndThisAssemblyClass(bool ns)
{
this.CodeBuilder.AppendLine("do()");
}

internal override void StartThisAssemblyClass()
internal override void StartThisAssemblyClass(string ns)
{
// ns is handled in EmitNamespaceIfRequired().
this.CodeBuilder.AppendLine("do()");
this.CodeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}");
this.CodeBuilder.AppendLine($"[<System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")>]");
Expand Down Expand Up @@ -753,8 +757,13 @@ internal override void DeclareAttribute(Type type, string arg)
this.CodeBuilder.AppendLine($"[assembly: {type.FullName}(\"{arg}\")]");
}

internal override void StartThisAssemblyClass()
internal override void StartThisAssemblyClass(string ns)
{
if (ns is not null)
{
this.CodeBuilder.AppendLine($"namespace {ns} {{");
}

this.CodeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}");
this.CodeBuilder.AppendLine($"[System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")]");
this.CodeBuilder.AppendLine("#endif");
Expand All @@ -779,9 +788,14 @@ internal override void AddThisAssemblyMember(string name, DateTime value)
this.CodeBuilder.AppendLine($" internal static readonly System.DateTime {name} = new System.DateTime({value.Ticks}L, System.DateTimeKind.Utc);");
}

internal override void EndThisAssemblyClass()
internal override void EndThisAssemblyClass(bool ns)
{
this.CodeBuilder.AppendLine("}");

if (ns)
{
this.CodeBuilder.AppendLine("}");
}
}
}

Expand All @@ -802,8 +816,13 @@ internal override void DeclareAttribute(Type type, string arg)
this.CodeBuilder.AppendLine($"<Assembly: {type.FullName}(\"{arg}\")>");
}

internal override void StartThisAssemblyClass()
internal override void StartThisAssemblyClass(string ns)
{
if (ns is not null)
{
this.CodeBuilder.AppendLine($"Namespace {ns}");
}

this.CodeBuilder.AppendLine($"#If {CompilerDefinesAroundExcludeFromCodeCoverageAttribute.Replace("||", " Or ")} Then");
this.CodeBuilder.AppendLine($"<System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")>");
this.CodeBuilder.AppendLine("<System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage>");
Expand Down Expand Up @@ -831,9 +850,14 @@ internal override void AddThisAssemblyMember(string name, DateTime value)
this.CodeBuilder.AppendLine($" Friend Shared ReadOnly {name} As System.DateTime = New System.DateTime({value.Ticks}L, System.DateTimeKind.Utc)");
}

internal override void EndThisAssemblyClass()
internal override void EndThisAssemblyClass(bool ns)
{
this.CodeBuilder.AppendLine("End Class");

if (ns)
{
this.CodeBuilder.AppendLine("End Namespace");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
AssemblyInformationalVersion="$(AssemblyInformationalVersion)"
AssemblyName="$(AssemblyName)"
RootNamespace="$(RootNamespace)"
ThisAssemblyNamespace="$(NBGV_ThisAssemblyNamespace)"
AssemblyOriginatorKeyFile="$(AssemblyOriginatorKeyFile)"
AssemblyTitle="$(AssemblyTitle)"
AssemblyProduct="$(AssemblyProduct)"
Expand Down

0 comments on commit adb0bcd

Please sign in to comment.