Skip to content

Commit 482dfce

Browse files
authored
Merge pull request #1599 from ruhullahshah/feature/support_for_wix_format
Adding support to generate WiX version files
2 parents 3d645ad + 336a7f4 commit 482dfce

File tree

10 files changed

+287
-2
lines changed

10 files changed

+287
-2
lines changed

docs/usage/command-line.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,8 @@ It will not change config file 'GitVersion.yml'.
8080
### Example: How to override configuration option 'tag-prefix' to use prefix 'custom'
8181
`GitVersion.exe /output json /overrideconfig tag-prefix=custom`
8282

83+
## Writing version metadata in WiX format
84+
To support integration with WiX projects, use `GitVersion.exe /updatewixversionfile`. All the [variables](../more-info/variables.md) are written to `GitVersion_WixVersion.wxi` under the current working directory and can be referenced in the WiX project files.
85+
8386
## Mono
8487
To use on mac or linux, install `mono-complete` then just run `mono GitVersion.exe`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Include xmlns="http://schemas.microsoft.com/wix/2006/wi">
3+
<?define AssemblySemFileVer="1.2.3.0"?>
4+
<?define AssemblySemVer="1.2.3.0"?>
5+
<?define BranchName="develop"?>
6+
<?define BuildMetaData="5"?>
7+
<?define BuildMetaDataPadded="05"?>
8+
<?define CommitDate="2019-02-20"?>
9+
<?define CommitsSinceVersionSource="5"?>
10+
<?define CommitsSinceVersionSourcePadded="0005"?>
11+
<?define FullBuildMetaData="5.Branch.develop.Sha.commitSha"?>
12+
<?define FullSemVer="1.2.3+5"?>
13+
<?define InformationalVersion="1.2.3+5.Branch.develop.Sha.commitSha"?>
14+
<?define LegacySemVer="1.2.3"?>
15+
<?define LegacySemVerPadded="1.2.3"?>
16+
<?define Major="1"?>
17+
<?define MajorMinorPatch="1.2.3"?>
18+
<?define Minor="2"?>
19+
<?define NuGetPreReleaseTag=""?>
20+
<?define NuGetPreReleaseTagV2=""?>
21+
<?define NuGetVersion="1.2.3"?>
22+
<?define NuGetVersionV2="1.2.3"?>
23+
<?define Patch="3"?>
24+
<?define PreReleaseLabel=""?>
25+
<?define PreReleaseNumber=""?>
26+
<?define PreReleaseTag=""?>
27+
<?define PreReleaseTagWithDash=""?>
28+
<?define SemVer="1.2.3"?>
29+
<?define Sha="commitSha"?>
30+
<?define ShortSha="commitShortSha"?>
31+
</Include>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace GitVersionCore.Tests
2+
{
3+
using System;
4+
using System.IO;
5+
using System.Text;
6+
using GitVersion;
7+
using NUnit.Framework;
8+
using Shouldly;
9+
10+
[TestFixture]
11+
[Parallelizable(ParallelScope.None)]
12+
class WixFileTests
13+
{
14+
[SetUp]
15+
public void Setup()
16+
{
17+
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>();
18+
}
19+
20+
[Test]
21+
[Category("NoMono")]
22+
[Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")]
23+
public void UpdateWixVersionFile()
24+
{
25+
var fileSystem = new TestFileSystem();
26+
var workingDir = Path.GetTempPath();
27+
var semVer = new SemanticVersion
28+
{
29+
Major = 1,
30+
Minor = 2,
31+
Patch = 3,
32+
BuildMetaData = "5.Branch.develop"
33+
};
34+
35+
semVer.BuildMetaData.Sha = "commitSha";
36+
semVer.BuildMetaData.ShortSha = "commitShortSha";
37+
semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2019-02-20 23:59:59Z");
38+
39+
var config = new TestEffectiveConfiguration(buildMetaDataPadding: 2, legacySemVerPadding: 5);
40+
var vars = VariableProvider.GetVariablesFor(semVer, config, false);
41+
42+
StringBuilder log = new StringBuilder();
43+
Action<string> action = s => log.AppendLine(s);
44+
Logger.SetLoggers(action, action, action, action);
45+
using (var wixVersionFileUpdater = new WixVersionFileUpdater(workingDir, vars, fileSystem))
46+
{
47+
wixVersionFileUpdater.Update();
48+
}
49+
50+
fileSystem.ReadAllText(WixVersionFileUpdater.GetWixVersionFileName()).
51+
ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved")));
52+
}
53+
}
54+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace GitVersion
2+
{
3+
using Helpers;
4+
5+
using System;
6+
using System.Text;
7+
using System.Xml;
8+
9+
public class WixVersionFileUpdater : IDisposable
10+
{
11+
string workingDirectory;
12+
VersionVariables variables;
13+
IFileSystem fileSystem;
14+
private const string WIX_VERSION_FILE = "GitVersion_WixVersion.wxi ";
15+
16+
public WixVersionFileUpdater(string workingDirectory, VersionVariables variables, IFileSystem fileSystem)
17+
{
18+
this.workingDirectory = workingDirectory;
19+
this.variables = variables;
20+
this.fileSystem = fileSystem;
21+
}
22+
23+
public static string GetWixVersionFileName()
24+
{
25+
return WIX_VERSION_FILE;
26+
}
27+
28+
public void Update()
29+
{
30+
Logger.WriteInfo("Updating GitVersion_WixVersion.wxi");
31+
32+
XmlDocument doc = new XmlDocument();
33+
doc.LoadXml(GetWixFormatFromVersionVariables());
34+
35+
XmlDeclaration xmlDecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
36+
XmlElement root = doc.DocumentElement;
37+
doc.InsertBefore(xmlDecl, root);
38+
39+
using (var fs = fileSystem.OpenWrite(WIX_VERSION_FILE))
40+
{
41+
doc.Save(fs);
42+
}
43+
}
44+
45+
private string GetWixFormatFromVersionVariables()
46+
{
47+
StringBuilder builder = new StringBuilder();
48+
builder.Append("<Include xmlns=\"http://schemas.microsoft.com/wix/2006/wi\">\n");
49+
var availableVariables = VersionVariables.AvailableVariables;
50+
foreach (var variable in availableVariables)
51+
{
52+
string value = null;
53+
variables.TryGetValue(variable, out value);
54+
builder.Append(string.Format("\t<?define {0}=\"{1}\"?>\n", variable, value));
55+
}
56+
builder.Append("</Include>\n");
57+
return builder.ToString();
58+
}
59+
60+
public void Dispose()
61+
{
62+
Logger.WriteInfo(string.Format("Done writing {0}", WIX_VERSION_FILE));
63+
}
64+
}
65+
}

src/GitVersionExe.Tests/HelpWriterTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public void AllArgsAreInHelp()
1717
{ "LogFilePath" , "/l" },
1818
{ "DynamicRepositoryLocation" , "/dynamicRepoLocation" },
1919
{ "IsHelp", "/?" },
20-
{ "IsVersion", "/version" }
20+
{ "IsVersion", "/version" },
21+
{ "UpdateWixVersionFile", "/updatewixversionfile" }
2122
};
2223
string helpText = null;
2324

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
namespace GitVersionExe.Tests
2+
{
3+
using System.IO;
4+
using System.Linq;
5+
using NUnit.Framework;
6+
7+
using GitVersion;
8+
using GitTools.Testing;
9+
using System.Collections.Generic;
10+
using System.Xml;
11+
12+
[TestFixture]
13+
[Parallelizable(ParallelScope.None)]
14+
class UpdateWixVersionFileTests
15+
{
16+
private string WixVersionFileName;
17+
18+
[SetUp]
19+
public void Setup()
20+
{
21+
WixVersionFileName = WixVersionFileUpdater.GetWixVersionFileName();
22+
}
23+
24+
[Test]
25+
[Category("NoMono")]
26+
[Description("Doesn't work on Mono/Unix because of the path heuristics that needs to be done there in order to figure out whether the first argument actually is a path.")]
27+
public void WixVersionFileCreationTest()
28+
{
29+
using (var fixture = new EmptyRepositoryFixture())
30+
{
31+
fixture.MakeATaggedCommit("1.2.3");
32+
fixture.MakeACommit();
33+
34+
GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " -updatewixversionfile");
35+
Assert.IsTrue(File.Exists(Path.Combine(fixture.RepositoryPath, WixVersionFileName)));
36+
}
37+
}
38+
39+
[Test]
40+
[Category("NoMono")]
41+
[Description("Doesn't work on Mono/Unix because of the path heuristics that needs to be done there in order to figure out whether the first argument actually is a path.")]
42+
public void WixVersionFileVarCountTest()
43+
{
44+
//Make sure we have captured all the version variables by count in the Wix version file
45+
using (var fixture = new EmptyRepositoryFixture())
46+
{
47+
fixture.MakeATaggedCommit("1.2.3");
48+
fixture.MakeACommit();
49+
50+
var gitVersionExecutionResults = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: null);
51+
VersionVariables vars = gitVersionExecutionResults.OutputVariables;
52+
53+
GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " -updatewixversionfile");
54+
55+
var gitVersionVarsInWix = GetGitVersionVarsInWixFile(Path.Combine(fixture.RepositoryPath, WixVersionFileName));
56+
var gitVersionVars = VersionVariables.AvailableVariables;
57+
58+
Assert.AreEqual(gitVersionVars.Count(), gitVersionVarsInWix.Count);
59+
}
60+
}
61+
62+
[Test]
63+
[Category("NoMono")]
64+
[Description("Doesn't work on Mono/Unix because of the path heuristics that needs to be done there in order to figure out whether the first argument actually is a path.")]
65+
public void WixVersionFileContentTest()
66+
{
67+
using (var fixture = new EmptyRepositoryFixture())
68+
{
69+
fixture.MakeATaggedCommit("1.2.3");
70+
fixture.MakeACommit();
71+
72+
var gitVersionExecutionResults = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: null);
73+
VersionVariables vars = gitVersionExecutionResults.OutputVariables;
74+
75+
GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " -updatewixversionfile");
76+
77+
var gitVersionVarsInWix = GetGitVersionVarsInWixFile(Path.Combine(fixture.RepositoryPath, WixVersionFileName));
78+
var gitVersionVars = VersionVariables.AvailableVariables;
79+
80+
foreach (var variable in gitVersionVars)
81+
{
82+
string value = null;
83+
vars.TryGetValue(variable, out value);
84+
//Make sure the variable is present in the Wix file
85+
Assert.IsTrue(gitVersionVarsInWix.ContainsKey(variable));
86+
//Make sure the values are equal
87+
Assert.AreEqual(value, gitVersionVarsInWix[variable]);
88+
}
89+
}
90+
}
91+
92+
private Dictionary<string, string> GetGitVersionVarsInWixFile(string file)
93+
{
94+
var gitVersionVarsInWix = new Dictionary<string, string>();
95+
using (var reader = new XmlTextReader(file))
96+
{
97+
while (reader.Read())
98+
{
99+
if (reader.Name == "define")
100+
{
101+
string[] component = reader.Value.Split('=');
102+
gitVersionVarsInWix[component[0]] = component[1].TrimStart('"').TrimEnd('"');
103+
}
104+
}
105+
}
106+
return gitVersionVarsInWix;
107+
}
108+
}
109+
}

src/GitVersionExe/ArgumentParser.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
337337
continue;
338338
}
339339

340+
if (name.IsSwitch("updatewixversionfile"))
341+
{
342+
arguments.UpdateWixVersionFile = true;
343+
continue;
344+
}
345+
340346
var couldNotParseMessage = string.Format("Could not parse command line parameter '{0}'.", name);
341347

342348
// If we've reached through all argument switches without a match, we can relatively safely assume that the first argument isn't a switch, but the target path.

src/GitVersionExe/Arguments.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public Arguments()
4343
public ISet<string> UpdateAssemblyInfoFileName;
4444
public bool EnsureAssemblyInfo;
4545

46+
public bool UpdateWixVersionFile;
47+
4648
public bool ShowConfig;
4749
public bool NoFetch;
4850
public bool NoCache;

src/GitVersionExe/HelpWriter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ Specify name of AssemblyInfo file. Can also /updateAssemblyInfo GlobalAssemblyIn
4848
it be created with these attributes: AssemblyFileVersion, AssemblyVersion and AssemblyInformationalVersion
4949
---
5050
Supports writing version info for: C#, F#, VB
51-
# Remote repository args
51+
52+
# Create or update Wix version file
53+
/updatewixversionfile
54+
All the GitVersion variables are written to 'GitVersion_WixVersion.wxi'.
55+
The variables can then be referenced in other WiX project files for versioning.
56+
57+
# Remote repository args
5258
/url Url to remote git repository.
5359
/b Name of the branch to use on the remote repository, must be used in combination with /url.
5460
/u Username in case authentication is required.

src/GitVersionExe/SpecifiedArgumentRunner.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public static void Run(Arguments arguments, IFileSystem fileSystem)
5454
}
5555
}
5656

57+
if (arguments.UpdateWixVersionFile)
58+
{
59+
using (var wixVersionFileUpdater = new WixVersionFileUpdater(targetPath, variables, fileSystem))
60+
{
61+
wixVersionFileUpdater.Update();
62+
}
63+
}
64+
5765
using (var assemblyInfoUpdater = new AssemblyInfoFileUpdater(arguments.UpdateAssemblyInfoFileName, targetPath, variables, fileSystem, arguments.EnsureAssemblyInfo))
5866
{
5967
if (arguments.UpdateAssemblyInfo)

0 commit comments

Comments
 (0)