Fix the overwriting of Fil's fix, blargh

This commit is contained in:
Joe Bowser 2017-04-11 14:41:27 -07:00
parent db87e0ae6a
commit 69260fb96a

View File

@ -1,5 +1,3 @@
/* /*
Licensed to the Apache Software Foundation (ASF) under one Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file or more contributor license agreements. See the NOTICE file
@ -68,65 +66,38 @@ module.exports.version_string_to_api_level = {
'7.1.1': 25 '7.1.1': 25
}; };
module.exports.list_targets_with_android = function() { function parse_targets(output) {
return superspawn.spawn('android', ['list', 'targets']) var target_out = output.split('\n');
.then(function(stdout) { var targets = [];
var target_out = stdout.split('\n'); for (var i = target_out.length - 1; i >= 0; i--) {
var targets = []; if(target_out[i].match(/id:/)) { // if "id:" is in the line...
for (var i = target_out.length - 1; i >= 0; i--) { targets.push(target_out[i].match(/"(.+)"/)[1]); //.. match whatever is in quotes.
if(target_out[i].match(/id:/)) {
targets.push(target_out[i].match(/"(.+)"/)[1]);
}
} }
return targets; }
}); return targets;
}
module.exports.list_targets_with_android = function() {
return superspawn.spawn('android', ['list', 'target'])
.then(parse_targets);
}; };
module.exports.list_targets_with_sdkmanager = function() { module.exports.list_targets_with_avdmanager = function() {
return superspawn.spawn('sdkmanager', ['--list']) return superspawn.spawn('avdmanager', ['list', 'target'])
.then(function(stdout) { .then(parse_targets);
var parsing_installed_packages = false;
var lines = stdout.split('\n');
var targets = [];
for (var i = 0, l = lines.length; i < l; i++) {
var line = lines[i];
if (line.match(/Installed packages/)) {
parsing_installed_packages = true;
} else if (line.match(/Available Packages/) || line.match(/Available Updates/)) {
// we are done working through installed packages, exit
break;
}
if (parsing_installed_packages) {
// Match stock android platform
if (line.match(/platforms;android-\d+/)) {
targets.push(line.match(/(android-\d+)/)[1]);
}
// Match Google APIs
if (line.match(/addon-google_apis-google-\d+/)) {
var description = lines[i + 1];
// munge description to match output from old android sdk tooling
var api_level = description.match(/Android (\d+)/); //[1];
if (api_level) {
targets.push('Google Inc.:Google APIs:' + api_level[1]);
}
}
// TODO: match anything else?
}
}
return targets;
});
}; };
module.exports.list_targets = function() { module.exports.list_targets = function() {
return module.exports.list_targets_with_android() return module.exports.list_targets_with_avdmanager()
.catch(function(err) { .catch(function(err) {
// there's a chance `android` no longer works. // If there's an error, like avdmanager could not be found, we can try
// lets see if `sdkmanager` is available and we can figure it out // as a last resort, to run `android`, in case this is a super old
var avail_regex = /"?android"? command is no longer available/; // SDK installation.
if (err.code && ((err.stdout && err.stdout.match(avail_regex)) || (err.stderr && err.stderr.match(avail_regex)))) { if (err && (err.code == 'ENOENT' || (err.stderr && err.stderr.match(/not recognized/)))) {
return module.exports.list_targets_with_sdkmanager(); return module.exports.list_targets_with_android();
} else throw err; } else throw err;
}).then(function(targets) { })
.then(function(targets) {
if (targets.length === 0) { if (targets.length === 0) {
return Q.reject(new Error('No android targets (SDKs) installed!')); return Q.reject(new Error('No android targets (SDKs) installed!'));
} }