aboutsummaryrefslogtreecommitdiff
path: root/src/test/burn/WixToolset.WixBA/Model.cs
blob: 0ca7fd8335a7670c19bf766509dd3b42a3a91474 (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
// 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.WixBA
{
    using System;
    using System.Collections.Generic;
    using System.Net;
    using WixToolset.BootstrapperApplicationApi;

    /// <summary>
    /// The model.
    /// </summary>
    public class Model
    {
        private const string BurnBundleInstallDirectoryVariable = "InstallFolder";
        private const string BurnBundleLayoutDirectoryVariable = "WixBundleLayoutDirectory";
        private const string BurnBundleVersionVariable = "WixBundleVersion";

        /// <summary>
        /// Creates a new model for the BA.
        /// </summary>
        /// <param name="bootstrapper">The BA.</param>
        public Model(WixBA bootstrapper)
        {
            this.BAManifest = bootstrapper.BAManifest;
            this.Bootstrapper = bootstrapper;
            this.Command = bootstrapper.Command;
            this.Engine = bootstrapper.Engine;
            this.Telemetry = new List<KeyValuePair<string, string>>();
            this.Version = this.Engine.GetVariableVersion(BurnBundleVersionVariable);
        }

        public IBootstrapperApplicationData BAManifest { get; }

        /// <summary>
        /// Gets the bootstrapper.
        /// </summary>
        public IDefaultBootstrapperApplication Bootstrapper { get; }

        /// <summary>
        /// Gets the bootstrapper command-line.
        /// </summary>
        public IBootstrapperCommand Command { get; }

        /// <summary>
        /// Gets the bootstrapper engine.
        /// </summary>
        public IEngine Engine { get; }

        /// <summary>
        /// Gets the key/value pairs used in telemetry.
        /// </summary>
        public List<KeyValuePair<string, string>> Telemetry { get; private set; }

        /// <summary>
        /// Get or set the final result of the installation.
        /// </summary>
        public int Result { get; set; }

        /// <summary>
        /// Get the version of the install.
        /// </summary>
        public string Version { get; private set; }

        /// <summary>
        /// Get or set the path where the bundle is installed.
        /// </summary>
        public string InstallDirectory
        {
            get
            {
                if (!this.Engine.ContainsVariable(BurnBundleInstallDirectoryVariable))
                {
                    return null;
                }

                return this.Engine.GetVariableString(BurnBundleInstallDirectoryVariable);
            }

            set
            {
                this.Engine.SetVariableString(BurnBundleInstallDirectoryVariable, value, false);
            }
        }

        /// <summary>
        /// Get or set the path for the layout to be created.
        /// </summary>
        public string LayoutDirectory
        {
            get
            {
                if (!this.Engine.ContainsVariable(BurnBundleLayoutDirectoryVariable))
                {
                    return null;
                }

                return this.Engine.GetVariableString(BurnBundleLayoutDirectoryVariable);
            }

            set
            {
                this.Engine.SetVariableString(BurnBundleLayoutDirectoryVariable, value, false);
            }
        }

        public LaunchAction PlannedAction { get; set; }

        /// <summary>
        /// Creates a correctly configured HTTP web request.
        /// </summary>
        /// <param name="uri">URI to connect to.</param>
        /// <returns>Correctly configured HTTP web request.</returns>
        public HttpWebRequest CreateWebRequest(string uri)
        {
#pragma warning disable SYSLIB0014 // Type or member is obsolete
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
#pragma warning restore SYSLIB0014 // Type or member is obsolete
            request.UserAgent = String.Concat("WixInstall", this.Version.ToString());

            return request;
        }

        /// <summary>
        /// Gets the display name for a package if possible.
        /// </summary>
        /// <param name="packageId">Identity of the package to find the display name.</param>
        /// <returns>Display name of the package if found or the package id if not.</returns>
        public string GetPackageName(string packageId)
        {
            return this.BAManifest.Bundle.Packages.TryGetValue(packageId, out var package) ? package.DisplayName : packageId;
        }
    }
}