From 5df3b188583697ca6f3910224e2135e8bacc56f0 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Tue, 6 Apr 2021 13:30:49 -0700 Subject: Add StandardDirectory conversions Completes wixtoolset/issues#6416 --- src/WixToolset.Converters/WixConverter.cs | 63 ++++++++++++++- .../WixToolsetTest.Converters/DirectoryFixture.cs | 92 ++++++++++++++++++++++ 2 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/test/WixToolsetTest.Converters/DirectoryFixture.cs (limited to 'src') diff --git a/src/WixToolset.Converters/WixConverter.cs b/src/WixToolset.Converters/WixConverter.cs index 1ef57fe7..80784807 100644 --- a/src/WixToolset.Converters/WixConverter.cs +++ b/src/WixToolset.Converters/WixConverter.cs @@ -12,6 +12,7 @@ namespace WixToolset.Converters using System.Xml; using System.Xml.Linq; using WixToolset.Data; + using WixToolset.Data.WindowsInstaller; using WixToolset.Extensibility.Services; /// @@ -120,6 +121,7 @@ namespace WixToolset.Converters private static readonly XName ShortcutPropertyElementName = WixNamespace + "ShortcutProperty"; private static readonly XName SoftwareTagElementName = WixNamespace + "SoftwareTag"; private static readonly XName SoftwareTagRefElementName = WixNamespace + "SoftwareTagRef"; + private static readonly XName StandardDirectoryElementName = WixNamespace + "StandardDirectory"; private static readonly XName TagElementName = XNamespace.None + "Tag"; private static readonly XName TagRefElementName = XNamespace.None + "TagRef"; private static readonly XName TextElementName = WixNamespace + "Text"; @@ -468,7 +470,18 @@ namespace WixToolset.Converters { this.ConvertElement(element); - this.ConvertNodes(element.Nodes(), level + 1); + var before = element.Nodes().ToList(); + + this.ConvertNodes(before, level + 1); + + // If any nodes were added during the processing of the children, + // ensure those added children get processed as well. + var added = element.Nodes().Except(before).ToList(); + + if (added.Any()) + { + this.ConvertNodes(added, level + 1); + } } } } @@ -950,6 +963,44 @@ namespace WixToolset.Converters } } } + + var id = element.Attribute("Id")?.Value; + + if (id == "TARGETDIR" && + this.OnError(ConverterTestType.TargetDirDeprecated, element, "The TARGETDIR directory should not longer be explicitly defined. Remove the Directory element with Id attribute 'TARGETDIR'.")) + { + var parentElement = element.Parent; + + element.Remove(); + + if (parentElement.FirstNode is XText text && String.IsNullOrWhiteSpace(text.Value)) + { + parentElement.FirstNode.Remove(); + } + + foreach (var child in element.Nodes()) + { + parentElement.Add(child); + } + + element.RemoveAll(); + + if (parentElement.FirstNode is XText textAgain && String.IsNullOrWhiteSpace(textAgain.Value)) + { + parentElement.FirstNode.Remove(); + } + } + else if (id != null && + WindowsInstallerStandard.IsStandardDirectory(id) && + this.OnError(ConverterTestType.DefiningStandardDirectoryDeprecated, element, "Standard directories such as '{0}' should no longer be defined using the Directory element. Use the StandardDirectory element instead.", id)) + { + element.Name = StandardDirectoryElementName; + + foreach (var attrib in element.Attributes().Where(a => a.Name.LocalName != "Id").ToList()) + { + attrib.Remove(); + } + } } private void ConvertFeatureElement(XElement element) @@ -2254,6 +2305,16 @@ namespace WixToolset.Converters /// The SoftwareTag element's Type attribute is obsolete. /// SoftwareTagTypeObsolete, + + /// + /// TARGETDIR directory should not longer be explicitly defined. + /// + TargetDirDeprecated, + + /// + /// Standard directories should no longer be defined using the Directory element. + /// + DefiningStandardDirectoryDeprecated, } } } diff --git a/src/test/WixToolsetTest.Converters/DirectoryFixture.cs b/src/test/WixToolsetTest.Converters/DirectoryFixture.cs new file mode 100644 index 00000000..3c906320 --- /dev/null +++ b/src/test/WixToolsetTest.Converters/DirectoryFixture.cs @@ -0,0 +1,92 @@ +// 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. + +namespace WixToolsetTest.Converters +{ + using System; + using System.Xml.Linq; + using WixBuildTools.TestSupport; + using WixToolset.Converters; + using WixToolsetTest.Converters.Mocks; + using Xunit; + + public class DirectoryFixture : BaseConverterFixture + { + [Fact] + public void RemoveTargetDir() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + ""); + + var expected = new[] + { + "", + " ", + " ", + " ", + " ", + " ", + " ", + "" + }; + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new MockMessaging(); + var converter = new WixConverter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + Assert.Equal(3, errors); + + var actualLines = UnformattedDocumentLines(document); + WixAssert.CompareLineByLine(expected, actualLines); + } + + [Fact] + public void FixStandardDirectory() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + ""); + + var expected = new[] + { + "", + " ", + " ", + " ", + " ", + " ", + "" + }; + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new MockMessaging(); + var converter = new WixConverter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + Assert.Equal(4, errors); + + var actualLines = UnformattedDocumentLines(document); + WixAssert.CompareLineByLine(expected, actualLines); + } + } +} -- cgit v1.2.3-55-g6feb