-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
genymotion.js
582 lines (524 loc) Β· 18.1 KB
/
genymotion.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/**
* @overview
* Library for controlling a Genymotion emulator.
*
* @module lib/emulators/genymotion
*
* @copyright
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
*
* @license
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
var appc = require('node-appc'),
__ = appc.i18n(__dirname).__,
async = require('async'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
wrench = require('wrench'),
ADB = require('../adb'),
EmulatorManager = require('../emulator'),
defaultTitaniumHomeDir = appc.fs.resolvePath('~', '.titanium'),
exe = process.platform == 'win32' ? '.exe' : '',
cache;
/**
* Detects that Genymotion is installed and which Genymotion emulators exist.
* @param {Object} config - The CLI config object
* @param {Object} opts - Detect options
* @param {Boolean} [opts.bypassCache=false] - Bypasses the Genymotion environment detection cache and re-queries the system
* @param {Function} finished - A function to call when the detection has completed
*/
exports.detect = function detect(config, opts, finished) {
opts = opts || {};
if (cache && !opts.bypassCache) return finished(null, cache);
async.parallel({
genymotion: function (next) {
var queue = async.queue(function (task, callback) {
task(function (err, result) {
if (err) {
callback(); // go to next item in the queue
} else {
next(null, result);
}
});
}, 1);
queue.drain = function () {
// we have completely exhausted all search paths
next(null, null);
};
queue.push([
// first let's check the config's value
function (cb) {
findGenymotion(config.get('genymotion.path'), config, cb);
},
// try finding the 'genymotion' executable
function (cb) {
appc.subprocess.findExecutable([config.get('genymotion.executables.genymotion'), 'genymotion' + exe], function (err, result) {
if (err) {
cb(err);
} else {
findGenymotion(path.resolve(result, '..', '..'), config, cb);
}
});
}
]);
var genyRegexp = /genymo(tion|bile)/i,
dirs = process.platform == 'win32'
? ['%SystemDrive%', '%ProgramFiles%', '%ProgramFiles(x86)%', '%CommonProgramFiles%', '~']
: ['/opt', '/opt/local', '/usr', '/usr/local', '~'];
if (process.platform == 'darwin' && fs.existsSync('/Applications')) {
dirs.unshift('/Applications');
}
dirs.forEach(function (dir) {
dir = appc.fs.resolvePath(dir);
fs.existsSync(dir) && fs.readdirSync(dir).forEach(function (name) {
var subdir = path.join(dir, name);
if (genyRegexp.test(name) && name[0] != '.' && fs.existsSync(subdir) && fs.statSync(subdir).isDirectory()) {
queue.push(function (cb) {
findGenymotion(subdir, config, cb);
});
}
});
});
},
virtualbox: function (next) {
// try to find the VBoxManage file in the config file or system paths
appc.subprocess.findExecutable([config.get('genymotion.executables.vboxmanage'), 'VBoxManage' + exe], function (err, result) {
function getVersion(exe) {
appc.subprocess.run(exe, '--version', function (code, out, err) {
next(null, {
vboxmanage: exe,
version: code ? null : out.trim()
});
});
}
if (err || !result) {
// didn't find it, try a deep scanning various paths
var executableName = 'VBoxManage' + exe,
queue = async.queue(function (task, callback) {
task(function (err, result) {
if (err) {
callback(); // go to next item in the queue
} else if (result) {
getVersion(result);
} else {
next(null, null);
}
});
}, 1);
queue.drain = function () {
// we have completely exhausted all search paths
next(null, null);
};
queue.push(
(process.platform == 'win32'
// default location is C:\Program Files\Oracle\VirtualBox
? ['%ProgramFiles%', '%ProgramFiles(x86)%']
: ['/opt', '/usr', '~']
).map(function (dir) {
dir = appc.fs.resolvePath(dir);
return function (next) {
next(null, fs.existsSync(dir) && (function scan(parent, depth) {
var files = fs.readdirSync(parent),
i = 0,
l = files.length,
name, file, stat, result;
for (; i < l; i++) {
name = files[i];
file = path.join(parent, name);
if (fs.existsSync(file)) {
stat = fs.statSync(file);
if (stat.isFile() && name == executableName) {
return file;
} else if (stat.isDirectory() && depth) {
if (result = scan(file, depth - 1)) {
return result;
}
}
}
}
}(dir, 3))); // we only want to go 3 levels deep
};
})
);
} else {
getVersion(result);
}
});
}
}, function (err, data) {
var results = data.genymotion || {};
results.executables || (results.executables = {
genymotion: null,
player: null,
vboxmanage: null
});
results.home = null;
results.avds = [];
results.issues = [];
function finalize() {
finished(null, cache = results);
}
if (!data.genymotion) {
return finalize();
}
if (!results.executables.player) {
results.issues.push({
id: 'GENYMOTION_MISSING_PLAYER',
type: 'error',
message: __('Unable to locate the Genymotion "player" executable.')
});
}
// attempt to find the Genymotion home directory
var genymotionHomeDirs = [ config.get('genymotion.home') ];
if (process.platform == 'win32') {
genymotionHomeDirs.push('~/AppData/Local/Genymobile/Genymotion');
} else {
genymotionHomeDirs.push('~/.Genymobile/Genymotion', '~/.Genymotion');
}
for (var i = 0; i < genymotionHomeDirs.length; i++) {
if (genymotionHomeDirs[i]) {
var dir = appc.fs.resolvePath(genymotionHomeDirs[i]);
if (fs.existsSync(dir) && fs.statSync(dir).isDirectory()) {
results.home = dir;
break;
}
}
}
if (!results.home) {
results.issues.push({
id: 'GENYMOTION_HOME_NOT_FOUND',
type: 'error',
message: __('Unable to locate the Genymotion home directory.') + '\n' +
__('If you haven\'t launched Genymotion yet, run it quick so that the home directory is created.')
});
}
if (data.virtualbox) {
results.virtualbox = data.virtualbox.version;
var vboxmanage = results.executables.vboxmanage = data.virtualbox.vboxmanage;
if (!vboxmanage) {
results.issues.push({
id: 'VIRTUALBOX_MANAGE_NOT_FOUND',
type: 'error',
message: __('Unable to locate VirtualBox\'s "VBoxManage" executable.')
});
finalize();
} else {
// since we found VBoxManage, now we can find all AVDs
getVMInfo(config, vboxmanage, function (err, emus) {
results.avds = emus;
finalize();
});
}
} else {
results.issues.push({
id: 'VIRTUALBOX_NOT_FOUND',
type: 'error',
message: __('Unable to find VirtualBox which is required for Genymotion to work.')
});
finalize();
}
});
};
function findGenymotion(dir, config, callback) {
if (!dir) return callback(true);
// check if the supplied directory exists and is actually a directory
dir = appc.fs.resolvePath(dir);
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return callback(true);
// find the genymotion executable
var executableName = 'genymotion' + exe,
executable = (function scan(parent) {
var files = fs.readdirSync(parent),
i = 0,
l = files.length,
name, file, stat, result;
for (; i < l; i++) {
name = files[i];
file = path.join(parent, name);
if (fs.existsSync(file)) {
stat = fs.statSync(file);
if (stat.isFile() && name == executableName) {
return file;
} else if (stat.isDirectory()) {
if (result = scan(file)) {
return result;
}
}
}
}
}(dir));
// check if we found it
if (!executable) return callback(true);
// strip off the executable name to get the genymotion directory
dir = path.dirname(executable);
var player = config.get('genymotion.executables.player');
if (!player || !fs.existsSync(player)) {
player = path.join(dir, 'player' + exe);
}
if (player && !fs.existsSync(player)) {
player = null;
}
callback(null, {
path: dir,
executables: {
genymotion: executable,
player: player
}
});
}
function getVMInfo(config, vboxmanage, callback) {
appc.subprocess.run(vboxmanage, ['list', 'vms'], function (code, out, err) {
if (code) return callback(null, []);
async.parallel(out.split('\n').map(function (line) {
return function (next) {
line = line.trim();
if (!line) return next();
var m = line.match(/^"(.+)" \{(.+)\}$/);
if (!m) return next();
var emu = {
name: m[1],
guid: m[2],
type: 'genymotion',
abi: 'x86',
googleApis: null // null means maybe since we don't know for sure unless the emulator is running
};
appc.subprocess.run(vboxmanage, ['guestproperty', 'enumerate', emu.guid], function (code, out, err) {
if (!code) {
out.split('\n').forEach(function (line) {
var m = line.trim().match(/Name: (\S+), value: (\S*), timestamp:/);
if (m) {
switch (m[1]) {
case 'android_version':
emu['sdk-version'] = emu.target = m[2];
break;
case 'genymotion_version':
emu.genymotion = m[2];
break;
case 'hardware_opengl':
emu.hardwareOpenGL = !!parseInt(m[2]);
break;
case 'vbox_dpi':
emu.dpi = ~~m[2];
break;
case 'vbox_graph_mode':
emu.display = m[2];
break;
case 'androvm_ip_management':
emu.ipaddress = m[2];
break;
}
}
});
}
// if the virtual machine does not define the genymotion version, then
// it's not a Genymotion virtual machine
if (!emu.genymotion) {
emu = null;
}
// this is a hack, but by default new Genymotion emulators that have Google APIs will
// say "Google Apps" in the name, so if we find that, assume it has Google APIs
if (emu && /google apps/i.test(emu.name)) {
emu.googleApis = true;
}
// if we have an ip address, then the Genymotion emulator is running and
// we can see if the Google APIs are installed
if (emu && emu.ipaddress) {
var adb = new ADB(config);
adb.shell(emu.ipaddress + ':5555', '[ -f /system/etc/g.prop ] && cat /system/etc/g.prop || echo ""', function (err, out) {
if (err) {
// if we errored, then that means the 'androvm_ip_management'
// was stale and we should just assume it's not running
emu.ipaddress = null;
} else {
emu.googleApis = out ? out.toString().indexOf('gapps') != -1 : null;
}
next(null, emu);
});
} else {
next(null, emu);
}
});
}
}), function (err, emus) {
callback(null, emus.filter(function (a) { return a; }));
});
});
}
/**
* Detects if a specific Genymotion VM is running and if so, returns
* the emulator definition object and the device definition object.
* @param {Object} emu - The Android emulator avd definition
* @param {Array<Object>} devices - An array of device definition objects
* @param {Function} callback - A function to call when the detection has completed
*/
exports.isRunning = function isRunning(config, emu, devices, callback) {
if (!devices.length || !emu.type == 'genymotion') return callback(null, false);
function next() {
var id = emu.ipaddress + ':5555',
i = 0,
len = devices.length;
for (; i < len; i++) {
if (devices[i].id == id) {
return callback(null, devices[i]);
}
}
callback(null, false);
}
if (emu.ipaddress) {
next();
} else {
// see if we can get the ip address
this.detect(config, null, function (err, results) {
if (results.executables.vboxmanage) {
getVMInfo(config, results.executables.vboxmanage, function (err, emus) {
emu = emus.filter(function (e) {
return e && e.name == emu.name;
}).shift();
if (emu && emu.ipaddress) {
return next();
}
callback(null, false);
});
} else {
// genymotion not installed
callback(null, false);
}
});
}
};
/**
* Detects if a specific device name is an Genymotion emulator.
* @param {Object} config - The CLI config object
* @param {Object} device - The device name
* @param {Function} callback - A function to call when the detection has completed
*/
exports.isEmulator = function isEmulator(config, device, callback) {
this.detect(config, {}, function (err, results) {
if (results.executables.vboxmanage) {
getVMInfo(config, results.executables.vboxmanage, function (err, emus) {
emu = emus.filter(function (e) {
return e && e.ipaddress && device.indexOf(e.ipaddress + ':') == 0;
}).shift();
if (emu) {
return callback(null, emu);
}
callback(null, false);
});
} else {
// genymotion not installed
callback(null, false);
}
});
};
/**
* Launches the specified Genymotion emulator.
* @param {Object} config - The CLI config object
* @param {Object|String} emu - The Android emulator avd definition or the name of the emulator
* @param {Object} opts - Emulator options object
* @param {String} [opts.titaniumHomeDir="~/.titanium"] - The Titanium home directory
* @param {Boolean} [opts.bypassCache=false] - Bypasses the Genymotion environment detection cache and re-queries the system
* @param {String} [opts.cwd] - The current working directory to pass into spawn()
* @param {Array|String} [opts.stdio="ignore"] - The stdio configuration to pass into spawn()
* @param {Object} [opts.env] - The environment variables to pass into spawn()
* @param {Boolean} [opts.detached=true] - The detached flag to pass into spawn()
* @param {Number} [opts.uid] - The user id to pass into spawn()
* @param {Number} [opts.gid] - The group id to pass into spawn()
* @param {Function} callback - A function to call when the emulator is started
*/
exports.start = function start(config, emu, opts, callback) {
opts = opts || {};
exports.detect(config, { bypassCache: opts.bypassCache }, function (err, results) {
if (err) return callback(err);
if (!results.path) return callback(new Error(__('Unable to find Genymotion installation')));
if (!results.executables.player) return callback(new Error(__('Unable to find Genymotion "player" executable')));
// if they passed in the emulator name, get the emulator avd definition
if (emu && typeof emu == 'string') {
var name = emu;
emu = results.avds.filter(function (e) { return e && e.name == name; }).shift();
if (!emu) return callback(new Error(__('Invalid emulator "%s"', name)), null);
}
var emuopts = {
detached: opts.hasOwnProperty('detached') ? !!opts.detached : true,
stdio: opts.stdio || 'ignore'
};
opts.cwd && (emuopts.cwd = opts.cwd);
opts.env && (emuopts.env = opts.env);
opts.uid && (emuopts.uid = opts.uid);
opts.gid && (emuopts.gid = opts.gid);
opts.logger && opts.logger.info(__('Running: %s', (results.executables.player + ' --vm-name "' + emu.name + '"').cyan));
var child = this.child = spawn(results.executables.player, ['--vm-name', emu.name], emuopts),
device = new EmulatorManager.Emulator;
device.emulator = {
pid: child.pid
};
appc.util.mix(device.emulator, emu);
child.stdout && child.stdout.on('data', function (data) {
device.emit('stdout', data);
}.bind(this));
child.stderr && child.stderr.on('data', function (data) {
device.emit('stderr', data);
}.bind(this));
child.on('error', function (err) {
device.emit('error', err);
}.bind(this));
child.on('close', function (code, signal) {
device.emit('exit', code, signal);
}.bind(this));
child.unref();
var titaniumHomeDir = appc.fs.resolvePath(opts.titaniumHomeDir || defaultTitaniumHomeDir),
pidDir = path.join(titaniumHomeDir, 'genymotion'),
pidFile = path.join(pidDir, emu.name + '.pid');
fs.existsSync(pidFile) && fs.unlinkSync(pidFile);
fs.existsSync(pidDir) || wrench.mkdirSyncRecursive(pidDir);
fs.writeFileSync(pidFile, child.pid);
callback(null, device);
});
};
/**
* Kills the specified Genymotion emulator.
* @param {Object} config - The CLI config object
* @param {String} name - The name of the emulator
* @param {Object} device - Android device definition object
* @param {Object} opts - Emulator options object
* @param {String} [opts.titaniumHomeDir="~/.titanium"] - The Titanium home directory
* @param {Boolean} [opts.bypassCache=false] - Bypasses the Genymotion environment detection cache and re-queries the system
* @param {String} [opts.cwd] - The current working directory to pass into spawn()
* @param {Array|String} [opts.stdio="ignore"] - The stdio configuration to pass into spawn()
* @param {Object} [opts.env] - The environment variables to pass into spawn()
* @param {Boolean} [opts.detached=true] - The detached flag to pass into spawn()
* @param {Number} [opts.uid] - The user id to pass into spawn()
* @param {Number} [opts.gid] - The group id to pass into spawn()
* @param {Function} callback - A function to call when the emulator is stopped
*/
exports.stop = function stop(config, name, device, opts, callback) {
if (opts && typeof opts == 'function') {
callback = opts;
opts = {};
} else {
opts = opts || {};
}
exports.detect(config, { bypassCache: opts.bypassCache }, function (err, results) {
var emu = results.avds.filter(function (e) { return e && e.name == name; }).shift();
if (!emu) return callback(new Error(__('Invalid emulator "%s"', name)), null);
exports.isRunning(config, emu, [ device ], function (err, running) {
if (err || !running) return callback(err);
var titaniumHomeDir = appc.fs.resolvePath(opts.titaniumHomeDir || defaultTitaniumHomeDir),
pidFile = path.join(titaniumHomeDir, 'genymotion', emu.name + '.pid');
if (fs.existsSync(pidFile)) {
var pid = parseInt(fs.readFileSync(pidFile).toString().trim());
pid && process.kill(pid);
fs.unlinkSync(pidFile);
}
// unclean shutdowns cause adb to notice the emulator went away
var adb = new ADB(config);
adb.stopServer(function (err) {
if (err) return callback();
adb.startServer(function () {
callback();
});
});
});
});
};