Fix Issue 7100
Clean up some issues with ConversionLab Fix Issue 7100. Clean up some issues with the use of ConversionLab.
Ron Martin committed
Jan 22, 2023 at 21:25 UTC
6784a3e77d9780a43e583981c2e30379579a1915
5 files changed
+60
-20
src/wix/WixToolset.Converters/ConversionLab.cs
+45
-8
@@ -40,21 +40,58 @@ namespace WixToolset.Converters
40
}
41
}
42
43
- public void RemoveTargetElement()
43
+ public void InsertElementBeforeTargetElement(XElement newElement)
44
+ {
45
+ var index = this.index - 1;
46
+
47
+ if (0 <= index
48
+ && this.siblingNodes[index] is XText leadingText
49
+ && String.IsNullOrWhiteSpace(leadingText.Value))
50
+ {
51
+ this.siblingNodes.Insert(index, newElement);
52
+ this.siblingNodes.Insert(index, new XText(leadingText.Value));
53
+ this.index += 2;
54
+ }
55
+ }
56
+
57
+ private bool IsUniqueElement(XElement newElement)
58
+ {
59
+ foreach (XNode node in this.siblingNodes)
60
+ {
61
+ if (node is XElement element)
62
+ {
63
+ if (element.Name == newElement.Name)
64
+ {
65
+ return false;
66
+ }
67
+ }
68
+ }
69
+
70
+ return true;
71
+ }
72
+
73
+ public void InsertUniqueElementBeforeTargetElement(XElement newElement)
74
{
45
- if (this.index + 1 < this.siblingNodes.Count
46
- && this.siblingNodes[this.index + 1] is XText trailingText
47
- && String.IsNullOrWhiteSpace(trailingText.Value))
75
+ if (this.IsUniqueElement(newElement))
76
{
49
- this.siblingNodes.RemoveAt(this.index + 1);
77
+ this.InsertElementBeforeTargetElement(newElement);
78
}
79
+ }
80
+
81
+ public void RemoveTargetElement()
82
+ {
83
this.siblingNodes.RemoveAt(this.index);
52
- if (0 < this.index
53
- && this.siblingNodes[this.index - 1] is XText leadingText
84
+
85
+ var index = this.index - 1;
86
+
87
+ if (0 <= index
88
+ && this.siblingNodes[index] is XText leadingText
89
&& String.IsNullOrWhiteSpace(leadingText.Value))
90
{
56
- this.siblingNodes.RemoveAt(this.index - 1);
91
+ this.siblingNodes.RemoveAt(index);
92
}
93
+
94
+ this.RemoveOrphanTextNodes();
95
}
96
97
public void ReplaceTargetElement(XElement replacement)
src/wix/WixToolset.Converters/WixConverter.cs
+11
-10
@@ -563,9 +563,7 @@ namespace WixToolset.Converters
563
else if (node is XElement element)
564
{
565
this.ConvertElement(element);
566
-
566
var before = element.Nodes().ToList();
568
-
567
this.ConvertNodes(before, level + 1);
568
569
// If any nodes were added during the processing of the children,
@@ -1066,7 +1064,7 @@ namespace WixToolset.Converters
1064
{
1065
xCondition.Remove();
1066
element.Add(new XAttribute("Condition", text));
1069
- lab.RemoveOrphanTextNodes();
1067
+ lab. RemoveOrphanTextNodes();
1068
lab.AddCommentsAsSiblings(comments);
1069
}
1070
}
@@ -1280,6 +1278,7 @@ namespace WixToolset.Converters
1278
using (var lab = new ConversionLab(element))
1279
{
1280
element.Add(new XAttribute("Condition", text));
1281
+ lab.RemoveOrphanTextNodes();
1282
lab.AddCommentsAsSiblings(comments);
1283
}
1284
}
@@ -1610,13 +1609,15 @@ namespace WixToolset.Converters
1609
if (!String.IsNullOrEmpty(newElementName)
1610
&& this.OnInformation(ConverterTestType.ReferencesReplaced, element, "UI, custom action, and property reference {0} has been replaced with strongly-typed element.", id))
1611
{
1613
- this.XRoot.SetAttributeValue(XNamespace.Xmlns + newNamespaceName, newNamespace.NamespaceName);
1614
-
1615
- element.AddBeforeSelf(new XElement(newNamespace + newElementName));
1616
-
1617
- if (replace)
1612
+ using (var lab = new ConversionLab(element))
1613
{
1619
- element.Remove();
1614
+ this.XRoot.SetAttributeValue(XNamespace.Xmlns + newNamespaceName, newNamespace.NamespaceName);
1615
+ lab.InsertUniqueElementBeforeTargetElement(new XElement(newNamespace + newElementName));
1616
+
1617
+ if (replace)
1618
+ {
1619
+ lab.RemoveTargetElement();
1620
+ }
1621
}
1622
}
1623
}
@@ -2241,7 +2242,7 @@ namespace WixToolset.Converters
2242
element.Name = ns.GetName(element.Name.LocalName);
2243
}
2244
2244
- // Remove all the attributes and add them back to with their namespace updated (as necessary).
2245
+ // Remove all the attributes and add them back with their namespace updated (as necessary).
2246
IEnumerable<XAttribute> attributes = element.Attributes().ToList();
2247
element.RemoveAttributes();
2248
src/wix/test/WixToolsetTest.Converters/ConditionFixture.cs
+1
@@ -187,6 +187,7 @@ namespace WixToolsetTest.Converters
187
var actualLines = UnformattedDocumentLines(document);
188
WixAssert.CompareLineByLine(expected, actualLines);
189
}
190
+
191
[Fact]
192
public void FixPublishConditionWithComment()
193
{
src/wix/test/WixToolsetTest.Converters/PrereqPackageFixture.cs
+2
-1
@@ -29,7 +29,8 @@ namespace WixToolsetTest.Converters
29
var expected = new[]
30
{
31
"<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\" xmlns:bal=\"http://wixtoolset.org/schemas/v4/wxs/bal\">",
32
- " <Fragment><PackageGroup Id=\"NetFx452Web\">",
32
+ " <Fragment>",
33
+ " <PackageGroup Id=\"NetFx452Web\">",
34
" <ExePackage Id=\"NetFx452Web\" bal:PrereqPackage=\"yes\" bal:PrereqLicenseUrl=\"$(var.NetFx452EulaLink)\" />",
35
" </PackageGroup>",
36
" </Fragment>",
src/wix/test/WixToolsetTest.Converters/VSExtensionFixture.cs
+1
-1
@@ -28,7 +28,7 @@ namespace WixToolsetTest.Converters
28
"<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\" xmlns:vs=\"http://wixtoolset.org/schemas/v4/wxs/vs\">",
29
" <Fragment>",
30
" <vs:FindVisualStudio />",
31
- " <vs:FindVisualStudio /><PropertyRef Id=\"VS2022_BOOTSTRAPPER_PACKAGE_FOLDER\" />",
31
+ " <PropertyRef Id=\"VS2022_BOOTSTRAPPER_PACKAGE_FOLDER\" />",
32
" <CustomActionRef Id=\"VS2017Setup\" />",
33
" </Fragment>",
34
"</Wix>"