CB-12546: start of work on parsing emulator info from avdmanager output. work in progress.

This commit is contained in:
filmaj 2017-03-07 17:04:11 -08:00
parent 7d5afdebe1
commit 73f28541ab

View File

@ -29,9 +29,11 @@ var AndroidManifest = require('./AndroidManifest');
var events = require('cordova-common').events; var events = require('cordova-common').events;
var spawn = require('cordova-common').superspawn.spawn; var spawn = require('cordova-common').superspawn.spawn;
var CordovaError = require('cordova-common').CordovaError; var CordovaError = require('cordova-common').CordovaError;
var shelljs = require('shelljs');
var Q = require('q'); var Q = require('q');
var os = require('os'); var os = require('os');
var fs = require('fs');
var child_process = require('child_process'); var child_process = require('child_process');
// constants // constants
@ -42,6 +44,62 @@ var NUM_INSTALL_RETRIES = 3;
var CHECK_BOOTED_INTERVAL = 3 * ONE_SECOND; // in milliseconds var CHECK_BOOTED_INTERVAL = 3 * ONE_SECOND; // in milliseconds
var EXEC_KILL_SIGNAL = 'SIGKILL'; var EXEC_KILL_SIGNAL = 'SIGKILL';
function forgivingWhichSync(cmd) {
try {
return fs.realpathSync(shelljs.which(cmd));
} catch (e) {
return '';
}
}
function list_images_using_avdmanager() {
return spawn('avdmanager', ['list', 'avd'])
.then(function(output) {
var response = output.split('\n');
var emulator_list = [];
for (var i = 1; i < response.length; i++) {
// To return more detailed information use img_obj
var img_obj = {};
if (response[i].match(/Name:\s/)) {
img_obj['name'] = response[i].split('Name: ')[1].replace('\r', '');
if (response[i + 1].match(/Device:\s/)) {
i++;
img_obj['device'] = response[i].split('Device: ')[1].replace('\r', '');
}
if (response[i + 1].match(/Path:\s/)) {
i++;
img_obj['path'] = response[i].split('Path: ')[1].replace('\r', '');
}
if (response[i + 1].match(/Target:\s/)) {
var api = response[i + 1].split('Target: ')[1];
i++;
if (response[i + 1].match(/ABI:\s/)) {
img_obj['abi'] = response[i + 1].split('ABI: ')[1].replace('\r', '');
}
if (response[i + 1].match(/Based\son:\s/)) {
img_obj['target'] = response[i + 1].split('Based on:')[1];
if (img_obj['target'].match(/Tag\/ABI:\s/)) {
img_obj['target'] = img_obj['target'].split('Tag/ABI:')[0].replace('\r', '');
}
}
}
if (response[i + 1].match(/Skin:\s/)) {
i++;
img_obj['skin'] = response[i].split('Skin: ')[1].replace('\r', '');
}
emulator_list.push(img_obj);
}
/* To just return a list of names use this
if (response[i].match(/Name:\s/)) {
emulator_list.push(response[i].split('Name: ')[1].replace('\r', '');
}*/
}
return emulator_list;
});
}
/** /**
* Returns a Promise for a list of emulator images in the form of objects * Returns a Promise for a list of emulator images in the form of objects
* { * {
@ -53,6 +111,7 @@ var EXEC_KILL_SIGNAL = 'SIGKILL';
} }
*/ */
module.exports.list_images = function() { module.exports.list_images = function() {
if (forgivingWhichSync('android')) {
return spawn('android', ['list', 'avds']) return spawn('android', ['list', 'avds'])
.then(function(output) { .then(function(output) {
var response = output.split('\n'); var response = output.split('\n');
@ -93,7 +152,19 @@ module.exports.list_images = function() {
} }
return emulator_list; return emulator_list;
}).catch(function(stderr) {
// try to use `avdmanager` in case `android` has problems
// this likely means the target machine is using a newer version of
// the android sdk, and possibly `avdmanager` is available.
return list_images_using_avdmanager();
}); });
} else if (forgivingWhichSync('avdmanager')) {
return list_images_using_avdmanager();
} else {
return Q().then(function() {
throw new CordovaError('Could not find either `android` or `avdmanager` on your $PATH! Are you sure the Android SDK is installed and available?');
});
}
}; };
/** /**