| 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 WixInternal.Core.TestPackage |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | using System.Xml; |
| 9 | using WixToolset.Data.Burn; |
| 10 | using WixToolset.Extensibility.Services; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// Class to extract bundle contents for testing. |
| 14 | /// </summary> |
| 15 | public class BundleExtractor |
| 16 | { |
| 17 | private const string BurnNamespace = "http://wixtoolset.org/schemas/v4/2008/Burn"; |
| 18 | private const string BADataFileName = "BootstrapperApplicationData.xml"; |
| 19 | private const string BootstrapperExtensionDataFileName = "BootstrapperExtensionData.xml"; |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Extracts the BA container. |
| 23 | /// </summary> |
| 24 | /// <param name="messaging"></param> |
| 25 | /// <param name="bundleFilePath">Path to the bundle.</param> |
| 26 | /// <param name="destinationFolderPath">Path to extract to.</param> |
| 27 | /// <param name="tempFolderPath">Temp path for extraction.</param> |
| 28 | /// <returns></returns> |
| 29 | public static ExtractBAContainerResult ExtractBAContainer(IMessaging messaging, string bundleFilePath, string destinationFolderPath, string tempFolderPath) |
| 30 | { |
| 31 | return ExtractAllContainers(messaging, bundleFilePath, destinationFolderPath, null, tempFolderPath); |
| 32 | } |
| 33 | |
| 34 | /// <summary> |
| 35 | /// Extracts the BA container. |
| 36 | /// </summary> |
| 37 | /// <param name="messaging"></param> |
| 38 | /// <param name="bundleFilePath">Path to the bundle.</param> |
| 39 | /// <param name="baFolderPath">Path to extract BA to.</param> |
| 40 | /// <param name="otherContainersFolderPath">Optional path to extract other attached containers to.</param> |
| 41 | /// <param name="tempFolderPath">Temp path for extraction.</param> |
| 42 | /// <returns></returns> |
| 43 | public static ExtractBAContainerResult ExtractAllContainers(IMessaging messaging, string bundleFilePath, string baFolderPath, string otherContainersFolderPath, string tempFolderPath) |
| 44 | { |
| 45 | Directory.CreateDirectory(tempFolderPath); |
| 46 | |
| 47 | var args = new List<string> |
| 48 | { |
| 49 | "burn", "extract", |
| 50 | "-intermediatefolder", tempFolderPath, |
| 51 | bundleFilePath, |
| 52 | "-oba", baFolderPath |
| 53 | }; |
| 54 | |
| 55 | if (!String.IsNullOrEmpty(otherContainersFolderPath)) |
| 56 | { |
| 57 | args.Add("-o"); |
| 58 | args.Add(otherContainersFolderPath); |
| 59 | } |
| 60 | |
| 61 | var runnerResult = WixRunner.Execute(args.ToArray()); |
| 62 | |
| 63 | var result = new ExtractBAContainerResult(); |
| 64 | |
| 65 | if (runnerResult.ExitCode == 0) |
| 66 | { |
| 67 | result.Success = true; |
| 68 | result.ManifestDocument = LoadBurnManifest(baFolderPath); |
| 69 | result.ManifestNamespaceManager = GetBurnNamespaceManager(result.ManifestDocument, "burn"); |
| 70 | |
| 71 | result.BADataDocument = LoadBAData(baFolderPath); |
| 72 | result.BADataNamespaceManager = GetBADataNamespaceManager(result.BADataDocument, "ba"); |
| 73 | |
| 74 | result.BootstrapperExtensionDataDocument = LoadBootstrapperExtensionData(baFolderPath); |
| 75 | result.BootstrapperExtensionDataNamespaceManager = GetBootstrapperExtensionDataNamespaceManager(result.BootstrapperExtensionDataDocument, "be"); |
| 76 | } |
| 77 | |
| 78 | return result; |
| 79 | } |
| 80 | |
| 81 | /// <summary> |
| 82 | /// Gets an <see cref="XmlNamespaceManager"/> for BootstrapperApplicationData.xml with the given prefix assigned to the root namespace. |
| 83 | /// </summary> |
| 84 | /// <param name="document"></param> |
| 85 | /// <param name="prefix"></param> |
| 86 | /// <returns></returns> |
| 87 | public static XmlNamespaceManager GetBADataNamespaceManager(XmlDocument document, string prefix) |
| 88 | { |
| 89 | var namespaceManager = new XmlNamespaceManager(document.NameTable); |
| 90 | namespaceManager.AddNamespace(prefix, BurnConstants.BootstrapperApplicationDataNamespace); |
| 91 | return namespaceManager; |
| 92 | } |
| 93 | |
| 94 | /// <summary> |
| 95 | /// Gets an <see cref="XmlNamespaceManager"/> for BootstrapperExtensionData.xml with the given prefix assigned to the root namespace. |
| 96 | /// </summary> |
| 97 | /// <param name="document"></param> |
| 98 | /// <param name="prefix"></param> |
| 99 | /// <returns></returns> |
| 100 | public static XmlNamespaceManager GetBootstrapperExtensionDataNamespaceManager(XmlDocument document, string prefix) |
| 101 | { |
| 102 | var namespaceManager = new XmlNamespaceManager(document.NameTable); |
| 103 | namespaceManager.AddNamespace(prefix, BurnConstants.BootstrapperExtensionDataNamespace); |
| 104 | return namespaceManager; |
| 105 | } |
| 106 | |
| 107 | /// <summary> |
| 108 | /// Gets an <see cref="XmlNamespaceManager"/> for the Burn manifest.xml with the given prefix assigned to the root namespace. |
| 109 | /// </summary> |
| 110 | /// <param name="document"></param> |
| 111 | /// <param name="prefix"></param> |
| 112 | /// <returns></returns> |
| 113 | public static XmlNamespaceManager GetBurnNamespaceManager(XmlDocument document, string prefix) |
| 114 | { |
| 115 | var namespaceManager = new XmlNamespaceManager(document.NameTable); |
| 116 | namespaceManager.AddNamespace(prefix, BurnNamespace); |
| 117 | return namespaceManager; |
| 118 | } |
| 119 | |
| 120 | /// <summary> |
| 121 | /// Loads an XmlDocument with the BootstrapperApplicationData.xml from the given folder that contains the contents of the BA container. |
| 122 | /// </summary> |
| 123 | /// <param name="baFolderPath"></param> |
| 124 | /// <returns></returns> |
| 125 | public static XmlDocument LoadBAData(string baFolderPath) |
| 126 | { |
| 127 | var document = new XmlDocument(); |
| 128 | document.Load(Path.Combine(baFolderPath, BADataFileName)); |
| 129 | return document; |
| 130 | } |
| 131 | |
| 132 | /// <summary> |
| 133 | /// Loads an XmlDocument with the BootstrapperApplicationData.xml from the given folder that contains the contents of the BA container. |
| 134 | /// </summary> |
| 135 | /// <param name="baFolderPath"></param> |
| 136 | /// <returns></returns> |
| 137 | public static XmlDocument LoadBootstrapperExtensionData(string baFolderPath) |
| 138 | { |
| 139 | var document = new XmlDocument(); |
| 140 | document.Load(Path.Combine(baFolderPath, BootstrapperExtensionDataFileName)); |
| 141 | return document; |
| 142 | } |
| 143 | |
| 144 | /// <summary> |
| 145 | /// Loads an XmlDocument with the BootstrapperApplicationData.xml from the given folder that contains the contents of the BA container. |
| 146 | /// </summary> |
| 147 | /// <param name="baFolderPath"></param> |
| 148 | /// <returns></returns> |
| 149 | public static XmlDocument LoadBurnManifest(string baFolderPath) |
| 150 | { |
| 151 | var document = new XmlDocument(); |
| 152 | document.Load(Path.Combine(baFolderPath, "manifest.xml")); |
| 153 | return document; |
| 154 | } |
| 155 | } |
| 156 | } |