-
Notifications
You must be signed in to change notification settings - Fork 652
/
WixVersionFileUpdater.cs
61 lines (53 loc) · 2.13 KB
/
WixVersionFileUpdater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.IO;
using System.Text;
using System.Xml;
using GitVersion.Logging;
using GitVersion.OutputVariables;
namespace GitVersion.VersionConverters.WixUpdater
{
public interface IWixVersionFileUpdater : IVersionConverter<WixVersionContext>
{
}
public class WixVersionFileUpdater : IWixVersionFileUpdater
{
private readonly IFileSystem fileSystem;
private readonly ILog log;
private string wixVersionFile;
public const string WixVersionFileName = "GitVersion_WixVersion.wxi";
public WixVersionFileUpdater(IFileSystem fileSystem, ILog log)
{
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
this.log = log ?? throw new ArgumentNullException(nameof(log));
}
public void Execute(VersionVariables variables, WixVersionContext context)
{
wixVersionFile = Path.Combine(context.WorkingDirectory, WixVersionFileName);
log.Info("Updating GitVersion_WixVersion.wxi");
var doc = new XmlDocument();
doc.LoadXml(GetWixFormatFromVersionVariables(variables));
var xmlDecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
var root = doc.DocumentElement;
doc.InsertBefore(xmlDecl, root);
using var fs = fileSystem.OpenWrite(wixVersionFile);
doc.Save(fs);
}
private static string GetWixFormatFromVersionVariables(VersionVariables variables)
{
var builder = new StringBuilder();
builder.Append("<Include xmlns=\"http://schemas.microsoft.com/wix/2006/wi\">\n");
var availableVariables = VersionVariables.AvailableVariables;
foreach (var variable in availableVariables)
{
variables.TryGetValue(variable, out var value);
builder.Append($"\t<?define {variable}=\"{value}\"?>\n");
}
builder.Append("</Include>\n");
return builder.ToString();
}
public void Dispose()
{
log.Info($"Done writing {wixVersionFile}");
}
}
}