Skip to content

Commit d5bbf0b

Browse files
authored
Merge pull request #1155 from kjjuno/feature/#947_display_own_version
Adds /version switch
2 parents 321d709 + 1571ced commit d5bbf0b

9 files changed

+118
-2
lines changed

src/GitVersionExe.Tests/GitVersionExe.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="MsBuildProjectArgTest.cs" />
101101
<Compile Include="PullRequestInTeamCityTest.cs" />
102102
<Compile Include="AssemblyInfoFileUpdateTests.cs" />
103+
<Compile Include="VersionWriterTests.cs" />
103104
</ItemGroup>
104105
<ItemGroup>
105106
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />

src/GitVersionExe.Tests/HelpWriterTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public void AllArgsAreInHelp()
1616
{ "TargetBranch", "/b" },
1717
{ "LogFilePath" , "/l" },
1818
{ "DynamicRepositoryLocation" , "/dynamicRepoLocation" },
19-
{ "IsHelp", "/?" }
19+
{ "IsHelp", "/?" },
20+
{ "IsVersion", "/version" }
2021
};
2122
string helpText = null;
2223

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace GitVersionExe.Tests
2+
{
3+
using System;
4+
using System.IO;
5+
using System.Reflection;
6+
using GitVersion;
7+
using Mono.Cecil;
8+
using NUnit.Framework;
9+
10+
[TestFixture]
11+
public class VersionWriterTests
12+
{
13+
[Test]
14+
public void WriteVersion_ShouldWriteFileVersion_WithNoPrereleaseTag()
15+
{
16+
var asm = GenerateAssembly(new Version(1, 0, 0), "");
17+
18+
string version = null;
19+
VersionWriter.WriteTo(asm, v => version = v);
20+
21+
Assert.IsNotNull(asm);
22+
Assert.AreEqual("1.0.0", version);
23+
}
24+
25+
[Test]
26+
public void WriteVersion_ShouldWriteFileVersion_WithPrereleaseTag()
27+
{
28+
var asm = GenerateAssembly(new Version(1, 0, 0), "-beta0004");
29+
30+
string version = null;
31+
VersionWriter.WriteTo(asm, v => version = v);
32+
33+
Assert.IsNotNull(asm);
34+
Assert.AreEqual("1.0.0-beta0004", version);
35+
}
36+
37+
private Assembly GenerateAssembly(Version fileVersion, string prereleaseInfo)
38+
{
39+
var definition = new AssemblyNameDefinition("test-asm", fileVersion);
40+
41+
var asmDef = AssemblyDefinition.CreateAssembly(definition, "test-asm", ModuleKind.Dll);
42+
var constructor = typeof(AssemblyInformationalVersionAttribute).GetConstructor(new[] { typeof(string) });
43+
var methodReference = asmDef.MainModule.Import(constructor);
44+
var customAttribute = new CustomAttribute(methodReference);
45+
customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(asmDef.MainModule.TypeSystem.String, fileVersion + prereleaseInfo));
46+
asmDef.CustomAttributes.Add(customAttribute);
47+
48+
using (var memoryStream = new MemoryStream())
49+
{
50+
asmDef.Write(memoryStream);
51+
52+
return Assembly.Load(memoryStream.ToArray());
53+
}
54+
}
55+
}
56+
}

src/GitVersionExe/ArgumentParser.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
6565
var values = switchesAndValues.GetValues(name);
6666
var value = values != null ? values.FirstOrDefault() : null;
6767

68+
if (name.IsSwitch("version"))
69+
{
70+
EnsureArgumentValueCount(values);
71+
arguments.IsVersion = true;
72+
continue;
73+
}
74+
6875
if (name.IsSwitch("l"))
6976
{
7077
EnsureArgumentValueCount(values);

src/GitVersionExe/Arguments.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public Arguments()
2828
public bool Init;
2929
public bool Diag;
3030

31+
public bool IsVersion;
3132
public bool IsHelp;
3233
public string LogFilePath;
3334
public string ShowVariable;

src/GitVersionExe/GitVersionExe.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="AssemblyInfo.cs" />
7373
<Compile Include="AssemblyInfoFileUpdate.cs" />
7474
<Compile Include="SpecifiedArgumentRunner.cs" />
75+
<Compile Include="VersionWriter.cs" />
7576
</ItemGroup>
7677
<ItemGroup>
7778
<None Include="app.config" />

src/GitVersionExe/HelpWriter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace GitVersion
22
{
33
using System;
4+
using System.Reflection;
45

56
class HelpWriter
67
{
@@ -11,12 +12,18 @@ public static void Write()
1112

1213
public static void WriteTo(Action<string> writeAction)
1314
{
14-
const string message = @"Use convention to derive a SemVer product version from a GitFlow or GitHub based repository.
15+
string version = string.Empty;
16+
Assembly assembly = Assembly.GetExecutingAssembly();
17+
VersionWriter.WriteTo(assembly, v => version = v);
18+
19+
string message = "GitVersion " + version + @"
20+
Use convention to derive a SemVer product version from a GitFlow or GitHub based repository.
1521
1622
GitVersion [path]
1723
1824
path The directory containing .git. If not defined current directory is used. (Must be first argument)
1925
init Configuration utility for gitversion
26+
/version Displays the version of GitVersion
2027
/diag Runs GitVersion with additional diagnostic information (requires git.exe to be installed)
2128
/h or /? Shows Help
2229

src/GitVersionExe/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace GitVersion
77
using System.Diagnostics;
88
using System.IO;
99
using System.Linq;
10+
using System.Reflection;
1011
using System.Text;
1112

1213
class Program
@@ -57,6 +58,13 @@ static int VerifyArgumentsAndRun()
5758
return 1;
5859
}
5960

61+
if (arguments.IsVersion)
62+
{
63+
var assembly = Assembly.GetExecutingAssembly();
64+
VersionWriter.Write(assembly);
65+
return 0;
66+
}
67+
6068
if (arguments.IsHelp)
6169
{
6270
HelpWriter.Write();

src/GitVersionExe/VersionWriter.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using System.Linq;
5+
using System.Reflection;
6+
7+
class VersionWriter
8+
{
9+
public static void Write(Assembly assembly)
10+
{
11+
WriteTo(assembly, Console.WriteLine);
12+
}
13+
14+
public static void WriteTo(Assembly assembly, Action<string> writeAction)
15+
{
16+
var version = GetAssemblyVersion(assembly);
17+
writeAction(version);
18+
}
19+
20+
private static string GetAssemblyVersion(Assembly assembly)
21+
{
22+
var attribute = assembly
23+
.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)
24+
.FirstOrDefault() as AssemblyInformationalVersionAttribute;
25+
26+
if (attribute != null)
27+
{
28+
return attribute.InformationalVersion;
29+
}
30+
31+
return assembly.GetName().Version.ToString();
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)