aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Mba.Core/PackageInfo.cs
blob: 99ebb33a05755d4bd32067b417f3efa0b8fa5302 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 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.BootstrapperCore
{
    using System;
    using System.Collections.Generic;
    using System.Xml;
    using System.Xml.XPath;

    public enum CacheType
    {
        No,
        Yes,
        Always,
    }

    public enum PackageType
    {
        Unknown,
        Exe,
        Msi,
        Msp,
        Msu,
        UpgradeBundle,
        AddonBundle,
        PatchBundle,
    }

    public class PackageInfo : IPackageInfo
    {
        public string Id { get; internal set; }
        public string DisplayName { get; internal set; }
        public string Description { get; internal set; }
        public PackageType Type { get; internal set; }
        public bool Permanent { get; internal set; }
        public bool Vital { get; internal set; }
        public bool DisplayInternalUI { get; internal set; }
        public string ProductCode { get; internal set; }
        public string UpgradeCode { get; internal set; }
        public string Version { get; internal set; }
        public string InstallCondition { get; internal set; }
        public CacheType CacheType { get; internal set; }

        internal PackageInfo() { }

        public static IEnumerable<IPackageInfo> ParsePackagesFromXml(XPathNavigator root)
        {
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(root.NameTable);
            namespaceManager.AddNamespace("p", BootstrapperApplicationData.XMLNamespace);
            XPathNodeIterator nodes = root.Select("/p:BootstrapperApplicationData/p:WixPackageProperties", namespaceManager);

            foreach (XPathNavigator node in nodes)
            {
                var package = new PackageInfo();

                string id = BootstrapperApplicationData.GetAttribute(node, "Package");
                if (id == null)
                {
                    throw new Exception("Failed to get package identifier for package.");
                }
                package.Id = id;

                package.DisplayName = BootstrapperApplicationData.GetAttribute(node, "DisplayName");

                package.Description = BootstrapperApplicationData.GetAttribute(node, "Description");

                PackageType? packageType = GetPackageTypeAttribute(node, "PackageType");
                if (!packageType.HasValue)
                {
                    throw new Exception("Failed to get package type for package.");
                }
                package.Type = packageType.Value;

                bool? permanent = BootstrapperApplicationData.GetYesNoAttribute(node, "Permanent");
                if (!permanent.HasValue)
                {
                    throw new Exception("Failed to get permanent settings for package.");
                }
                package.Permanent = permanent.Value;

                bool? vital = BootstrapperApplicationData.GetYesNoAttribute(node, "Vital");
                if (!vital.HasValue)
                {
                    throw new Exception("Failed to get vital setting for package.");
                }
                package.Vital = vital.Value;

                bool? displayInternalUI = BootstrapperApplicationData.GetYesNoAttribute(node, "DisplayInternalUI");
                package.DisplayInternalUI = displayInternalUI.HasValue && displayInternalUI.Value;

                package.ProductCode = BootstrapperApplicationData.GetAttribute(node, "ProductCode");

                package.UpgradeCode = BootstrapperApplicationData.GetAttribute(node, "UpgradeCode");

                package.Version = BootstrapperApplicationData.GetAttribute(node, "Version");

                package.InstallCondition = BootstrapperApplicationData.GetAttribute(node, "InstallCondition");

                yield return package;
            }
        }

        public static CacheType? GetCacheTypeAttribute(XPathNavigator node, string attributeName)
        {
            string attributeValue = BootstrapperApplicationData.GetAttribute(node, attributeName);

            if (attributeValue == null)
            {
                return null;
            }

            if (attributeValue.Equals("yes", StringComparison.InvariantCulture))
            {
                return CacheType.Yes;
            }
            else if (attributeValue.Equals("always", StringComparison.InvariantCulture))
            {
                return CacheType.Always;
            }
            else
            {
                return CacheType.No;
            }
        }

        public static PackageType? GetPackageTypeAttribute(XPathNavigator node, string attributeName)
        {
            string attributeValue = BootstrapperApplicationData.GetAttribute(node, attributeName);

            if (attributeValue == null)
            {
                return null;
            }

            if (attributeValue.Equals("Exe", StringComparison.InvariantCulture))
            {
                return PackageType.Exe;
            }
            else if (attributeValue.Equals("Msi", StringComparison.InvariantCulture))
            {
                return PackageType.Msi;
            }
            else if (attributeValue.Equals("Msp", StringComparison.InvariantCulture))
            {
                return PackageType.Msp;
            }
            else if (attributeValue.Equals("Msu", StringComparison.InvariantCulture))
            {
                return PackageType.Msu;
            }
            else
            {
                return PackageType.Unknown;
            }
        }

        public static PackageInfo GetRelatedBundleAsPackage(string id, RelationType relationType, bool perMachine, Version version)
        {
            PackageInfo package = new PackageInfo();
            package.Id = id;
            package.Version = version.ToString();

            switch (relationType)
            {
                case RelationType.Addon:
                    package.Type = PackageType.AddonBundle;
                    break;
                case RelationType.Patch:
                    package.Type = PackageType.PatchBundle;
                    break;
                case RelationType.Upgrade:
                    package.Type = PackageType.UpgradeBundle;
                    break;
                default:
                    throw new Exception(string.Format("Unknown related bundle type: {0}", relationType));
            }

            return package;
        }
    }
}