summaryrefslogtreecommitdiff
path: root/src/api/burn/WixToolset.Mba.Core/OverridableVariables.cs
blob: 148acb34e4c63c24965cbf59504a044d2550e106 (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
// 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.Mba.Core
{
    using System;
    using System.Collections.Generic;
    using System.Xml;
    using System.Xml.XPath;

    /// <summary>
    /// Default implementation of <see cref="IOverridableVariables"/>.
    /// </summary>
    public class OverridableVariablesInfo : IOverridableVariables
    {
        /// <inheritdoc />
        public VariableCommandLineType CommandLineType { get; internal set; }

        /// <inheritdoc />
        public IDictionary<string, IOverridableVariableInfo> Variables { get; internal set; }

        internal OverridableVariablesInfo() { }

        /// <summary>
        /// Parses the overridable variable info from the BA manifest.
        /// </summary>
        /// <param name="root">XML root</param>
        /// <returns>The parsed information.</returns>
        public static IOverridableVariables ParseFromXml(XPathNavigator root)
        {
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(root.NameTable);
            namespaceManager.AddNamespace("p", BootstrapperApplicationData.XMLNamespace);
            XPathNavigator commandLineNode = root.SelectSingleNode("/p:BootstrapperApplicationData/p:CommandLine", namespaceManager);
            XPathNodeIterator nodes = root.Select("/p:BootstrapperApplicationData/p:WixStdbaOverridableVariable", namespaceManager);

            var overridableVariables = new OverridableVariablesInfo();

            if (commandLineNode == null)
            {
                throw new Exception("Failed to select command line information.");
            }

            string variablesValue = BootstrapperApplicationData.GetAttribute(commandLineNode, "Variables");

            if (variablesValue == null)
            {
                throw new Exception("Failed to get command line variable type.");
            }

            if (variablesValue.Equals("upperCase", StringComparison.InvariantCulture))
            {
                overridableVariables.CommandLineType = VariableCommandLineType.UpperCase;
            }
            else if (variablesValue.Equals("caseSensitive", StringComparison.InvariantCulture))
            {
                overridableVariables.CommandLineType = VariableCommandLineType.CaseSensitive;
            }
            else
            {
                throw new Exception(string.Format("Unknown command line variable type: '{0}'", variablesValue));
            }

            overridableVariables.Variables = new Dictionary<string, IOverridableVariableInfo>();

            foreach (XPathNavigator node in nodes)
            {
                var variable = new OverridableVariableInfo();

                string name = BootstrapperApplicationData.GetAttribute(node, "Name");
                if (name == null)
                {
                    throw new Exception("Failed to get name for overridable variable.");
                }
                variable.Name = name;

                overridableVariables.Variables.Add(variable.Name, variable);
            }

            return overridableVariables;
        }
    }
}