Skip to content

Commit afdd2ad

Browse files
authored
Merge pull request #2313 from odalet/feature/put-vbnet-code_in-global-namespace
Modified the VB.NET Template so that GitVersionInformation is in the Global namespace
2 parents 0960d87 + 1b7ab3e commit afdd2ad

File tree

4 files changed

+94
-57
lines changed

4 files changed

+94
-57
lines changed

docs/input/docs/usage/msbuild-task.md

+32-13
Original file line numberDiff line numberDiff line change
@@ -84,44 +84,63 @@ appended to it.
8484
#### Other injected Variables
8585

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

8989
```csharp
90-
namespace AssemblyName
90+
[CompilerGenerated]
91+
internal static class GitVersionInformation
9192
{
92-
[CompilerGenerated]
93-
internal static class GitVersionInformation
94-
{
95-
public static string Major = "1";
96-
public static string Minor = "1";
97-
public static string Patch = "0";
98-
...All other variables
99-
}
93+
public static string Major = "1";
94+
public static string Minor = "1";
95+
public static string Patch = "0";
96+
...All other variables
10097
}
10198
```
10299

103100
### Accessing injected Variables
104101

102+
**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.**
103+
105104
#### All variables
106105

107106
```csharp
108107
var assemblyName = assembly.GetName().Name;
109-
var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation");
108+
var gitVersionInformationType = assembly.GetType("GitVersionInformation");
110109
var fields = gitVersionInformationType.GetFields();
111110

112111
foreach (var field in fields)
113112
{
114113
Trace.WriteLine(string.Format("{0}: {1}", field.Name, field.GetValue(null)));
115114
}
115+
116+
// The GitVersionInformation class generated from a F# project exposes properties
117+
var properties = gitVersionInformationType.GetProperties();
118+
119+
foreach (var property in properties)
120+
{
121+
Trace.WriteLine(string.Format("{0}: {1}", property.Name, property.GetGetMethod(true).Invoke(null, null)));
122+
}
116123
```
117124

118125
##### Specific variable
119126

120127
```csharp
121128
var assemblyName = assembly.GetName().Name;
122-
var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation");
129+
var gitVersionInformationType = assembly.GetType("GitVersionInformation");
123130
var versionField = gitVersionInformationType.GetField("Major");
124-
Trace.WriteLine(versionField.GetValue(null));
131+
if (versionField != null)
132+
{
133+
Trace.WriteLine(versionField.GetValue(null));
134+
}
135+
else
136+
{
137+
// The GitVersionInformation class generated from a F# project exposes properties
138+
var versionProperty = gitVersionInformationType.GetProperty("Major");
139+
if (versionProperty != null)
140+
{
141+
Trace.WriteLine(versionProperty.GetGetMethod(true).Invoke(null, null));
142+
}
143+
}
125144
```
126145

127146
### Populate some MSBuild properties with version metadata

src/GitVersionCore.Tests/VersionConverters/Approved/vb/GitVersionInfoGeneratorTests.ShouldCreateFile.approved.txt

+41-37
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,44 @@
88
' </auto-generated>
99
'------------------------------------------------------------------------------
1010

11-
<Global.System.Runtime.CompilerServices.CompilerGenerated()>
12-
<Global.System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage()>
13-
NotInheritable Class GitVersionInformation
14-
Private Sub New()
15-
End Sub
16-
Public Shared Major As String = "1"
17-
Public Shared Minor As String = "2"
18-
Public Shared Patch As String = "3"
19-
Public Shared PreReleaseTag As String = "unstable.4"
20-
Public Shared PreReleaseTagWithDash As String = "-unstable.4"
21-
Public Shared PreReleaseLabel As String = "unstable"
22-
Public Shared PreReleaseNumber As String = "4"
23-
Public Shared WeightedPreReleaseNumber As String = "4"
24-
Public Shared BuildMetaData As String = "5"
25-
Public Shared BuildMetaDataPadded As String = "0005"
26-
Public Shared FullBuildMetaData As String = "5.Branch.feature1.Sha.commitSha"
27-
Public Shared MajorMinorPatch As String = "1.2.3"
28-
Public Shared SemVer As String = "1.2.3-unstable.4"
29-
Public Shared LegacySemVer As String = "1.2.3-unstable4"
30-
Public Shared LegacySemVerPadded As String = "1.2.3-unstable0004"
31-
Public Shared AssemblySemVer As String = "1.2.3.0"
32-
Public Shared AssemblySemFileVer As String = "1.2.3.0"
33-
Public Shared FullSemVer As String = "1.2.3-unstable.4+5"
34-
Public Shared InformationalVersion As String = "1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha"
35-
Public Shared BranchName As String = "feature1"
36-
Public Shared EscapedBranchName As String = "feature1"
37-
Public Shared Sha As String = "commitSha"
38-
Public Shared ShortSha As String = "commitShortSha"
39-
Public Shared NuGetVersionV2 As String = "1.2.3-unstable0004"
40-
Public Shared NuGetVersion As String = "1.2.3-unstable0004"
41-
Public Shared NuGetPreReleaseTagV2 As String = "unstable0004"
42-
Public Shared NuGetPreReleaseTag As String = "unstable0004"
43-
Public Shared VersionSourceSha As String = "versionSourceSha"
44-
Public Shared CommitsSinceVersionSource As String = "5"
45-
Public Shared CommitsSinceVersionSourcePadded As String = "0005"
46-
Public Shared CommitDate As String = "2014-03-06"
47-
End Class
11+
Namespace Global
12+
13+
<Global.System.Runtime.CompilerServices.CompilerGenerated()>
14+
<Global.System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage()>
15+
NotInheritable Class GitVersionInformation
16+
Private Sub New()
17+
End Sub
18+
Public Shared Major As String = "1"
19+
Public Shared Minor As String = "2"
20+
Public Shared Patch As String = "3"
21+
Public Shared PreReleaseTag As String = "unstable.4"
22+
Public Shared PreReleaseTagWithDash As String = "-unstable.4"
23+
Public Shared PreReleaseLabel As String = "unstable"
24+
Public Shared PreReleaseNumber As String = "4"
25+
Public Shared WeightedPreReleaseNumber As String = "4"
26+
Public Shared BuildMetaData As String = "5"
27+
Public Shared BuildMetaDataPadded As String = "0005"
28+
Public Shared FullBuildMetaData As String = "5.Branch.feature1.Sha.commitSha"
29+
Public Shared MajorMinorPatch As String = "1.2.3"
30+
Public Shared SemVer As String = "1.2.3-unstable.4"
31+
Public Shared LegacySemVer As String = "1.2.3-unstable4"
32+
Public Shared LegacySemVerPadded As String = "1.2.3-unstable0004"
33+
Public Shared AssemblySemVer As String = "1.2.3.0"
34+
Public Shared AssemblySemFileVer As String = "1.2.3.0"
35+
Public Shared FullSemVer As String = "1.2.3-unstable.4+5"
36+
Public Shared InformationalVersion As String = "1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha"
37+
Public Shared BranchName As String = "feature1"
38+
Public Shared EscapedBranchName As String = "feature1"
39+
Public Shared Sha As String = "commitSha"
40+
Public Shared ShortSha As String = "commitShortSha"
41+
Public Shared NuGetVersionV2 As String = "1.2.3-unstable0004"
42+
Public Shared NuGetVersion As String = "1.2.3-unstable0004"
43+
Public Shared NuGetPreReleaseTagV2 As String = "unstable0004"
44+
Public Shared NuGetPreReleaseTag As String = "unstable0004"
45+
Public Shared VersionSourceSha As String = "versionSourceSha"
46+
Public Shared CommitsSinceVersionSource As String = "5"
47+
Public Shared CommitsSinceVersionSourcePadded As String = "0005"
48+
Public Shared CommitDate As String = "2014-03-06"
49+
End Class
50+
51+
End Namespace

src/GitVersionCore/VersionConverters/GitVersionInfo/GitVersionInfoGenerator.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public void Execute(VersionVariables variables, GitVersionInfoContext context)
3636
var fileExtension = Path.GetExtension(filePath);
3737
var template = templateManager.GetTemplateFor(fileExtension);
3838
var addFormat = templateManager.GetAddFormatFor(fileExtension);
39+
var indentation = GetIndentation(fileExtension);
3940

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

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

@@ -50,5 +51,14 @@ public void Execute(VersionVariables variables, GitVersionInfoContext context)
5051
public void Dispose()
5152
{
5253
}
54+
55+
// Because The VB-generated class is included in a namespace declaration,
56+
// the properties must be offsetted by 2 tabs.
57+
// Whereas in the C# and F# cases, 1 tab is enough.
58+
private static string GetIndentation(string fileExtension)
59+
{
60+
var tabs = fileExtension.ToLowerInvariant().EndsWith("vb") ? 2 : 1;
61+
return new string(' ', tabs * 4);
62+
}
5363
}
5464
}

src/GitVersionCore/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.vb

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
' </auto-generated>
99
'------------------------------------------------------------------------------
1010

11-
<Global.System.Runtime.CompilerServices.CompilerGenerated()>
12-
<Global.System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage()>
13-
NotInheritable Class GitVersionInformation
14-
Private Sub New()
15-
End Sub
11+
Namespace Global
12+
13+
<Global.System.Runtime.CompilerServices.CompilerGenerated()>
14+
<Global.System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage()>
15+
NotInheritable Class GitVersionInformation
16+
Private Sub New()
17+
End Sub
1618
{0}
17-
End Class
19+
End Class
20+
21+
End Namespace

0 commit comments

Comments
 (0)