Skip to content

Adding support to generate WiX version files #1386

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

Closed
Show file tree
Hide file tree
Changes from 3 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`
3 changes: 2 additions & 1 deletion src/GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
<Compile Include="SemanticVersionBuildMetaData.cs" />
<Compile Include="SemanticVersionPreReleaseTag.cs" />
<Compile Include="BuildServers\TravisCI.cs" />
<Compile Include="WixVersionFileUpdater.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="GitVersionInformationResources\AddFormats\GitVersionInformation.cs" />
Expand Down Expand Up @@ -232,4 +233,4 @@
</Target>
<Import Project="..\packages\PepitaPackage.1.21.4\build\PepitaPackage.targets" Condition="Exists('..\packages\PepitaPackage.1.21.4\build\PepitaPackage.targets')" />
<Import Project="..\packages\Fody.2.0.8\build\portable-net+sl+win+wpa+wp\Fody.targets" Condition="Exists('..\packages\Fody.2.0.8\build\portable-net+sl+win+wpa+wp\Fody.targets')" />
</Project>
</Project>
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));
}
}
}
1 change: 1 addition & 0 deletions src/GitVersionExe.Tests/GitVersionExe.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<Compile Include="MsBuildProjectArgTest.cs" />
<Compile Include="PullRequestInJenkinsPipelineTest.cs" />
<Compile Include="PullRequestInTeamCityTest.cs" />
<Compile Include="UpdateWixVersionFileTests.cs" />
<Compile Include="VersionWriterTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
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
102 changes: 102 additions & 0 deletions src/GitVersionExe.Tests/UpdateWixVersionFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
namespace GitVersionExe.Tests
Copy link
Member

Choose a reason for hiding this comment

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

Why only GitVersionExe tests and not for GitVersionCore? Also, don't you agree approval tests such as this is better suited for file manipulation testing such as this?

public void ShouldCreateAssemblyInfoFileWhenNotExistsAndEnsureAssemblyInfo(string fileExtension)
{
var fileSystem = new TestFileSystem();
var workingDir = Path.GetTempPath();
var assemblyInfoFile = "VersionAssemblyInfo." + fileExtension;
var fullPath = Path.Combine(workingDir, assemblyInfoFile);
var variables = VariableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", "v"), new TestEffectiveConfiguration(), false);
using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFile, workingDir, variables, fileSystem, true))
{
assemblyInfoFileUpdater.Update();
fileSystem.ReadAllText(fullPath).ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved", fileExtension)));
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you please explain this in detail?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, sure. Approval tests allows you to create a file on disk that is what the test is compared against. If the content matches, the test is successful, otherwise it fails and a diff is presented.

The above test will verify the manipulated file content against the following approved text file:

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by GitVersion.
//
// You can modify this code as we will not overwrite it when re-executing GitVersion
// </auto-generated>
//------------------------------------------------------------------------------
using System.Reflection;
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]

{
using System.IO;
using System.Linq;
using NUnit.Framework;

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

[TestFixture]
class UpdateWixVersionFileTests
{
private string WixVersionFileName;

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

[Test]
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]
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]
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 @@ -338,6 +338,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 @@ -44,6 +44,8 @@ public Arguments()
public ISet<string> UpdateAssemblyInfoFileName;
public bool EnsureAssemblyInfo;

public bool UpdateWixVersionFile;

public bool ShowConfig;
public bool NoFetch;
public bool NoCache;
Expand Down
6 changes: 6 additions & 0 deletions src/GitVersionExe/HelpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ 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

# 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.
Expand Down
8 changes: 8 additions & 0 deletions src/GitVersionExe/SpecifiedArgumentRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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