-
Notifications
You must be signed in to change notification settings - Fork 29
/
Deployer.cs
191 lines (147 loc) · 5.84 KB
/
Deployer.cs
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
182
183
184
185
186
187
188
189
190
191
// Library needed to connect to the Windows Phone X Emulator
namespace Winium.Mobile.Connectivity
{
#region
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Phone.Tools.Deploy;
using Microsoft.SmartDevice.Connectivity.Interface;
using Microsoft.SmartDevice.MultiTargeting.Connectivity;
using Winium.Mobile.Common.Exceptions;
using Winium.Mobile.Logging;
using DeviceInfo = Microsoft.Phone.Tools.Deploy.Patched.DeviceInfo;
using Utils = Microsoft.Phone.Tools.Deploy.Patched.Utils;
#endregion
/// <summary>
/// App Deploy for 8.1 or greater (uses Microsoft.Phone.Tools.Deploy shipped with Microsoft SDKs\Windows Phone\v8.1\Tools\AppDeploy)
/// </summary>
/// TODO: do not copy Microsoft.Phone.Tools.Deploy assembly on build. Set Copy Local to false and use specified path to assembly.
public class Deployer : IDeployer
{
#region Constants
private const int MaxRetries = 3;
#endregion
#region Fields
private readonly DeviceInfo deviceInfo;
private bool installed;
#endregion
#region Constructors and Destructors
public Deployer(string desiredDevice, bool strict)
{
this.deviceInfo = Devices.Instance.GetMatchingDevice(desiredDevice, strict);
if (this.deviceInfo == null)
{
throw new AutomationException(
string.Format(
"Could not find a device to launch. You requested '{0}', but the available devices were:\n{1}",
desiredDevice,
Devices.Instance));
}
var propertyInfo = this.deviceInfo.GetType().GetTypeInfo().GetDeclaredProperty("DeviceId");
var deviceId = (string)propertyInfo.GetValue(this.deviceInfo);
var connectableDevice =
new MultiTargetingConnectivity(CultureInfo.CurrentUICulture.LCID).GetConnectableDevice(deviceId);
this.Device = connectableDevice.Connect(true);
Logger.Info("Target emulator: '{0}'", this.DeviceName);
}
#endregion
#region Public Properties
public string DeviceName
{
get
{
return this.deviceInfo.ToString();
}
}
#endregion
#region Properties
private IDevice Device { get; set; }
private IRemoteApplication RemoteApplication { get; set; }
#endregion
#region Public Methods and Operators
public void Install(string appPath, List<string> dependencies)
{
this.InstallDependencies(dependencies);
this.InstallApp(appPath);
}
public void UsePreInstalledApplication(string appPath)
{
var appManifest = Utils.ReadAppManifestInfoFromPackage(appPath);
this.RemoteApplication = this.Device.GetApplication(appManifest.ProductId);
}
public void Launch()
{
this.Device.Activate();
Retry.WithRetry(() => this.RemoteApplication.Launch(), MaxRetries);
}
public void ReceiveFile(string isoStoreRoot, string sourceDeviceFilePath, string targetDesktopFilePath)
{
var isolatedStore = this.RemoteApplication.GetIsolatedStore(isoStoreRoot);
isolatedStore.ReceiveFile(sourceDeviceFilePath, targetDesktopFilePath, true);
}
public void SendFile(string isoStoreRoot, string sourceDesktopFilePath, string targetDeviceFilePath)
{
var isolatedStore = this.RemoteApplication.GetIsolatedStore(isoStoreRoot);
isolatedStore.SendFile(sourceDesktopFilePath, targetDeviceFilePath, true);
}
public void SendFiles(List<KeyValuePair<string, string>> files)
{
if (files == null || !files.Any())
{
return;
}
var isolatedStore = this.RemoteApplication.GetIsolatedStore("Local");
foreach (var file in files)
{
Logger.Debug("Sending file \"{0}\" to \"{1}\"", file.Key, file.Value);
isolatedStore.SendFile(file.Key, file.Value, true);
}
}
public void Terminate()
{
throw new NotImplementedException("Deployer.Terminate");
}
public void Uninstall()
{
if (!this.installed)
{
Logger.Debug("Could not uninstall application that is already uninstalled.");
return;
}
this.RemoteApplication.Uninstall();
this.RemoteApplication = null;
this.Device.Disconnect();
}
#endregion
#region Methods
private void InstallApp(string appPath)
{
var appManifestInfo = this.InstallApplicationPackage(appPath);
this.installed = true;
this.RemoteApplication = this.Device.GetApplication(appManifestInfo.ProductId);
}
private IAppManifestInfo InstallApplicationPackage(string path)
{
var appManifest = Utils.ReadAppManifestInfoFromPackage(path);
Utils.InstallApplication(this.deviceInfo, appManifest, DeploymentOptions.None, path);
Logger.Info("{0} was successfully deployed using Microsoft.Phone.Tools.Deploy", appManifest.Name);
return appManifest;
}
private void InstallDependencies(List<string> dependencies)
{
if (dependencies == null || !dependencies.Any())
{
return;
}
foreach (var dependency in dependencies)
{
this.InstallApplicationPackage(dependency);
}
}
#endregion
}
}