aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.TestPackage/BundleExtractor.cs
blob: ad97f1132b76614a8a9410265b7af5174473b0eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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 WixToolset.Core.TestPackage
{
    using System.IO;
    using System.Xml;
    using WixToolset.Core.Burn.Bundles;
    using WixToolset.Extensibility.Services;

    /// <summary>
    /// Class to extract bundle contents for testing.
    /// </summary>
    public class BundleExtractor
    {
        /// <summary>
        /// Extracts the BA container.
        /// </summary>
        /// <param name="messaging"></param>
        /// <param name="bundleFilePath">Path to the bundle.</param>
        /// <param name="destinationFolderPath">Path to extract to.</param>
        /// <param name="tempFolderPath">Temp path for extraction.</param>
        /// <returns></returns>
        public static ExtractBAContainerResult ExtractBAContainer(IMessaging messaging, string bundleFilePath, string destinationFolderPath, string tempFolderPath)
        {
            var result = new ExtractBAContainerResult();
            Directory.CreateDirectory(tempFolderPath);
            using (var burnReader = BurnReader.Open(messaging, bundleFilePath))
            {
                result.Success = burnReader.ExtractUXContainer(destinationFolderPath, tempFolderPath);
            }

            if (result.Success)
            {
                result.ManifestDocument = LoadBurnManifest(destinationFolderPath);
                result.ManifestNamespaceManager = GetBurnNamespaceManager(result.ManifestDocument, "burn");

                result.BADataDocument = LoadBAData(destinationFolderPath);
                result.BADataNamespaceManager = GetBADataNamespaceManager(result.BADataDocument, "ba");

                result.BundleExtensionDataDocument = LoadBundleExtensionData(destinationFolderPath);
                result.BundleExtensionDataNamespaceManager = GetBundleExtensionDataNamespaceManager(result.BundleExtensionDataDocument, "be");
            }

            return result;
        }

        /// <summary>
        /// Gets an <see cref="XmlNamespaceManager"/> for BootstrapperApplicationData.xml with the given prefix assigned to the root namespace.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static XmlNamespaceManager GetBADataNamespaceManager(XmlDocument document, string prefix)
        {
            var namespaceManager = new XmlNamespaceManager(document.NameTable);
            namespaceManager.AddNamespace(prefix, BurnCommon.BADataNamespace);
            return namespaceManager;
        }

        /// <summary>
        /// Gets an <see cref="XmlNamespaceManager"/> for BundleExtensionData.xml with the given prefix assigned to the root namespace.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static XmlNamespaceManager GetBundleExtensionDataNamespaceManager(XmlDocument document, string prefix)
        {
            var namespaceManager = new XmlNamespaceManager(document.NameTable);
            namespaceManager.AddNamespace(prefix, BurnCommon.BundleExtensionDataNamespace);
            return namespaceManager;
        }

        /// <summary>
        /// Gets an <see cref="XmlNamespaceManager"/> for the Burn manifest.xml with the given prefix assigned to the root namespace.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static XmlNamespaceManager GetBurnNamespaceManager(XmlDocument document, string prefix)
        {
            var namespaceManager = new XmlNamespaceManager(document.NameTable);
            namespaceManager.AddNamespace(prefix, BurnCommon.BurnNamespace);
            return namespaceManager;
        }

        /// <summary>
        /// Loads an XmlDocument with the BootstrapperApplicationData.xml from the given folder that contains the contents of the BA container.
        /// </summary>
        /// <param name="baFolderPath"></param>
        /// <returns></returns>
        public static XmlDocument LoadBAData(string baFolderPath)
        {
            var document = new XmlDocument();
            document.Load(Path.Combine(baFolderPath, BurnCommon.BADataFileName));
            return document;
        }

        /// <summary>
        /// Loads an XmlDocument with the BootstrapperApplicationData.xml from the given folder that contains the contents of the BA container.
        /// </summary>
        /// <param name="baFolderPath"></param>
        /// <returns></returns>
        public static XmlDocument LoadBundleExtensionData(string baFolderPath)
        {
            var document = new XmlDocument();
            document.Load(Path.Combine(baFolderPath, BurnCommon.BundleExtensionDataFileName));
            return document;
        }

        /// <summary>
        /// Loads an XmlDocument with the BootstrapperApplicationData.xml from the given folder that contains the contents of the BA container.
        /// </summary>
        /// <param name="baFolderPath"></param>
        /// <returns></returns>
        public static XmlDocument LoadBurnManifest(string baFolderPath)
        {
            var document = new XmlDocument();
            document.Load(Path.Combine(baFolderPath, "manifest.xml"));
            return document;
        }
    }
}