diff options
Diffstat (limited to 'src/test/burn/WixToolset.WixBA/WixBA.cs')
-rw-r--r-- | src/test/burn/WixToolset.WixBA/WixBA.cs | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/src/test/burn/WixToolset.WixBA/WixBA.cs b/src/test/burn/WixToolset.WixBA/WixBA.cs new file mode 100644 index 00000000..2d680c7e --- /dev/null +++ b/src/test/burn/WixToolset.WixBA/WixBA.cs | |||
@@ -0,0 +1,229 @@ | |||
1 | // 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. | ||
2 | |||
3 | namespace WixToolset.WixBA | ||
4 | { | ||
5 | using System; | ||
6 | using System.Collections.Generic; | ||
7 | using System.Diagnostics; | ||
8 | using System.IO; | ||
9 | using System.Net; | ||
10 | using System.Text; | ||
11 | using WixToolset.Mba.Core; | ||
12 | |||
13 | using Threading = System.Windows.Threading; | ||
14 | using WinForms = System.Windows.Forms; | ||
15 | |||
16 | /// <summary> | ||
17 | /// The WiX toolset bootstrapper application. | ||
18 | /// </summary> | ||
19 | public class WixBA : BootstrapperApplication | ||
20 | { | ||
21 | public WixBA(IEngine engine, IBootstrapperCommand command) | ||
22 | : base(engine) | ||
23 | { | ||
24 | this.Command = command; | ||
25 | |||
26 | this.BAManifest = new BootstrapperApplicationData(); | ||
27 | } | ||
28 | |||
29 | internal IBootstrapperApplicationData BAManifest { get; } | ||
30 | |||
31 | internal IBootstrapperCommand Command { get; } | ||
32 | |||
33 | internal IEngine Engine => this.engine; | ||
34 | |||
35 | /// <summary> | ||
36 | /// Gets the global model. | ||
37 | /// </summary> | ||
38 | static public Model Model { get; private set; } | ||
39 | |||
40 | /// <summary> | ||
41 | /// Gets the global view. | ||
42 | /// </summary> | ||
43 | static public RootView View { get; private set; } | ||
44 | // TODO: We should refactor things so we dont have a global View. | ||
45 | |||
46 | /// <summary> | ||
47 | /// Gets the global dispatcher. | ||
48 | /// </summary> | ||
49 | static public Threading.Dispatcher Dispatcher { get; private set; } | ||
50 | |||
51 | /// <summary> | ||
52 | /// Launches the default web browser to the provided URI. | ||
53 | /// </summary> | ||
54 | /// <param name="uri">URI to open the web browser.</param> | ||
55 | public static void LaunchUrl(string uri) | ||
56 | { | ||
57 | WixBA.UseShellExecute(uri); | ||
58 | } | ||
59 | |||
60 | /// <summary> | ||
61 | /// Open a log file. | ||
62 | /// </summary> | ||
63 | /// <param name="uri">URI to a log file.</param> | ||
64 | internal static void OpenLog(Uri uri) | ||
65 | { | ||
66 | WixBA.UseShellExecute(uri.ToString()); | ||
67 | } | ||
68 | |||
69 | /// <summary> | ||
70 | /// Open a log folder. | ||
71 | /// </summary> | ||
72 | /// <param name="string">path to a log folder.</param> | ||
73 | internal static void OpenLogFolder(string logFolder) | ||
74 | { | ||
75 | WixBA.UseShellExecute(logFolder); | ||
76 | } | ||
77 | |||
78 | /// <summary> | ||
79 | /// Open a log folder. | ||
80 | /// </summary> | ||
81 | /// <param name="uri">path to a log folder.</param> | ||
82 | private static void UseShellExecute(string path) | ||
83 | { | ||
84 | // Switch the wait cursor since shellexec can take a second or so. | ||
85 | System.Windows.Input.Cursor cursor = WixBA.View.Cursor; | ||
86 | WixBA.View.Cursor = System.Windows.Input.Cursors.Wait; | ||
87 | Process process = null; | ||
88 | try | ||
89 | { | ||
90 | process = new Process(); | ||
91 | process.StartInfo.FileName = path; | ||
92 | process.StartInfo.UseShellExecute = true; | ||
93 | process.StartInfo.Verb = "open"; | ||
94 | |||
95 | process.Start(); | ||
96 | } | ||
97 | finally | ||
98 | { | ||
99 | if (null != process) | ||
100 | { | ||
101 | process.Dispose(); | ||
102 | } | ||
103 | // back to the original cursor. | ||
104 | WixBA.View.Cursor = cursor; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | /// <summary> | ||
109 | /// Starts planning the appropriate action. | ||
110 | /// </summary> | ||
111 | /// <param name="action">Action to plan.</param> | ||
112 | public static void Plan(LaunchAction action) | ||
113 | { | ||
114 | WixBA.Model.PlannedAction = action; | ||
115 | WixBA.Model.Engine.Plan(WixBA.Model.PlannedAction); | ||
116 | } | ||
117 | |||
118 | public static void PlanLayout() | ||
119 | { | ||
120 | // Either default or set the layout directory | ||
121 | if (String.IsNullOrEmpty(WixBA.Model.Command.LayoutDirectory)) | ||
122 | { | ||
123 | WixBA.Model.LayoutDirectory = Directory.GetCurrentDirectory(); | ||
124 | |||
125 | // Ask the user for layout folder if one wasn't provided and we're in full UI mode | ||
126 | if (WixBA.Model.Command.Display == Display.Full) | ||
127 | { | ||
128 | WixBA.Dispatcher.Invoke((Action)delegate() | ||
129 | { | ||
130 | WinForms.FolderBrowserDialog browserDialog = new WinForms.FolderBrowserDialog(); | ||
131 | browserDialog.RootFolder = Environment.SpecialFolder.MyComputer; | ||
132 | |||
133 | // Default to the current directory. | ||
134 | browserDialog.SelectedPath = WixBA.Model.LayoutDirectory; | ||
135 | WinForms.DialogResult result = browserDialog.ShowDialog(); | ||
136 | |||
137 | if (WinForms.DialogResult.OK == result) | ||
138 | { | ||
139 | WixBA.Model.LayoutDirectory = browserDialog.SelectedPath; | ||
140 | WixBA.Plan(WixBA.Model.Command.Action); | ||
141 | } | ||
142 | else | ||
143 | { | ||
144 | WixBA.View.Close(); | ||
145 | } | ||
146 | } | ||
147 | ); | ||
148 | } | ||
149 | } | ||
150 | else | ||
151 | { | ||
152 | WixBA.Model.LayoutDirectory = WixBA.Model.Command.LayoutDirectory; | ||
153 | WixBA.Plan(WixBA.Model.Command.Action); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | /// <summary> | ||
158 | /// Thread entry point for WiX Toolset Bootstrapper Application. | ||
159 | /// </summary> | ||
160 | protected override void Run() | ||
161 | { | ||
162 | this.Engine.Log(LogLevel.Verbose, "Running the WiX BA."); | ||
163 | WixBA.Model = new Model(this); | ||
164 | WixBA.Dispatcher = Threading.Dispatcher.CurrentDispatcher; | ||
165 | RootViewModel viewModel = new RootViewModel(); | ||
166 | |||
167 | // Kick off detect which will populate the view models. | ||
168 | this.Engine.Detect(); | ||
169 | |||
170 | // Create a Window to show UI. | ||
171 | if (WixBA.Model.Command.Display == Display.Passive || | ||
172 | WixBA.Model.Command.Display == Display.Full) | ||
173 | { | ||
174 | this.Engine.Log(LogLevel.Verbose, "Creating a UI."); | ||
175 | WixBA.View = new RootView(viewModel); | ||
176 | WixBA.View.Show(); | ||
177 | } | ||
178 | |||
179 | Threading.Dispatcher.Run(); | ||
180 | |||
181 | this.PostTelemetry(); | ||
182 | this.Engine.Quit(WixBA.Model.Result); | ||
183 | } | ||
184 | |||
185 | private void PostTelemetry() | ||
186 | { | ||
187 | string result = String.Concat("0x", WixBA.Model.Result.ToString("x")); | ||
188 | |||
189 | StringBuilder telemetryData = new StringBuilder(); | ||
190 | foreach (KeyValuePair<string, string> kvp in WixBA.Model.Telemetry) | ||
191 | { | ||
192 | telemetryData.AppendFormat("{0}={1}+", kvp.Key, kvp.Value); | ||
193 | } | ||
194 | telemetryData.AppendFormat("Result={0}", result); | ||
195 | |||
196 | byte[] data = Encoding.UTF8.GetBytes(telemetryData.ToString()); | ||
197 | |||
198 | try | ||
199 | { | ||
200 | HttpWebRequest post = WixBA.Model.CreateWebRequest(String.Format(WixDistribution.TelemetryUrlFormat, WixBA.Model.Version.ToString(), result)); | ||
201 | post.Method = "POST"; | ||
202 | post.ContentType = "application/x-www-form-urlencoded"; | ||
203 | post.ContentLength = data.Length; | ||
204 | |||
205 | using (Stream postStream = post.GetRequestStream()) | ||
206 | { | ||
207 | postStream.Write(data, 0, data.Length); | ||
208 | } | ||
209 | |||
210 | HttpWebResponse response = (HttpWebResponse)post.GetResponse(); | ||
211 | } | ||
212 | catch (ArgumentException) | ||
213 | { | ||
214 | } | ||
215 | catch (FormatException) | ||
216 | { | ||
217 | } | ||
218 | catch (OverflowException) | ||
219 | { | ||
220 | } | ||
221 | catch (ProtocolViolationException) | ||
222 | { | ||
223 | } | ||
224 | catch (WebException) | ||
225 | { | ||
226 | } | ||
227 | } | ||
228 | } | ||
229 | } | ||