@joebigelow / wix / commits / f2e5bdc2

Remove DirectoryRef to TARGETDIR

Fixes 7061

Rob Mensching committed Dec 6, 2022 at 14:10 UTC f2e5bdc263b8f6def149c918c332bd0d66fb6c1f
3 files changed +117 -4
src/wix/WixToolset.Converters/WixConverter.cs
+37
@@ -92,6 +92,7 @@ namespace WixToolset.Converters
92 private static readonly XName OldRequiresElementName = WixDependencyNamespace + "Requires";
93 private static readonly XName OldRequiresRefElementName = WixDependencyNamespace + "RequiresRef";
94 private static readonly XName DirectoryElementName = WixNamespace + "Directory";
95 + private static readonly XName DirectoryRefElementName = WixNamespace + "DirectoryRef";
96 private static readonly XName EmbeddedChainerElementName = WixNamespace + "EmbeddedChainer";
97 private static readonly XName ErrorElementName = WixNamespace + "Error";
98 private static readonly XName FeatureElementName = WixNamespace + "Feature";
@@ -288,6 +289,7 @@ namespace WixToolset.Converters
289 { WixConverter.CustomTableElementName, this.ConvertCustomTableElement },
290 { WixConverter.DataElementName, this.ConvertDataElement },
291 { WixConverter.DirectoryElementName, this.ConvertDirectoryElement },
292 + { WixConverter.DirectoryRefElementName, this.ConvertDirectoryRefElement },
293 { WixConverter.FeatureElementName, this.ConvertFeatureElement },
294 { WixConverter.FileElementName, this.ConvertFileElement },
295 { WixConverter.FragmentElementName, this.ConvertFragmentElement },
@@ -1129,6 +1131,36 @@ namespace WixToolset.Converters
1131 }
1132 }
1133
1134 + private void ConvertDirectoryRefElement(XElement element)
1135 + {
1136 + var id = element.Attribute("Id")?.Value;
1137 +
1138 + if (id == "TARGETDIR" &&
1139 + this.OnInformation(ConverterTestType.TargetDirRefDeprecated, element, "The TARGETDIR directory should not longer be explicitly referenced. Remove the DirectoryRef element with Id attribute 'TARGETDIR'."))
1140 + {
1141 + var parentElement = element.Parent;
1142 +
1143 + element.Remove();
1144 +
1145 + if (parentElement.FirstNode is XText text && String.IsNullOrWhiteSpace(text.Value))
1146 + {
1147 + parentElement.FirstNode.Remove();
1148 + }
1149 +
1150 + foreach (var child in element.Nodes())
1151 + {
1152 + parentElement.Add(child);
1153 + }
1154 +
1155 + element.RemoveAll();
1156 +
1157 + if (parentElement.FirstNode is XText textAgain && String.IsNullOrWhiteSpace(textAgain.Value))
1158 + {
1159 + parentElement.FirstNode.Remove();
1160 + }
1161 + }
1162 + }
1163 +
1164 private void ConvertFeatureElement(XElement element)
1165 {
1166 var xAbsent = element.Attribute("Absent");
@@ -3144,6 +3176,11 @@ namespace WixToolset.Converters
3176 /// Custom action ids have changed in WiX v4 extensions. Because WiX v4 has platform-specific custom actions, the platform is applied as a suffix: _X86, _X64, _A64 (Arm64). When manually rescheduling custom actions, you must use the new custom action id, with platform suffix.
3177 /// </summary>
3178 CustomActionIdsIncludePlatformSuffix,
3179 +
3180 + /// <summary>
3181 + /// The TARGETDIR directory should not longer be explicitly referenced.
3182 + /// </summary>
3183 + TargetDirRefDeprecated,
3184 }
3185 }
3186 }
src/wix/test/WixToolsetTest.Converters/ConverterFixture.cs
+2 -4
@@ -282,10 +282,8 @@ namespace WixToolsetTest.Converters
282 "<Include xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
283 " <?define Version = 1.2.3 ?>",
284 " <Fragment>",
285 - " <DirectoryRef Id=\"TARGETDIR\">",
285 " <Directory Id=\"ANOTHERDIR\" Name=\"Another\" />",
287 - " </DirectoryRef>",
288 - " </Fragment>",
286 + " </Fragment>",
287 "</Include>",
288 };
289
@@ -298,9 +296,9 @@ namespace WixToolsetTest.Converters
296
297 var actual = UnformattedDocumentLines(document);
298
301 - Assert.Equal(2, errors);
299 WixAssert.CompareLineByLine(expected, actual);
300 Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace());
301 + Assert.Equal(3, errors);
302 }
303
304 [Fact]
src/wix/test/WixToolsetTest.Converters/DirectoryFixture.cs
+78
@@ -45,10 +45,50 @@ namespace WixToolsetTest.Converters
45 var converter = new WixConverter(messaging, 2, null, null);
46
47 var errors = converter.ConvertDocument(document);
48 +
49 + var actualLines = UnformattedDocumentLines(document);
50 + WixAssert.CompareLineByLine(expected, actualLines);
51 Assert.Equal(3, errors);
52 + }
53 +
54 + [Fact]
55 + public void RemoveTargetDirRef()
56 + {
57 + var parse = String.Join(Environment.NewLine,
58 + "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
59 + "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>",
60 + " <Fragment>",
61 + " <DirectoryRef Id='TARGETDIR'>",
62 + " <!-- Comment -->",
63 + " <Directory Id='RootFolder' Name='Root'>",
64 + " <Directory Id='ChildFolder' Name='Child' />",
65 + " </Directory>",
66 + " </DirectoryRef>",
67 + " </Fragment>",
68 + "</Wix>");
69 +
70 + var expected = new[]
71 + {
72 + "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
73 + " <Fragment>",
74 + " <!-- Comment -->",
75 + " <Directory Id=\"RootFolder\" Name=\"Root\">",
76 + " <Directory Id=\"ChildFolder\" Name=\"Child\" />",
77 + " </Directory>",
78 + " </Fragment>",
79 + "</Wix>"
80 + };
81 +
82 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
83 +
84 + var messaging = new MockMessaging();
85 + var converter = new WixConverter(messaging, 2, null, null);
86 +
87 + var errors = converter.ConvertDocument(document);
88
89 var actualLines = UnformattedDocumentLines(document);
90 WixAssert.CompareLineByLine(expected, actualLines);
91 + Assert.Equal(3, errors);
92 }
93
94 [Fact]
@@ -83,10 +123,48 @@ namespace WixToolsetTest.Converters
123 var converter = new WixConverter(messaging, 2, null, null);
124
125 var errors = converter.ConvertDocument(document);
126 +
127 + var actualLines = UnformattedDocumentLines(document);
128 + WixAssert.CompareLineByLine(expected, actualLines);
129 Assert.Equal(4, errors);
130 + }
131 +
132 + [Fact]
133 + public void RemoveTargetDirRefAndFixStandardDirectory()
134 + {
135 + var parse = String.Join(Environment.NewLine,
136 + "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
137 + "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>",
138 + " <Fragment>",
139 + " <DirectoryRef Id='TARGETDIR'>",
140 + " <Directory Id='ProgramFilesFolder' Name='PFiles'>",
141 + " <Directory Id='ChildFolder' Name='Child' />",
142 + " </Directory>",
143 + " </DirectoryRef>",
144 + " </Fragment>",
145 + "</Wix>");
146 +
147 + var expected = new[]
148 + {
149 + "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
150 + " <Fragment>",
151 + " <StandardDirectory Id=\"ProgramFilesFolder\">",
152 + " <Directory Id=\"ChildFolder\" Name=\"Child\" />",
153 + " </StandardDirectory>",
154 + " </Fragment>",
155 + "</Wix>"
156 + };
157 +
158 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
159 +
160 + var messaging = new MockMessaging();
161 + var converter = new WixConverter(messaging, 2, null, null);
162 +
163 + var errors = converter.ConvertDocument(document);
164
165 var actualLines = UnformattedDocumentLines(document);
166 WixAssert.CompareLineByLine(expected, actualLines);
167 + Assert.Equal(4, errors);
168 }
169 }
170 }