Implement SourceDir substitution for Payloads.
7160
Sean Hall committed
Jan 15, 2023 at 20:29 UTC
f4329d17538c14569ab7058c1c378aa5b2297b2c
2 files changed
+115
src/tools/heat/UtilFinalizeHarvesterMutator.cs
+35
@@ -25,6 +25,7 @@ namespace WixToolset.Harvesters
25
private Hashtable filePaths;
26
private ArrayList files;
27
private ArrayList registryValues;
28
+ private List<Wix.Payload> payloads;
29
private bool suppressCOMElements;
30
private bool suppressVB6COMElements;
31
private string preprocessorVariable;
@@ -40,6 +41,7 @@ namespace WixToolset.Harvesters
41
this.filePaths = new Hashtable();
42
this.files = new ArrayList();
43
this.registryValues = new ArrayList();
44
+ this.payloads = new List<Wix.Payload>();
45
}
46
47
/// <summary>
@@ -93,6 +95,7 @@ namespace WixToolset.Harvesters
95
this.filePaths.Clear();
96
this.files.Clear();
97
this.registryValues.Clear();
98
+ this.payloads.Clear();
99
100
// index elements in this wix document
101
this.IndexElement(wix);
@@ -100,6 +103,7 @@ namespace WixToolset.Harvesters
103
this.MutateDirectories();
104
this.MutateFiles();
105
this.MutateRegistryValues();
106
+ this.MutatePayloads();
107
108
// must occur after all the registry values have been formatted
109
this.MutateComponents();
@@ -131,6 +135,10 @@ namespace WixToolset.Harvesters
135
{
136
this.registryValues.Add(element);
137
}
138
+ else if (element is Wix.Payload payloadElement)
139
+ {
140
+ this.payloads.Add(payloadElement);
141
+ }
142
143
// index the child elements
144
if (element is Wix.IParentElement)
@@ -1005,6 +1013,33 @@ namespace WixToolset.Harvesters
1013
}
1014
}
1015
1016
+ /// <summary>
1017
+ /// Mutate the payloads.
1018
+ /// </summary>
1019
+ private void MutatePayloads()
1020
+ {
1021
+ string sourceDirSubstitution = this.preprocessorVariable;
1022
+ if (sourceDirSubstitution == null)
1023
+ {
1024
+ return;
1025
+ }
1026
+
1027
+ string prefix = "$(";
1028
+ if (sourceDirSubstitution.StartsWith("wix.", StringComparison.Ordinal))
1029
+ {
1030
+ prefix = "!(";
1031
+ }
1032
+ sourceDirSubstitution = String.Concat(prefix, sourceDirSubstitution, ")");
1033
+
1034
+ foreach (var payload in this.payloads)
1035
+ {
1036
+ if (payload.SourceFile != null && payload.SourceFile.StartsWith("SourceDir\\", StringComparison.Ordinal))
1037
+ {
1038
+ payload.SourceFile = payload.SourceFile.Substring(9).Insert(0, sourceDirSubstitution);
1039
+ }
1040
+ }
1041
+ }
1042
+
1043
/// <summary>
1044
/// Mutate an individual registry string, according to a collection of replacement items.
1045
/// </summary>
src/tools/test/WixToolsetTest.Heat/DirectoryToPayloadGroupFixture.cs
new
+80
@@ -0,0 +1,80 @@
1
+// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
+
3
+namespace WixToolsetTest.Heat
4
+{
5
+ using System;
6
+ using System.IO;
7
+ using System.Linq;
8
+ using WixInternal.TestSupport;
9
+ using Xunit;
10
+
11
+ public class DirectoryToPayloadGroupFixture
12
+ {
13
+ [Fact]
14
+ public void CanHarvestSimpleDirectory()
15
+ {
16
+ var folder = TestData.Get("TestData", "SingleFile");
17
+
18
+ using (var fs = new DisposableFileSystem())
19
+ {
20
+ var outputPath = Path.Combine(fs.GetFolder(), "out.wxs");
21
+
22
+ var args = new[]
23
+ {
24
+ "dir", folder,
25
+ "-generate", "payloadgroup",
26
+ "-o", outputPath
27
+ };
28
+
29
+ var result = HeatRunner.Execute(args);
30
+ result.AssertSuccess();
31
+
32
+ var wxs = File.ReadAllLines(outputPath).Select(s => s.Replace("\"", "'")).ToArray();
33
+ WixAssert.CompareLineByLine(new[]
34
+ {
35
+ "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
36
+ " <Fragment>",
37
+ " <PayloadGroup Id='TARGETDIR'>",
38
+ " <Payload SourceFile='SourceDir\\a.txt' />",
39
+ " </PayloadGroup>",
40
+ " </Fragment>",
41
+ "</Wix>",
42
+ }, wxs);
43
+ }
44
+ }
45
+
46
+ [Fact]
47
+ public void CanHarvestSimpleDirectoryWithSourceDirSubstitution()
48
+ {
49
+ var folder = TestData.Get("TestData", "SingleFile");
50
+
51
+ using (var fs = new DisposableFileSystem())
52
+ {
53
+ var outputPath = Path.Combine(fs.GetFolder(), "out.wxs");
54
+
55
+ var args = new[]
56
+ {
57
+ "dir", folder,
58
+ "-generate", "payloadgroup",
59
+ "-var", "var.RootDir",
60
+ "-o", outputPath
61
+ };
62
+
63
+ var result = HeatRunner.Execute(args);
64
+ result.AssertSuccess();
65
+
66
+ var wxs = File.ReadAllLines(outputPath).Select(s => s.Replace("\"", "'")).ToArray();
67
+ WixAssert.CompareLineByLine(new[]
68
+ {
69
+ "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
70
+ " <Fragment>",
71
+ " <PayloadGroup Id='TARGETDIR'>",
72
+ " <Payload SourceFile='$(var.RootDir)\\a.txt' />",
73
+ " </PayloadGroup>",
74
+ " </Fragment>",
75
+ "</Wix>",
76
+ }, wxs);
77
+ }
78
+ }
79
+ }
80
+}