Skip to content
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

Adding support to generate WiX version files #1599

Merged
merged 5 commits into from
Feb 27, 2019
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
3 changes: 3 additions & 0 deletions docs/usage/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,8 @@ It will not change config file 'GitVersion.yml'.
### Example: How to override configuration option 'tag-prefix' to use prefix 'custom'
`GitVersion.exe /output json /overrideconfig tag-prefix=custom`

## Writing version metadata in WiX format
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.

## Mono
To use on mac or linux, install `mono-complete` then just run `mono GitVersion.exe`
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<Include xmlns="http://schemas.microsoft.com/wix/2006/wi">
<?define AssemblySemFileVer="1.2.3.0"?>
<?define AssemblySemVer="1.2.3.0"?>
<?define BranchName="develop"?>
<?define BuildMetaData="5"?>
<?define BuildMetaDataPadded="05"?>
<?define CommitDate="2019-02-20"?>
<?define CommitsSinceVersionSource="5"?>
<?define CommitsSinceVersionSourcePadded="0005"?>
<?define FullBuildMetaData="5.Branch.develop.Sha.commitSha"?>
<?define FullSemVer="1.2.3+5"?>
<?define InformationalVersion="1.2.3+5.Branch.develop.Sha.commitSha"?>
<?define LegacySemVer="1.2.3"?>
<?define LegacySemVerPadded="1.2.3"?>
<?define Major="1"?>
<?define MajorMinorPatch="1.2.3"?>
<?define Minor="2"?>
<?define NuGetPreReleaseTag=""?>
<?define NuGetPreReleaseTagV2=""?>
<?define NuGetVersion="1.2.3"?>
<?define NuGetVersionV2="1.2.3"?>
<?define Patch="3"?>
<?define PreReleaseLabel=""?>
<?define PreReleaseNumber=""?>
<?define PreReleaseTag=""?>
<?define PreReleaseTagWithDash=""?>
<?define SemVer="1.2.3"?>
<?define Sha="commitSha"?>
<?define ShortSha="commitShortSha"?>
</Include>
54 changes: 54 additions & 0 deletions src/GitVersionCore.Tests/WixFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace GitVersionCore.Tests
{
using System;
using System.IO;
using System.Text;
using GitVersion;
using NUnit.Framework;
using Shouldly;

[TestFixture]
[Parallelizable(ParallelScope.None)]
class WixFileTests
{
[SetUp]
public void Setup()
{
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>();
}

[Test]
[Category("NoMono")]
[Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")]
public void UpdateWixVersionFile()
{
var fileSystem = new TestFileSystem();
var workingDir = Path.GetTempPath();
var semVer = new SemanticVersion
{
Major = 1,
Minor = 2,
Patch = 3,
BuildMetaData = "5.Branch.develop"
};

semVer.BuildMetaData.Sha = "commitSha";
semVer.BuildMetaData.ShortSha = "commitShortSha";
semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2019-02-20 23:59:59Z");

var config = new TestEffectiveConfiguration(buildMetaDataPadding: 2, legacySemVerPadding: 5);
var vars = VariableProvider.GetVariablesFor(semVer, config, false);

StringBuilder log = new StringBuilder();
Action<string> action = s => log.AppendLine(s);
Logger.SetLoggers(action, action, action, action);
using (var wixVersionFileUpdater = new WixVersionFileUpdater(workingDir, vars, fileSystem))
{
wixVersionFileUpdater.Update();
}

fileSystem.ReadAllText(WixVersionFileUpdater.GetWixVersionFileName()).
ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved")));
}
}
}
65 changes: 65 additions & 0 deletions src/GitVersionCore/WixVersionFileUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace GitVersion
{
using Helpers;

using System;
using System.Text;
using System.Xml;

public class WixVersionFileUpdater : IDisposable
{
string workingDirectory;
VersionVariables variables;
IFileSystem fileSystem;
private const string WIX_VERSION_FILE = "GitVersion_WixVersion.wxi ";

public WixVersionFileUpdater(string workingDirectory, VersionVariables variables, IFileSystem fileSystem)
{
this.workingDirectory = workingDirectory;
this.variables = variables;
this.fileSystem = fileSystem;
}

public static string GetWixVersionFileName()
{
return WIX_VERSION_FILE;
}

public void Update()
{
Logger.WriteInfo("Updating GitVersion_WixVersion.wxi");

XmlDocument doc = new XmlDocument();
doc.LoadXml(GetWixFormatFromVersionVariables());

XmlDeclaration xmlDecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDecl, root);

using (var fs = fileSystem.OpenWrite(WIX_VERSION_FILE))
{
doc.Save(fs);
}
}

private string GetWixFormatFromVersionVariables()
{
StringBuilder builder = new StringBuilder();
builder.Append("<Include xmlns=\"http://schemas.microsoft.com/wix/2006/wi\">\n");
var availableVariables = VersionVariables.AvailableVariables;
foreach (var variable in availableVariables)
{
string value = null;
variables.TryGetValue(variable, out value);
builder.Append(string.Format("\t<?define {0}=\"{1}\"?>\n", variable, value));
}
builder.Append("</Include>\n");
return builder.ToString();
}

public void Dispose()
{
Logger.WriteInfo(string.Format("Done writing {0}", WIX_VERSION_FILE));
}
}
}
3 changes: 2 additions & 1 deletion src/GitVersionExe.Tests/HelpWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public void AllArgsAreInHelp()
{ "LogFilePath" , "/l" },
{ "DynamicRepositoryLocation" , "/dynamicRepoLocation" },
{ "IsHelp", "/?" },
{ "IsVersion", "/version" }
{ "IsVersion", "/version" },
{ "UpdateWixVersionFile", "/updatewixversionfile" }
};
string helpText = null;

Expand Down
109 changes: 109 additions & 0 deletions src/GitVersionExe.Tests/UpdateWixVersionFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
namespace GitVersionExe.Tests
{
using System.IO;
using System.Linq;
using NUnit.Framework;

using GitVersion;
using GitTools.Testing;
using System.Collections.Generic;
using System.Xml;

[TestFixture]
[Parallelizable(ParallelScope.None)]
class UpdateWixVersionFileTests
{
private string WixVersionFileName;

[SetUp]
public void Setup()
{
WixVersionFileName = WixVersionFileUpdater.GetWixVersionFileName();
}

[Test]
[Category("NoMono")]
[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.")]
public void WixVersionFileCreationTest()
{
using (var fixture = new EmptyRepositoryFixture())
{
fixture.MakeATaggedCommit("1.2.3");
fixture.MakeACommit();

GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " -updatewixversionfile");
Assert.IsTrue(File.Exists(Path.Combine(fixture.RepositoryPath, WixVersionFileName)));
}
}

[Test]
[Category("NoMono")]
[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.")]
public void WixVersionFileVarCountTest()
{
//Make sure we have captured all the version variables by count in the Wix version file
using (var fixture = new EmptyRepositoryFixture())
{
fixture.MakeATaggedCommit("1.2.3");
fixture.MakeACommit();

var gitVersionExecutionResults = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: null);
VersionVariables vars = gitVersionExecutionResults.OutputVariables;

GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " -updatewixversionfile");

var gitVersionVarsInWix = GetGitVersionVarsInWixFile(Path.Combine(fixture.RepositoryPath, WixVersionFileName));
var gitVersionVars = VersionVariables.AvailableVariables;

Assert.AreEqual(gitVersionVars.Count(), gitVersionVarsInWix.Count);
}
}

[Test]
[Category("NoMono")]
[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.")]
public void WixVersionFileContentTest()
{
using (var fixture = new EmptyRepositoryFixture())
{
fixture.MakeATaggedCommit("1.2.3");
fixture.MakeACommit();

var gitVersionExecutionResults = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: null);
VersionVariables vars = gitVersionExecutionResults.OutputVariables;

GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " -updatewixversionfile");

var gitVersionVarsInWix = GetGitVersionVarsInWixFile(Path.Combine(fixture.RepositoryPath, WixVersionFileName));
var gitVersionVars = VersionVariables.AvailableVariables;

foreach (var variable in gitVersionVars)
{
string value = null;
vars.TryGetValue(variable, out value);
//Make sure the variable is present in the Wix file
Assert.IsTrue(gitVersionVarsInWix.ContainsKey(variable));
//Make sure the values are equal
Assert.AreEqual(value, gitVersionVarsInWix[variable]);
}
}
}

private Dictionary<string, string> GetGitVersionVarsInWixFile(string file)
{
var gitVersionVarsInWix = new Dictionary<string, string>();
using (var reader = new XmlTextReader(file))
{
while (reader.Read())
{
if (reader.Name == "define")
{
string[] component = reader.Value.Split('=');
gitVersionVarsInWix[component[0]] = component[1].TrimStart('"').TrimEnd('"');
}
}
}
return gitVersionVarsInWix;
}
}
}
6 changes: 6 additions & 0 deletions src/GitVersionExe/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
continue;
}

if (name.IsSwitch("updatewixversionfile"))
{
arguments.UpdateWixVersionFile = true;
continue;
}

var couldNotParseMessage = string.Format("Could not parse command line parameter '{0}'.", name);

// 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.
Expand Down
2 changes: 2 additions & 0 deletions src/GitVersionExe/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public Arguments()
public ISet<string> UpdateAssemblyInfoFileName;
public bool EnsureAssemblyInfo;

public bool UpdateWixVersionFile;

public bool ShowConfig;
public bool NoFetch;
public bool NoCache;
Expand Down
8 changes: 7 additions & 1 deletion src/GitVersionExe/HelpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ Specify name of AssemblyInfo file. Can also /updateAssemblyInfo GlobalAssemblyIn
it be created with these attributes: AssemblyFileVersion, AssemblyVersion and AssemblyInformationalVersion
---
Supports writing version info for: C#, F#, VB
# Remote repository args

# Create or update Wix version file
/updatewixversionfile
All the GitVersion variables are written to 'GitVersion_WixVersion.wxi'.
The variables can then be referenced in other WiX project files for versioning.

# Remote repository args
/url Url to remote git repository.
/b Name of the branch to use on the remote repository, must be used in combination with /url.
/u Username in case authentication is required.
Expand Down
8 changes: 8 additions & 0 deletions src/GitVersionExe/SpecifiedArgumentRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public static void Run(Arguments arguments, IFileSystem fileSystem)
}
}

if (arguments.UpdateWixVersionFile)
{
using (var wixVersionFileUpdater = new WixVersionFileUpdater(targetPath, variables, fileSystem))
{
wixVersionFileUpdater.Update();
}
}

using (var assemblyInfoUpdater = new AssemblyInfoFileUpdater(arguments.UpdateAssemblyInfoFileName, targetPath, variables, fileSystem, arguments.EnsureAssemblyInfo))
{
if (arguments.UpdateAssemblyInfo)
Expand Down