UNPKG

9.78 kBPlain TextView Raw
1using System;
2using System.Reflection;
3using System.Collections.Generic;
4using System.Collections.ObjectModel;
5using System.Globalization;
6using Microsoft.SmartDevice.Connectivity.Interface;
7using Microsoft.SmartDevice.MultiTargeting.Connectivity;
8using System.Net;
9
10namespace wptool
11{
12 class wptool
13 {
14 static int Main(string[] args) {
15 if (args.Length == 0) {
16 return showHelp();
17 }
18
19 string command = null;
20 string wpsdk = null;
21 string udid = null;
22 Guid appid = Guid.Empty;
23 int i;
24
25 for (i = 0; i < args.Length; i++) {
26 if (args[i] == "--wpsdk") {
27 if (i + 1 < args.Length) {
28 wpsdk = args[++i];
29 }
30 } else if (command == null) {
31 command = args[i];
32 if (command == "connect" && i + 1 < args.Length) {
33 udid = args[++i];
34 }
35 else if (command == "launch" && i + 1 < args.Length)
36 {
37 udid = args[++i];
38 string rawAppId = args[++i];
39 try
40 {
41 appid = Guid.Parse(rawAppId);
42 } catch (FormatException fe)
43 {
44 return showHelp("Invalid app GUID format: " + rawAppId);
45 }
46 }
47 }
48 }
49
50 if (wpsdk == null) {
51 wpsdk = "10.0";
52 } else {
53 // trim whatever version number they pass in to 2 digits
54 string[] parts = wpsdk.Split(new Char [] {'.'});
55 wpsdk = "";
56 i = 0;
57 while (i < 2) {
58 if (wpsdk.Length > 0) {
59 wpsdk += '.';
60 }
61 if (i < parts.Length) {
62 wpsdk += parts[i];
63 } else {
64 wpsdk += '0';
65 }
66 i++;
67 }
68 }
69 if (!wpsdk.Equals("10.0") && !wpsdk.Equals("8.1"))
70 {
71 return showHelp("Unsupported wpsdk value. Please use '10.0' or '8.1'.");
72 }
73
74 int localeId = CultureInfo.CurrentUICulture.LCID;
75 MultiTargetingConnectivity multiTargetingConnectivity = new MultiTargetingConnectivity(localeId);
76 Collection<ConnectableDevice> devices = multiTargetingConnectivity.GetConnectableDevices();
77
78 List<ConnectableDevice> deviceList = new List<ConnectableDevice>();
79 i = 0;
80 foreach (ConnectableDevice dev in devices) {
81 // Filter to device and emulators that match the given SDK. We use 6.3 Version as the marker for 8.1 emus, assume rest are 10.0
82 string versionString = dev.Version.ToString();
83 if (!dev.IsEmulator() || (wpsdk == "8.1" && versionString.Equals("6.3")) || (wpsdk == "10.0" && !versionString.Equals("6.3"))) {
84 deviceList.Add(dev);
85 i++;
86 }
87 }
88
89 if (command == "enumerate") {
90 int id = 0;
91 int j = 0;
92
93 Console.WriteLine("{");
94
95 Console.WriteLine("\t\"devices\": [");
96 foreach (ConnectableDevice dev in deviceList) {
97 string versionString = dev.Version.ToString();
98 if (!dev.IsEmulator()) {
99 if (j > 0) {
100 Console.WriteLine(",");
101 }
102 string sdk = "null";
103 if (versionString == "6.3") {
104 sdk = "\"8.1\"";
105 } else if (versionString == "10.0") {
106 sdk = "\"10.0\"";
107 // skip invalid device
108 } else if (versionString.StartsWith("2147483647")) {
109 continue;
110 }
111 Console.WriteLine("\t\t{\n");
112 Console.WriteLine("\t\t\t\"name\": \"" + dev.Name.Replace("\"", "\\\"") + "\",");
113 Console.WriteLine("\t\t\t\"udid\": " + id + ",");
114 Console.WriteLine("\t\t\t\"index\": " + id + ",");
115 Console.WriteLine("\t\t\t\"version\": \"" + versionString + "\","); // windows 8.1: "6.3", windows 10: "10.0"
116 Console.WriteLine("\t\t\t\"wpsdk\": " + sdk + ",");
117 Console.WriteLine("\t\t\t\"type\": \"device\"");
118 Console.Write("\t\t}");
119 j++;
120 }
121 id++;
122 }
123 Console.WriteLine("\n\t],");
124
125 id = 0;
126 j = 0;
127
128 Console.WriteLine("\t\"emulators\": [");
129 foreach (ConnectableDevice dev in deviceList) {
130 if (dev.IsEmulator()) {
131 if (j > 0) {
132 Console.WriteLine(",");
133 }
134
135 Console.WriteLine("\t\t{\n");
136 Console.WriteLine("\t\t\t\"name\": \"" + dev.Name.Replace("\"", "\\\"") + "\",");
137 Console.WriteLine("\t\t\t\"udid\": \"" + wpsdk.Replace('.', '-') + "-" + id + "\",");
138 Console.WriteLine("\t\t\t\"index\": " + id + ",");
139 Console.WriteLine("\t\t\t\"guid\": \"" + dev.Id + "\",");
140 Console.WriteLine("\t\t\t\"version\": \"" + dev.Version + "\","); // 6.3 for 8.1 emulators, 6.4 for 10 emulators, 2147483647.2147483647.2147483647.2147483647 for device
141 Console.WriteLine("\t\t\t\"uapVersion\": \"" + dev.UapVersion + "\","); // blank/empty for 8.1 emulators and device, 10.0.10586.0 for win 10 emulators
142 Console.WriteLine("\t\t\t\"wpsdk\": \"" + wpsdk + "\",");
143 Console.WriteLine("\t\t\t\"type\": \"emulator\"");
144 Console.Write("\t\t}");
145 j++;
146 }
147 id++;
148 }
149 Console.WriteLine("\n\t]");
150
151 Console.WriteLine("}");
152 return 0;
153 }
154
155 // This won't just connect, it will launch the emulator!
156 if (command == "connect" || command == "launch") {
157 if (udid == null) {
158 return showHelp("Missing device/emulator UDID");
159 }
160 // TODO Validate that the udid is either our generated udid (i.e. 10-0-1), or it's GUID, or it's an integer index value!
161
162 int id = 0;
163 // Search devices for udid!
164 ConnectableDevice connectableDevice = null;
165 foreach (ConnectableDevice dev in deviceList) {
166 // Is it a matching GUID, matching UDID or matching Index value?
167 if (dev.Id.Equals(udid) || (wpsdk.Replace('.', '-') + "-" + id).Equals(udid) || udid.Equals(id.ToString()))
168 {
169 connectableDevice = dev;
170 break;
171 }
172 id++;
173 }
174 if (connectableDevice == null)
175 {
176 return showHelp(String.Format("Invalid device UDID '{0:D}'", udid));
177 }
178
179 try {
180 IDevice device = connectableDevice.Connect();
181
182 if (command == "launch")
183 {
184 IRemoteApplication app = device.GetApplication(appid);
185 app.TerminateRunningInstances();
186 app.Launch();
187 Console.WriteLine("{");
188 Console.WriteLine("\t\"success\": true");
189 Console.WriteLine("}");
190 } else {
191 try {
192 string destinationIp;
193 string sourceIp;
194 int destinationPort;
195 device.GetEndPoints(0, out sourceIp, out destinationIp, out destinationPort);
196 var address = IPAddress.Parse(destinationIp);
197 ISystemInfo systemInfo = device.GetSystemInfo();
198
199 Version version = new Version(systemInfo.OSMajor, systemInfo.OSMinor, systemInfo.OSBuildNo);
200 Console.WriteLine("{");
201 Console.WriteLine("\t\"success\": true,");
202 Console.WriteLine("\t\"ip\": \"" + address.ToString() + "\",");
203 Console.WriteLine("\t\"port\": " + destinationPort.ToString() + ",");
204 Console.WriteLine("\t\"osVersion\": \"" + version.ToString() + "\",");
205 Console.WriteLine("\t\"availablePhysical\": " + systemInfo.AvailPhys.ToString() + ",");
206 Console.WriteLine("\t\"totalPhysical\": " + systemInfo.TotalPhys.ToString() + ",");
207 Console.WriteLine("\t\"availableVirtual\": " + systemInfo.AvailVirtual.ToString() + ",");
208 Console.WriteLine("\t\"totalVirtual\": " + systemInfo.TotalVirtual.ToString() + ",");
209 Console.WriteLine("\t\"architecture\": \"" + systemInfo.ProcessorArchitecture.ToString() + "\",");
210 Console.WriteLine("\t\"instructionSet\": \"" + systemInfo.InstructionSet.ToString() + "\",");
211 Console.WriteLine("\t\"processorCount\": " + systemInfo.NumberOfProcessors.ToString() + "");
212 Console.WriteLine("}");
213 } catch (Exception ex) {
214 //
215 // ConnectableDevice throws an error when connecting to a physical Windows 10 device
216 // physical Windows 10 devices can be connected to using 127.0.0.1.
217 // From some point of Windows 10 SDK, IDevice.GetEndPoints throws Exception for Windows Phone 8.1 device too.
218 // Interestingly ConnectableDevice.Version returns 8.1 _or_ 6.3 in this case.
219 // Returning ok for now because we can still connect to the device.
220 //
221 if (command == "connect" && !connectableDevice.IsEmulator()) {
222 Console.WriteLine("{");
223 Console.WriteLine("\t\"success\": true,");
224 Console.WriteLine("\t\"ip\": \"127.0.0.1\",");
225 Console.WriteLine("\t\"osVersion\": \"" + connectableDevice.Version.ToString() + "\"");
226 Console.WriteLine("}");
227 return 0;
228 }
229 }
230 }
231 return 0;
232 } catch (Exception ex) {
233 Console.WriteLine("{");
234 Console.WriteLine("\t\"success\": false,");
235 Console.WriteLine("\t\"message\": \"" + ex.Message.Trim().Replace("\"", "\\\"") + "\"");
236 Console.WriteLine("}");
237 return 1;
238 }
239 }
240
241 if (command != null) {
242 return showHelp(String.Format("Invalid command '{0}'", command));
243 }
244
245 return showHelp();
246 }
247
248 static int showHelp(string msg = "") {
249 Console.WriteLine("Appcelerator Windows Phone Tool v1.0\n");
250
251 if (msg.Length > 0) {
252 Console.WriteLine("ERROR: " + msg + "\n");
253 }
254
255 Console.WriteLine("Usage:");
256 Console.WriteLine(" wptool <command> [--wpsdk <ver>]");
257 Console.WriteLine("");
258 Console.WriteLine("Commands:");
259 Console.WriteLine(" enumerate List all devices and emulators");
260 Console.WriteLine(" connect <udid> Connects to the specified device");
261 Console.WriteLine(" launch <udid> <product-guid> Connects to the specified device and launches the app with the given product guid");
262 Console.WriteLine("");
263 Console.WriteLine("Options:");
264 Console.WriteLine(" --wpsdk <ver> The version of the Windows Phone SDK emulators to use (e.g. '10.0', or '8.1')");
265 Console.WriteLine(" Defaults to Windows 10 Mobile (e.g. 10.0)");
266 Console.WriteLine("");
267
268 return msg.Length > 0 ? 1 : 0;
269 }
270 }
271}