android_sdk.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. var superspawn = require('cordova-common').superspawn;
  18. var suffix_number_regex = /(\d+)$/;
  19. // Used for sorting Android targets, example strings to sort:
  20. // android-19
  21. // android-L
  22. // Google Inc.:Google APIs:20
  23. // Google Inc.:Glass Development Kit Preview:20
  24. // The idea is to sort based on largest "suffix" number - meaning the bigger
  25. // the number at the end, the more recent the target, the closer to the
  26. // start of the array.
  27. function sort_by_largest_numerical_suffix (a, b) {
  28. var suffix_a = a.match(suffix_number_regex);
  29. var suffix_b = b.match(suffix_number_regex);
  30. if (suffix_a && suffix_b) {
  31. // If the two targets being compared have suffixes, return less than
  32. // zero, or greater than zero, based on which suffix is larger.
  33. return (parseInt(suffix_a[1]) > parseInt(suffix_b[1]) ? -1 : 1);
  34. } else {
  35. // If no suffix numbers were detected, leave the order as-is between
  36. // elements a and b.
  37. return 0;
  38. }
  39. }
  40. module.exports.print_newest_available_sdk_target = function () {
  41. return module.exports.list_targets().then(function (targets) {
  42. targets.sort(sort_by_largest_numerical_suffix);
  43. console.log(targets[0]);
  44. });
  45. };
  46. module.exports.version_string_to_api_level = {
  47. '4.0': 14,
  48. '4.0.3': 15,
  49. '4.1': 16,
  50. '4.2': 17,
  51. '4.3': 18,
  52. '4.4': 19,
  53. '4.4W': 20,
  54. '5.0': 21,
  55. '5.1': 22,
  56. '6.0': 23,
  57. '7.0': 24,
  58. '7.1.1': 25,
  59. '8.0': 26
  60. };
  61. function parse_targets (output) {
  62. var target_out = output.split('\n');
  63. var targets = [];
  64. for (var i = target_out.length - 1; i >= 0; i--) {
  65. if (target_out[i].match(/id:/)) { // if "id:" is in the line...
  66. targets.push(target_out[i].match(/"(.+)"/)[1]); // .. match whatever is in quotes.
  67. }
  68. }
  69. return targets;
  70. }
  71. module.exports.list_targets_with_android = function () {
  72. return superspawn.spawn('android', ['list', 'target']).then(parse_targets);
  73. };
  74. module.exports.list_targets_with_avdmanager = function () {
  75. return superspawn.spawn('avdmanager', ['list', 'target']).then(parse_targets);
  76. };
  77. module.exports.list_targets = function () {
  78. return module.exports.list_targets_with_avdmanager().catch(function (err) {
  79. // If there's an error, like avdmanager could not be found, we can try
  80. // as a last resort, to run `android`, in case this is a super old
  81. // SDK installation.
  82. if (err && (err.code === 'ENOENT' || (err.stderr && err.stderr.match(/not recognized/)))) {
  83. return module.exports.list_targets_with_android();
  84. } else throw err;
  85. }).then(function (targets) {
  86. if (targets.length === 0) {
  87. return Promise.reject(new Error('No android targets (SDKs) installed!'));
  88. }
  89. return targets;
  90. });
  91. };