-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulators.js
executable file
·364 lines (333 loc) · 10 KB
/
emulators.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
#!/usr/bin/env node
/**
* Emulators.js is a utility to help manage an array of
* Android emulators and installed applications.
*/
var exec = require('child_process').exec;
var fs = require('fs'),
spawn = require('child_process').spawn;
var program = require('commander');
var _ = require('underscore');
var DEFAULT_APP = process.env.EMULATORS_APP;
var DEFAULT_PROP_FILE = process.env.EMULATORS_PROPERTIES || "ant.properties";
var REALLY_EXECUTE = true;
var RESTART_ABD_ALWAYS = false;
//first, checks if it isn't implemented yet
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] !== 'undefined' ? args[number] : match;
});
};
}
/**
* Wraps system execute calls, primarily for mocking.
*/
function System() {
/**
* @cmd the command to run
* @next function to run synchronously after command
*/
this.exec = function(cmd, next) {
console.log("Executing: " + cmd);
if (REALLY_EXECUTE) {
exec(cmd, function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
} else {
console.log("Done executing: " + cmd);
}
// chain to the next bit logic synchronously
if (next !== undefined) {
next();
}
});
}
return 0;
};
/**
* Execute this command and detach from it.
*/
this.execDetached = function(cmd) {
console.log("Executing: " + cmd);
var cmdArray = cmd.trim().split(' ');
console.log(cmdArray);
var child = spawn(cmdArray[0], cmdArray.slice(1), {
//detached: true,
// TODO: direct these to a file in /tmp
stdio: [ 'ignore', process.stdout, process.stderr]
});
child.unref();
console.log("done executing");
};
}
/** Global "system" object */
var sys = new System();
/**
* We restart ABD because it is very finicky.
*
* @next is callback function to run when complete.
*/
function restartAdb(next) {
if (RESTART_ABD_ALWAYS) {
console.log("Restarting adb.");
sys.exec("adb kill-server", function() {
sys.exec("adb start-server", function() {
console.log("Restart of adb complete.");
if (next !== undefined) {
next();
}
});
});
} else {
next();
}
}
/**
* A configured emulator, which may not be started.
*/
function Emulator(id, options) {
var self = this;
self.id = id;
//self.options = options;
self.serial = options.serial;
self.name = options.name;
self.port = self.serial.substring(9);
/** Emulator only commands. */
self.emuStart = function(visual) {
// TODO: try up to 3 times to start the emulator
// TODO: create a unique outfile
//var outfile = "/tmp/" + self.serial;
//var cmdStart = "nohup emulator -avd {0} -port {1}".format(self.name, self.port);
// TODO: redirect output to logfile
var cmdStart = "emulator -avd {0} -port {1}".format(self.name, self.port),
cmdEnd = "", // " &"; // "> {0} 2>&1 &".format(outfile),
headlessExtras = "-wipe-data -no-boot-anim -no-window -noaudio",
cmd = visual ?
cmdStart + " " + cmdEnd :
cmdStart + " " + headlessExtras + cmdEnd;
sys.execDetached(cmd);
};
self.emuStop = function() {
console.log("trying to stop: " + self.serial);
self._doIfRunning(function() {
sys.exec("adb -s {0} emu kill".format(self.serial));
});
};
/** Emulator-app commands. */
self.appStop = function(app) {
self._doIfRunning(function() {
sys.exec("adb -s {0} shell am force-stop {1}".format(self.serial, app));
});
};
self.appClear = function(app) {
self._doIfRunning(function() {
sys.exec("adb -s {0} shell pm clear {1}".format(self.serial, app));
});
};
self.appInstall = function(apk) {
self._doIfRunning(function() {
sys.exec("adb -s {0} install {1}".format(self.serial, apk));
});
};
self.appUninstall = function(app) {
self._doIfRunning(function() {
sys.exec("adb -s {0} uninstall {1}".format(self.serial, app));
});
};
/**
* Execute the callback onResult iff self emulator is currently running.
* @attempts number of times to try with 1 second delays
*/
self._doIfRunning = function(onResult, attempts) {
var delayMs = 1000,
attempts = attempts || 1;
// TODO: keep running this until it's positive or we've tried 10x
// TODO: want try number #1 to be with no delay before it
function scanForDevice() {
console.log("DBG: %d: trying 'adb devices | grep %s", attempts, self.serial);
exec("adb devices | grep " + self.serial,
function (error, stdout, stderr) {
if (stdout.trim().match("^" + self.serial)) {
onResult();
clearInterval(timer);
} else {
console.log("Emulator is not running: {0} \n({1})".format(self.serial, stdout));
if (--attempts == 0) {
clearInterval(timer);
}
}
});
};
// try once immediately
scanForDevice();
var timer = setInterval(scanForDevice, delayMs);
};
}
/**
* Represents collection of all defined emulators.
* Defines available functions for emulators.
* Opts:
* antInit: if defined, will init from given ant properties file.
*/
function Emulators(opts) {
var self = this;
// maps id -> emulator
self.emus = {};
/**
* Define a new emulator.
*/
self.add = function(id, opts) {
self.emus[id] = new Emulator(id, opts);
};
/** Emulator only commands. */
self.cmd_start = function(ids, opts) {
self._execute(ids, function(emu) { emu.emuStart(opts.visual || false); });
};
self.cmd_stop = function(ids, opts) {
self._execute(ids, function(emu) { emu.emuStop(); });
};
/** Emulator-app commands. */
self.cmd_forceStop = function(ids, opts) {
self._execute(ids, function(emu) { emu.appStop(opts.app); });
};
self.cmd_clear = function(ids, opts) {
self._execute(ids, function(emu) { emu.appClear(opts.app); });
};
self.cmd_install = function(ids, opts) {
self._execute(ids, function(emu) { emu.appInstall(opts.apk); });
};
self.cmd_uninstall = function(ids, opts) {
self._execute(ids, function(emu) { emu.appUninstall(opts.app); });
};
self.executeFromOptions = function(opts) {
self[opts.cmd].call(self, opts.ids, opts);
};
/**
* Run an operation over specific emulator ids or else
* ALL by default.
*/
self._execute = function(ids, operation) {
// if ids are provide start only those
if (ids !== undefined && ids.length > 0) {
_.each(ids, function(id) {
operation(self.emus[id]);
});
}
// otherwise start all of them
else {
_.each(self.emus, function(emu, id) {
operation(emu);
});
}
};
/**
* Load a key-value properties file into an object.
*/
self.loadProperties = function(filename) {
var data = fs.readFileSync(filename).toString(),
props = {};
// create an object of the property value pairs
data.split('\n').forEach(function (line) {
if (line.trim().length > 0) {
var vals = line.split('=');
try {
props[vals[0].trim()] = vals[1].trim();
} catch (err) {
}
}
});
return props;
};
/**
* Define emulators based on ant properties file definitions.
*/
self.initFromAntProps = function(filename) {
var props = this.loadProperties(filename),
size = _.pairs(props).length;
// assume that # emulators <= # of properties
// probe to find out which ones exist
for (var i=0; i < size; i++) {
var name = props["emulator.{0}.name".format(i)];
var serial = props["emulator.{0}.console.port".format(i)];
if (name && serial) {
self.add(i, {name:name, serial:serial});
}
}
};
if (opts && opts.antInit) {
self.initFromAntProps(opts.antInit);
}
}
function parseOptions() {
function defaultVal(val, d) {
return val !== undefined ? val : d;
}
// HOWTO: require certain commands to have "app" or other args
// dynamically identify all commands by fields prefixed with cmd_,
// and return this list with the prefix removed.
// HOWTO: do this simpler?
var validCommands =
_.map(
_.filter(_.keys(new Emulators()),
function(k) {
return (/^cmd_/).test(k);
}),
function(k) {
return k.replace(/^cmd_/, "");
});
program
.version('0.0.1')
.usage('<command> [emulator numbers; default=all]')
.option('-a, --app <app name>', 'E.g. com.mycorp.myapp')
.option('-v, --visual', 'Run emulators in visual rather than headless mode')
.option('--apk <apk file>', 'E.g. myapp.apk');
program.on('--help', function(){
console.log(' Valid commands: ' + validCommands);
console.log('');
console.log(' Examples:');
console.log('');
console.log(' $ emulators.js start 2 3 4 --visual');
console.log(' $ emulators.js forceStop --app com.myapp');
console.log('');
});
program.parse(process.argv);
// validation on the command
if (program.args.length < 1) {
console.log("\nERROR: Please must provide a command argument.");
program.help();
} else {
program.cmd = program.args[0];
}
if (!_.contains(validCommands, program.cmd)) {
console.log("\nERROR: Unknown command: " + program.cmd);
program.help();
}
var opts = {
cmd : "cmd_" + program.cmd,
ids : program.args.slice(1),
app : defaultVal(program.app, DEFAULT_APP),
apk : program.apk,
visual : defaultVal(program.visual, false)
};
console.log(opts);
console.log("---");
return opts;
}
function main() {
var opts = parseOptions();
var emus = new Emulators({antInit:DEFAULT_PROP_FILE});
restartAdb(function() {
emus.executeFromOptions(opts);
});
}
if (require.main === module) {
main();
} else {
exports.Emulator = Emulator;
exports.Emulators = Emulators;
exports.sys = sys;
}