run.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env node
  2. /*
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. /* jshint loopfunc:true */
  19. var path = require('path'),
  20. build = require('./build'),
  21. emulator = require('./emulator'),
  22. device = require('./device'),
  23. shell = require('shelljs'),
  24. Q = require('q');
  25. /*
  26. * Runs the application on a device if available.
  27. * If no device is found, it will use a started emulator.
  28. * If no started emulators are found it will attempt to start an avd.
  29. * If no avds are found it will error out.
  30. * Returns a promise.
  31. */
  32. module.exports.run = function(args) {
  33. var buildFlags = [];
  34. var install_target;
  35. var list = false;
  36. for (var i=2; i<args.length; i++) {
  37. if (build.isBuildFlag(args[i])) {
  38. buildFlags.push(args[i]);
  39. } else if (args[i] == '--device') {
  40. install_target = '--device';
  41. } else if (args[i] == '--emulator') {
  42. install_target = '--emulator';
  43. } else if (/^--target=/.exec(args[i])) {
  44. install_target = args[i].substring(9, args[i].length);
  45. } else if (args[i] == '--list') {
  46. list = true;
  47. } else {
  48. console.warn('Option \'' + args[i] + '\' not recognized (ignoring).');
  49. }
  50. }
  51. if (list) {
  52. var output = '';
  53. var temp = '';
  54. if (!install_target) {
  55. output += 'Available Android Devices:\n';
  56. temp = shell.exec(path.join(__dirname, 'list-devices'), {silent:true}).output;
  57. temp = temp.replace(/^(?=[^\s])/gm, '\t');
  58. output += temp;
  59. output += 'Available Android Virtual Devices:\n';
  60. temp = shell.exec(path.join(__dirname, 'list-emulator-images'), {silent:true}).output;
  61. temp = temp.replace(/^(?=[^\s])/gm, '\t');
  62. output += temp;
  63. } else if (install_target == '--emulator') {
  64. output += 'Available Android Virtual Devices:\n';
  65. temp = shell.exec(path.join(__dirname, 'list-emulator-images'), {silent:true}).output;
  66. temp = temp.replace(/^(?=[^\s])/gm, '\t');
  67. output += temp;
  68. } else if (install_target == '--device') {
  69. output += 'Available Android Devices:\n';
  70. temp = shell.exec(path.join(__dirname, 'list-devices'), {silent:true}).output;
  71. temp = temp.replace(/^(?=[^\s])/gm, '\t');
  72. output += temp;
  73. }
  74. console.log(output);
  75. return;
  76. }
  77. return Q()
  78. .then(function() {
  79. if (!install_target) {
  80. // no target given, deploy to device if available, otherwise use the emulator.
  81. return device.list()
  82. .then(function(device_list) {
  83. if (device_list.length > 0) {
  84. console.log('WARNING : No target specified, deploying to device \'' + device_list[0] + '\'.');
  85. install_target = device_list[0];
  86. } else {
  87. console.log('WARNING : No target specified, deploying to emulator');
  88. install_target = '--emulator';
  89. }
  90. });
  91. }
  92. }).then(function() {
  93. if (install_target == '--device') {
  94. return device.resolveTarget(null);
  95. } else if (install_target == '--emulator') {
  96. // Give preference to any already started emulators. Else, start one.
  97. return emulator.list_started()
  98. .then(function(started) {
  99. return started && started.length > 0 ? started[0] : emulator.start();
  100. }).then(function(emulatorId) {
  101. return emulator.resolveTarget(emulatorId);
  102. });
  103. }
  104. // They specified a specific device/emulator ID.
  105. return device.list()
  106. .then(function(devices) {
  107. if (devices.indexOf(install_target) > -1) {
  108. return device.resolveTarget(install_target);
  109. }
  110. return emulator.list_started()
  111. .then(function(started_emulators) {
  112. if (started_emulators.indexOf(install_target) > -1) {
  113. return emulator.resolveTarget(install_target);
  114. }
  115. return emulator.list_images()
  116. .then(function(avds) {
  117. // if target emulator isn't started, then start it.
  118. for (var avd in avds) {
  119. if (avds[avd].name == install_target) {
  120. return emulator.start(install_target)
  121. .then(function(emulatorId) {
  122. return emulator.resolveTarget(emulatorId);
  123. });
  124. }
  125. }
  126. return Q.reject('Target \'' + install_target + '\' not found, unable to run project');
  127. });
  128. });
  129. });
  130. }).then(function(resolvedTarget) {
  131. return build.run(buildFlags, resolvedTarget).then(function(buildResults) {
  132. if (resolvedTarget.isEmulator) {
  133. return emulator.install(resolvedTarget, buildResults);
  134. }
  135. return device.install(resolvedTarget, buildResults);
  136. });
  137. });
  138. };
  139. module.exports.help = function(args) {
  140. console.log('Usage: ' + path.relative(process.cwd(), args[1]) + ' [options]');
  141. console.log('Build options :');
  142. console.log(' --debug : Builds project in debug mode');
  143. console.log(' --release : Builds project in release mode');
  144. console.log(' --nobuild : Runs the currently built project without recompiling');
  145. console.log('Deploy options :');
  146. console.log(' --device : Will deploy the built project to a device');
  147. console.log(' --emulator : Will deploy the built project to an emulator if one exists');
  148. console.log(' --target=<target_id> : Installs to the target with the specified id.');
  149. process.exit(0);
  150. };