1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const
|
15 | appc = require('node-appc'),
|
16 | async = require('async'),
|
17 | env = require('./env'),
|
18 | fs = require('fs'),
|
19 | magik = require('./utilities').magik,
|
20 | path = require('path'),
|
21 | proc = require('./process'),
|
22 | spawn = require('child_process').spawn,
|
23 | windowsphone = require('./windowsphone'),
|
24 | wptool = require('./wptool'),
|
25 | __ = appc.i18n(__dirname).__;
|
26 |
|
27 | var cache;
|
28 |
|
29 | exports.detect = detect;
|
30 | exports.isRunning = isRunning;
|
31 | exports.launch = launch;
|
32 | exports.install = install;
|
33 | exports.stop = stop;
|
34 | exports.status = status;
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function detect(options, callback) {
|
49 | return magik(options, callback, function (emitter, options, callback) {
|
50 | if (cache && !options.bypassCache) {
|
51 | emitter.emit('detected', cache);
|
52 | return callback(null, cache);
|
53 | }
|
54 |
|
55 | wptool.enumerate(options, function (err, results) {
|
56 | if (err) {
|
57 | emitter.emit('error', err);
|
58 | return callback(err);
|
59 | }
|
60 |
|
61 | var result = {
|
62 | emulators: {},
|
63 | issues: []
|
64 | };
|
65 |
|
66 | if (!err) {
|
67 | Object.keys(results).forEach(function (wpsdk) {
|
68 | result.emulators[wpsdk] = results[wpsdk].emulators;
|
69 | });
|
70 |
|
71 | cache = result;
|
72 | }
|
73 |
|
74 | emitter.emit('detected', result);
|
75 | callback(null, result);
|
76 | });
|
77 | });
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function isRunning(udid, options, callback) {
|
95 | return magik(options, callback, function (emitter, options, callback) {
|
96 | env.detect(options)
|
97 | .on('error', function (err) {
|
98 | emitter.emit('error', err);
|
99 | callback(err);
|
100 | })
|
101 | .on('detected', function (results) {
|
102 |
|
103 | detect(options)
|
104 | .on('error', function (err) {
|
105 | emitter.emit('error', err);
|
106 | callback(err);
|
107 | })
|
108 | .on('detected', function (results) {
|
109 | var emu;
|
110 |
|
111 | function testDev(d) {
|
112 | if (d.udid == udid) {
|
113 | emu = d;
|
114 | return true;
|
115 | }
|
116 | };
|
117 | Object.keys(results.emulators).some(function (wpsdk) {
|
118 | return results.emulators[wpsdk].some(testDev);
|
119 | });
|
120 |
|
121 | if (!emu) {
|
122 | var err = new Error(__('Invalid udid "%s"', udid));
|
123 | emitter.emit('error', err);
|
124 | return callback(err);
|
125 | }
|
126 |
|
127 | appc.subprocess.getRealName(path.resolve(__dirname, '..', 'bin', 'wp_emulator_status.ps1'), function (err, script) {
|
128 | if (err) {
|
129 | emitter.emit('error', err);
|
130 | return callback(err);
|
131 | }
|
132 |
|
133 | appc.subprocess.run(options.powershell || 'powershell', [
|
134 | '-ExecutionPolicy', 'Bypass', '-NoLogo', '-NonInteractive', '-NoProfile',
|
135 | '-File',
|
136 | script
|
137 | ], function (code, out, err) {
|
138 | if (code) {
|
139 | var err = new Error(__('Failed to detect running emulators'));
|
140 | emitter.emit('error', err);
|
141 | return callback(err);
|
142 | }
|
143 |
|
144 | var re = new RegExp('^' + emu.name + '\\..*(\\d+)(?:\\s+\\d+)$', 'i'),
|
145 | match = out.trim().split(/\r\n|\n/).map(function (line) {
|
146 | line = line.trim();
|
147 | return line && line.match(re);
|
148 | }).filter(function (m) { return !!m; }).shift(),
|
149 | running = match && ~~match[1] === 2 || false;
|
150 |
|
151 | emitter.emit('running', running);
|
152 | callback(null, running);
|
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 | function launch(udid, options, callback) {
|
183 | return magik(options, callback, function (emitter, options, callback) {
|
184 |
|
185 | detect(options, function (err, emuInfo) {
|
186 | if (err) {
|
187 | emitter.emit('error', err);
|
188 | return callback(err);
|
189 | }
|
190 |
|
191 | var emuHandle;
|
192 |
|
193 | if (udid) {
|
194 |
|
195 | Object.keys(emuInfo.emulators).filter(function (wpsdk) {
|
196 | return !options.wpsdk || wpsdk === options.wpsdk;
|
197 | }).some(function (wpsdk) {
|
198 | return emuInfo.emulators[wpsdk].some(function (emu) {
|
199 | if (emu.udid === udid) {
|
200 | emuHandle = appc.util.mix({}, emu);
|
201 | return true;
|
202 | }
|
203 | });
|
204 | });
|
205 |
|
206 | if (!emuHandle) {
|
207 | err = new Error(__('Unable to find an Windows Phone emulator with the UDID "%s"', udid));
|
208 | }
|
209 | } else {
|
210 | Object.keys(emuInfo.emulators).filter(function (wpsdk) {
|
211 | return !options.wpsdk || wpsdk === options.wpsdk;
|
212 | }).some(function (wpsdk) {
|
213 | if (emuInfo.emulators[wpsdk].length) {
|
214 | emuHandle = appc.util.mix({}, emuInfo.emulators[wpsdk][0]);
|
215 | return true;
|
216 | }
|
217 | });
|
218 |
|
219 | if (!emuHandle) {
|
220 |
|
221 | if (options.wpsdk) {
|
222 | err = new Error(__('Unable to find an Windows Phone %s emulator.', options.wpsdk));
|
223 | } else {
|
224 | err = new Error(__('Unable to find an Windows Phone emulator.'));
|
225 | }
|
226 | }
|
227 | }
|
228 |
|
229 | if (err) {
|
230 | emitter.emit('error', err);
|
231 | return callback(err);
|
232 | }
|
233 |
|
234 | if (options.appPath && !fs.existsSync(options.appPath)) {
|
235 | err = new Error(__('App path does not exist: ' + options.appPath));
|
236 | emitter.emit('error', err);
|
237 | return callback(err);
|
238 | }
|
239 |
|
240 | emuHandle.startTime = Date.now();
|
241 | emuHandle.running = false;
|
242 |
|
243 | function launchEmulator() {
|
244 | var timeout = options.timeout !== void 0 && Math.max(~~options.timeout, 1);
|
245 | if (options.appPath) {
|
246 | var mixedOptions = appc.util.mix({timeout: timeout}, options);
|
247 | wptool.install(emuHandle, options.appPath, mixedOptions)
|
248 | .on('error', function (err) {
|
249 | emitter.emit('error', err);
|
250 | callback(err);
|
251 | }).on('installed', function () {
|
252 | emuHandle.running = true;
|
253 | emitter.emit('installed', emuHandle);
|
254 |
|
255 | if (mixedOptions.skipLaunch) {
|
256 | callback(null, emuHandle);
|
257 | }
|
258 | }).on('launched', function () {
|
259 | emitter.emit('launched', emuHandle);
|
260 |
|
261 | if (!mixedOptions.skipLaunch) {
|
262 | callback(null, emuHandle);
|
263 | }
|
264 | }).on('timeout', function (err) {
|
265 | err || (err = new Error(__('Timed out after %d milliseconds waiting to launch the emulator.', timeout)));
|
266 | emitter.emit('timeout', err);
|
267 | callback(err);
|
268 | });
|
269 | } else {
|
270 |
|
271 | wptool.connect(emuHandle.udid, {
|
272 | timeout: timeout
|
273 | }).on('error', function (err) {
|
274 | emitter.emit('error', err);
|
275 | callback(err);
|
276 | }).on('connected', function (emu) {
|
277 | emuHandle.ip = emu.ip;
|
278 | emuHandle.running = emu.running || true;
|
279 | emitter.emit('launched', emuHandle);
|
280 | callback(null, emuHandle);
|
281 | }).on('timeout', function (err) {
|
282 | err || (err = new Error(__('Timed out after %d milliseconds waiting to launch the emulator.', timeout)));
|
283 | emitter.emit('timeout', err);
|
284 | callback(err);
|
285 | });
|
286 | }
|
287 | }
|
288 |
|
289 | if (options.killIfRunning) {
|
290 | stop(emuHandle, options, launchEmulator);
|
291 | } else {
|
292 | launchEmulator();
|
293 | }
|
294 | });
|
295 | });
|
296 | }
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | function install(udid, appPath, options, callback) {
|
316 | return magik(options, callback, function (emitter, options, callback) {
|
317 | if (!appPath) {
|
318 | var err = new Error(__('Missing app path argument'));
|
319 | emitter.emit('error', err);
|
320 | return callback(err);
|
321 | }
|
322 |
|
323 | var launchEmitter = launch(udid, appc.util.mix({
|
324 | appPath: appPath
|
325 | }, options), callback),
|
326 | oldEmit = launchEmitter.emit;
|
327 |
|
328 | launchEmitter.emit = function () {
|
329 | oldEmit.apply(launchEmitter, arguments);
|
330 | emitter.emit.apply(emitter, arguments);
|
331 | };
|
332 | });
|
333 | }
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 | function stop(emuHandle, options, callback) {
|
350 | return magik(options, callback, function (emitter, options, callback) {
|
351 | if (!emuHandle || typeof emuHandle !== 'object') {
|
352 | var err = new Error(__('Invalid emulator handle argument'));
|
353 | emitter.emit('error', err);
|
354 | return callback(err);
|
355 | }
|
356 |
|
357 |
|
358 | setTimeout(function () {
|
359 | proc.list(options, function (err, processes) {
|
360 | if (err) {
|
361 | emitter.emit('error', err);
|
362 | return callback(err);
|
363 | }
|
364 |
|
365 | appc.subprocess.getRealName(path.resolve(__dirname, '..', 'bin', 'wp_stop_emulator.ps1'), function (err, psScript) {
|
366 | if (err) {
|
367 | emitter.emit('error', err);
|
368 | return callback(err);
|
369 | }
|
370 |
|
371 | var xdeRegExp = /^xde\.exe$/i,
|
372 | name = emuHandle.name.toLowerCase();
|
373 |
|
374 | async.eachSeries(processes, function (p, next) {
|
375 | if (!xdeRegExp.test(p.name) || p.title.toLowerCase() !== name) {
|
376 | return next();
|
377 | }
|
378 |
|
379 | process.kill(p.pid, 'SIGKILL');
|
380 |
|
381 | setTimeout(function () {
|
382 |
|
383 | appc.subprocess.run(options.powershell || 'powershell', [
|
384 | '-ExecutionPolicy', 'Bypass', '-NoLogo', '-NonInteractive', '-NoProfile',
|
385 | '-File',
|
386 | psScript,
|
387 | '"' + emuHandle.name + '"'
|
388 | ], function (code, out, err) {
|
389 | try {
|
390 | if (!code) {
|
391 | var r = JSON.parse(out);
|
392 | if (r.success) {
|
393 | return next();
|
394 | }
|
395 | }
|
396 | } catch (e) {
|
397 | return next(e);
|
398 | }
|
399 | next(new Error('Failed to stop emulator.\r\nstdout: ' + out + '\r\nstderr: ' + err));
|
400 | });
|
401 | }, 1000);
|
402 | }, function (err) {
|
403 | if (err) {
|
404 | emitter.emit('error', err);
|
405 | return callback(err);
|
406 | }
|
407 |
|
408 | emuHandle.running = false;
|
409 | emitter.emit('stopped');
|
410 | callback();
|
411 | });
|
412 | });
|
413 | });
|
414 | }, Date.now() - emuHandle.startTime < 250 ? 250 : 0);
|
415 | });
|
416 | }
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 | function status(emuHandle, options, callback) {
|
430 | return magik(options, callback, function (emitter, options, callback) {
|
431 | if (!emuHandle || typeof emuHandle !== 'object') {
|
432 | var err = new Error(__('Invalid emulator handle argument'));
|
433 | emitter.emit('error', err);
|
434 | return callback(err);
|
435 | }
|
436 |
|
437 | appc.subprocess.getRealName(path.resolve(__dirname, '..', 'bin', 'wp_emulator_enabledstate.ps1'), function (err, psScript) {
|
438 | if (err) {
|
439 | emitter.emit('error', err);
|
440 | return callback(err);
|
441 | }
|
442 |
|
443 |
|
444 | appc.subprocess.run(options.powershell || 'powershell', [
|
445 | '-ExecutionPolicy', 'Bypass', '-NoLogo', '-NonInteractive', '-NoProfile',
|
446 | '-File',
|
447 | psScript,
|
448 | emuHandle.name
|
449 | ], function (code, out, err) {
|
450 | try {
|
451 | if (!code) {
|
452 | var result = JSON.parse(out);
|
453 | if (result.success) {
|
454 | return callback(null, result.state);
|
455 | } else {
|
456 | return callback(result.message);
|
457 | }
|
458 | }
|
459 | } catch (e) {}
|
460 | callback(new Error('Failed to retrieve emulator status'));
|
461 | });
|
462 | });
|
463 | });
|
464 | }
|