aboutsummaryrefslogtreecommitdiff
path: root/src/api/burn/WixToolset.BootstrapperApplicationApi/BundleInfo.cs
blob: 14cac881ced4669470bd4965a46c421c0c4dfabf (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
// 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.BootstrapperApplicationApi
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;
    using System.Xml.XPath;

    /// <summary>
    /// Default implementation of <see cref="IBundleInfo"/>.
    /// </summary>
    public class BundleInfo : IBundleInfo
    {
        /// <inheritdoc/>
        public bool PerMachine { get; internal set; }

        /// <inheritdoc/>
        public string Name { get; internal set; }

        /// <inheritdoc/>
        public string LogVariable { get; internal set; }

        /// <inheritdoc/>
        public IOverridableVariables OverridableVariables { get; internal set; }

        /// <inheritdoc/>
        public IDictionary<string, IPackageInfo> Packages { get; internal set; }

        internal BundleInfo()
        {
            this.Packages = new Dictionary<string, IPackageInfo>();
        }

        /// <inheritdoc/>
        public IPackageInfo AddRelatedBundleAsPackage(string productCode, RelationType relationType, bool perMachine, string version)
        {
            var package = PackageInfo.GetRelatedBundleAsPackage(productCode, relationType, perMachine, version);
            this.Packages.Add(package.Id, package);
            return package;
        }

        /// <inheritdoc/>
        public IPackageInfo AddUpdateBundleAsPackage(string packageId)
        {
            var package = PackageInfo.GetUpdateBundleAsPackage(packageId);
            this.Packages.Add(package.Id, package);
            return package;
        }

        /// <summary>
        /// Parses BA manifest from the given stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static IBundleInfo ParseBundleFromStream(Stream stream)
        {
            XPathDocument manifest = new XPathDocument(stream);
            XPathNavigator root = manifest.CreateNavigator();
            return ParseBundleFromXml(root);
        }

        /// <summary>
        /// Parses BA manifest from the given <see cref="XPathNavigator"/>.
        /// </summary>
        /// <param name="root">The root of the BA manifest.</param>
        /// <returns></returns>
        public static IBundleInfo ParseBundleFromXml(XPathNavigator root)
        {
            BundleInfo bundle = new BundleInfo();

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(root.NameTable);
            namespaceManager.AddNamespace("p", BootstrapperApplicationData.XMLNamespace);
            XPathNavigator bundleNode = root.SelectSingleNode("/p:BootstrapperApplicationData/p:WixBundleProperties", namespaceManager);

            if (bundleNode == null)
            {
                throw new Exception("Failed to select bundle information.");
            }

            bool? perMachine = BootstrapperApplicationData.GetYesNoAttribute(bundleNode, "PerMachine");
            if (perMachine.HasValue)
            {
                bundle.PerMachine = perMachine.Value;
            }

            bundle.Name = BootstrapperApplicationData.GetAttribute(bundleNode, "DisplayName");

            bundle.LogVariable = BootstrapperApplicationData.GetAttribute(bundleNode, "LogPathVariable");

            bundle.OverridableVariables = OverridableVariablesInfo.ParseFromXml(root);

            bundle.Packages = PackageInfo.ParsePackagesFromXml(root);

            return bundle;
        }
    }
}