Skip to content

Modified the VB.NET Template so that GitVersionInformation is in the Global namespace #2313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions docs/input/docs/usage/msbuild-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,63 @@ appended to it.
#### Other injected Variables

All other [variables](../more-info/variables) will be injected into an
internal static class:
internal static class part of the global namespace similar to this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just thinking, if GitversionInformation is moved to the global namespace, won't this make ILMerging difficult? If several of the merged assemblies are versioned with GitVersion, we would potentially end up with several colliding GitversionInformation classes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That poses no problem because the generated classes are, and this is very important, internal. An external assembly cannot see these class unless you use reflection, and when using reflection they are resolved with their full name (which happens to be only the class name) against a specific assembly, therefore, no collision.

This trick, I often use with Visual Studio's "shared projects": they are just bunches of code that get "included" into the consuming project. I always make sure classes in these shared projects are internal so that they can be used from any assembly even if these assemblies reference one another.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if this were an issue, it would already have shown with C# or F# generated classes that are already part of the global namespace (and internal).

Copy link
Contributor Author

@odalet odalet Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmh, maybe I spoke too quickly, I missed the bit about ILMerge... Everything being part of the same assembly in the end, it may be an issue. I'll give it a try, and sorry if you thought I was teaching you how visibility worked in .NET! However, my point that the issue already exists if any is still valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, after a quick googling, it appears this indeeds poses problems:

Suppose class A is internal to App, and also to one of the lib (with same namespace). Before merging there will not be any issue. After merging it will become issue to resolve ambiguous reference.

See https://stackoverflow.com/a/14042227/107552

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And with Fody + Costura, everything just works out of the box. This is because, the principle is radically different. The assembies' IL is not merged, instead the dependent assemblies are embedded as resources in the main assembly, then extracted and loaded at runtime (during module initialization). Thus, the assemblies never cease to exist as such and type resolution just works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are interested in witnessing the tests I did, everything is in this repository: https://github.com/odalet/GitVersionTests

Copy link
Contributor Author

@odalet odalet Jun 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And now for ideas on how this whole "code generation" thing could be tweaked:

  • The generated class could be put in a namespace or have a name that will guarantee non-collision. To guarantee this, the class name should be based on the assembly name (and not on some 'default' or root namespace). The class could be inside a namespace named as the assembly or be an inner class inside a class named like the assembly or be a concatenation of assembly name and GitVersionInformation...

  • A property / command line argument could be provided to let the user choose the name of the generated class.

  • I recently stumbled upon the GitInfo. Its purpose is similar to GitVersion's with the following differences:

    • Completely MSBuild-based, no C# code at all
    • Seems to have far less feature. It seems to simply expose Git information and does not attempt clever inference of versions.
    • There is however one feature that may be interesting here:

It, too, generates code containing version information. However:

  • The version information is made available through constants, not properties or fields.
  • The file containing this information is generated soon enough for it to be available at "design-time": it can be used without resorting to reflection.

For example, here is what I can write when using GitInfo:

Console.WriteLine($"{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Minor}.{ThisAssembly.Git.SemVer.Patch}");

The advantage of this strategy is that because constants are inlined in the calling code, the original type that exposes them is useless at runtime and therefore, collisions on this type are not anymore an issue. And being able to use the version information without reflection also seems more user-friendly.

So, mixing some of these ideas may be the way to go. However, in any case, it is quite some work, and most probably breaking. In the meantime, my experiments with a selection of assembly merging tools show that the collision problem can be mitigated.

It's now up to you to reflect upon all this and decide what or what not to do :)

Hope this helped!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constants are a great idea for how we can implement this in a new major version of GitVersion! Whether we'll do this for v6 or not is mainly dependent on timing; if someone submits a pull request implementing this before we go live with v6, it will be released with v6. If not, we would have to postpone it to v7.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generating constants should be rather straightforward. The MSBuild part that generates this soon so that the user can call into it may be a little trickier (I'm not very proficient at MSBuild ;)).

I'll probably come up with an issue that will serve as a discussion and specification of what can be done.


```csharp
namespace AssemblyName
[CompilerGenerated]
internal static class GitVersionInformation
{
[CompilerGenerated]
internal static class GitVersionInformation
{
public static string Major = "1";
public static string Minor = "1";
public static string Patch = "0";
...All other variables
}
public static string Major = "1";
public static string Minor = "1";
public static string Patch = "0";
...All other variables
}
```

### Accessing injected Variables

**NB: depending on the source language of the assembly, the injected variables may be exposed either as fields or as properties. The examples below take care of this.**

#### All variables

```csharp
var assemblyName = assembly.GetName().Name;
var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation");
var gitVersionInformationType = assembly.GetType("GitVersionInformation");
var fields = gitVersionInformationType.GetFields();

foreach (var field in fields)
{
Trace.WriteLine(string.Format("{0}: {1}", field.Name, field.GetValue(null)));
}

// The GitVersionInformation class generated from a F# project exposes properties
var properties = gitVersionInformationType.GetProperties();

foreach (var property in properties)
{
Trace.WriteLine(string.Format("{0}: {1}", property.Name, property.GetGetMethod(true).Invoke(null, null)));
}
```

##### Specific variable

```csharp
var assemblyName = assembly.GetName().Name;
var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation");
var gitVersionInformationType = assembly.GetType("GitVersionInformation");
var versionField = gitVersionInformationType.GetField("Major");
Trace.WriteLine(versionField.GetValue(null));
if (versionField != null)
{
Trace.WriteLine(versionField.GetValue(null));
}
else
{
// The GitVersionInformation class generated from a F# project exposes properties
var versionProperty = gitVersionInformationType.GetProperty("Major");
if (versionProperty != null)
{
Trace.WriteLine(versionProperty.GetGetMethod(true).Invoke(null, null));
}
}
```

### Populate some MSBuild properties with version metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,44 @@
' </auto-generated>
'------------------------------------------------------------------------------

<Global.System.Runtime.CompilerServices.CompilerGenerated()>
<Global.System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage()>
NotInheritable Class GitVersionInformation
Private Sub New()
End Sub
Public Shared Major As String = "1"
Public Shared Minor As String = "2"
Public Shared Patch As String = "3"
Public Shared PreReleaseTag As String = "unstable.4"
Public Shared PreReleaseTagWithDash As String = "-unstable.4"
Public Shared PreReleaseLabel As String = "unstable"
Public Shared PreReleaseNumber As String = "4"
Public Shared WeightedPreReleaseNumber As String = "4"
Public Shared BuildMetaData As String = "5"
Public Shared BuildMetaDataPadded As String = "0005"
Public Shared FullBuildMetaData As String = "5.Branch.feature1.Sha.commitSha"
Public Shared MajorMinorPatch As String = "1.2.3"
Public Shared SemVer As String = "1.2.3-unstable.4"
Public Shared LegacySemVer As String = "1.2.3-unstable4"
Public Shared LegacySemVerPadded As String = "1.2.3-unstable0004"
Public Shared AssemblySemVer As String = "1.2.3.0"
Public Shared AssemblySemFileVer As String = "1.2.3.0"
Public Shared FullSemVer As String = "1.2.3-unstable.4+5"
Public Shared InformationalVersion As String = "1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha"
Public Shared BranchName As String = "feature1"
Public Shared EscapedBranchName As String = "feature1"
Public Shared Sha As String = "commitSha"
Public Shared ShortSha As String = "commitShortSha"
Public Shared NuGetVersionV2 As String = "1.2.3-unstable0004"
Public Shared NuGetVersion As String = "1.2.3-unstable0004"
Public Shared NuGetPreReleaseTagV2 As String = "unstable0004"
Public Shared NuGetPreReleaseTag As String = "unstable0004"
Public Shared VersionSourceSha As String = "versionSourceSha"
Public Shared CommitsSinceVersionSource As String = "5"
Public Shared CommitsSinceVersionSourcePadded As String = "0005"
Public Shared CommitDate As String = "2014-03-06"
End Class
Namespace Global

<Global.System.Runtime.CompilerServices.CompilerGenerated()>
<Global.System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage()>
NotInheritable Class GitVersionInformation
Private Sub New()
End Sub
Public Shared Major As String = "1"
Public Shared Minor As String = "2"
Public Shared Patch As String = "3"
Public Shared PreReleaseTag As String = "unstable.4"
Public Shared PreReleaseTagWithDash As String = "-unstable.4"
Public Shared PreReleaseLabel As String = "unstable"
Public Shared PreReleaseNumber As String = "4"
Public Shared WeightedPreReleaseNumber As String = "4"
Public Shared BuildMetaData As String = "5"
Public Shared BuildMetaDataPadded As String = "0005"
Public Shared FullBuildMetaData As String = "5.Branch.feature1.Sha.commitSha"
Public Shared MajorMinorPatch As String = "1.2.3"
Public Shared SemVer As String = "1.2.3-unstable.4"
Public Shared LegacySemVer As String = "1.2.3-unstable4"
Public Shared LegacySemVerPadded As String = "1.2.3-unstable0004"
Public Shared AssemblySemVer As String = "1.2.3.0"
Public Shared AssemblySemFileVer As String = "1.2.3.0"
Public Shared FullSemVer As String = "1.2.3-unstable.4+5"
Public Shared InformationalVersion As String = "1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha"
Public Shared BranchName As String = "feature1"
Public Shared EscapedBranchName As String = "feature1"
Public Shared Sha As String = "commitSha"
Public Shared ShortSha As String = "commitShortSha"
Public Shared NuGetVersionV2 As String = "1.2.3-unstable0004"
Public Shared NuGetVersion As String = "1.2.3-unstable0004"
Public Shared NuGetPreReleaseTagV2 As String = "unstable0004"
Public Shared NuGetPreReleaseTag As String = "unstable0004"
Public Shared VersionSourceSha As String = "versionSourceSha"
Public Shared CommitsSinceVersionSource As String = "5"
Public Shared CommitsSinceVersionSourcePadded As String = "0005"
Public Shared CommitDate As String = "2014-03-06"
End Class

End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public void Execute(VersionVariables variables, GitVersionInfoContext context)
var fileExtension = Path.GetExtension(filePath);
var template = templateManager.GetTemplateFor(fileExtension);
var addFormat = templateManager.GetAddFormatFor(fileExtension);
var indentation = GetIndentation(fileExtension);

var members = string.Join(System.Environment.NewLine, variables.Select(v => string.Format(" " + addFormat, v.Key, v.Value)));
var members = string.Join(System.Environment.NewLine, variables.Select(v => string.Format(indentation + addFormat, v.Key, v.Value)));

var fileContents = string.Format(template, members);

Expand All @@ -50,5 +51,14 @@ public void Execute(VersionVariables variables, GitVersionInfoContext context)
public void Dispose()
{
}

// Because The VB-generated class is included in a namespace declaration,
// the properties must be offsetted by 2 tabs.
// Whereas in the C# and F# cases, 1 tab is enough.
private static string GetIndentation(string fileExtension)
{
var tabs = fileExtension.ToLowerInvariant().EndsWith("vb") ? 2 : 1;
return new string(' ', tabs * 4);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
' </auto-generated>
'------------------------------------------------------------------------------

<Global.System.Runtime.CompilerServices.CompilerGenerated()>
<Global.System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage()>
NotInheritable Class GitVersionInformation
Private Sub New()
End Sub
Namespace Global

<Global.System.Runtime.CompilerServices.CompilerGenerated()>
<Global.System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage()>
NotInheritable Class GitVersionInformation
Private Sub New()
End Sub
{0}
End Class
End Class

End Namespace