run.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 Q = require('q');
  18. var path = require('path');
  19. var iossim = require('ios-sim');
  20. var build = require('./build');
  21. var spawn = require('./spawn');
  22. var check_reqs = require('./check_reqs');
  23. var events = require('cordova-common').events;
  24. var cordovaPath = path.join(__dirname, '..');
  25. var projectPath = path.join(__dirname, '..', '..');
  26. module.exports.run = function (runOptions) {
  27. // Validate args
  28. if (runOptions.device && runOptions.emulator) {
  29. return Q.reject('Only one of "device"/"emulator" options should be specified');
  30. }
  31. // support for CB-8168 `cordova/run --list`
  32. if (runOptions.list) {
  33. if (runOptions.device) return module.exports.listDevices();
  34. if (runOptions.emulator) return module.exports.listEmulators();
  35. // if no --device or --emulator flag is specified, list both devices and emulators
  36. return module.exports.listDevices().then(function () {
  37. return module.exports.listEmulators();
  38. });
  39. }
  40. var useDevice = !!runOptions.device;
  41. return require('./list-devices').run()
  42. .then(function (devices) {
  43. if (devices.length > 0 && !(runOptions.emulator)) {
  44. useDevice = true;
  45. // we also explicitly set device flag in options as we pass
  46. // those parameters to other api (build as an example)
  47. runOptions.device = true;
  48. return check_reqs.check_ios_deploy();
  49. }
  50. }).then(function () {
  51. if (!runOptions.nobuild) {
  52. return build.run(runOptions);
  53. } else {
  54. return Q.resolve();
  55. }
  56. }).then(function () {
  57. return build.findXCodeProjectIn(projectPath);
  58. }).then(function (projectName) {
  59. var appPath = path.join(projectPath, 'build', 'emulator', projectName + '.app');
  60. var buildOutputDir = path.join(projectPath, 'build', 'device');
  61. // select command to run and arguments depending whether
  62. // we're running on device/emulator
  63. if (useDevice) {
  64. return module.exports.checkDeviceConnected()
  65. .then(function () {
  66. // Unpack IPA
  67. var ipafile = path.join(buildOutputDir, projectName + '.ipa');
  68. // unpack the existing platform/ios/build/device/appname.ipa (zipfile), will create a Payload folder
  69. return spawn('unzip', [ '-o', '-qq', ipafile ], buildOutputDir);
  70. })
  71. .then(function () {
  72. // Uncompress IPA (zip file)
  73. var appFileInflated = path.join(buildOutputDir, 'Payload', projectName + '.app');
  74. var appFile = path.join(buildOutputDir, projectName + '.app');
  75. var payloadFolder = path.join(buildOutputDir, 'Payload');
  76. // delete the existing platform/ios/build/device/appname.app
  77. return spawn('rm', [ '-rf', appFile ], buildOutputDir)
  78. .then(function () {
  79. // move the platform/ios/build/device/Payload/appname.app to parent
  80. return spawn('mv', [ '-f', appFileInflated, buildOutputDir ], buildOutputDir);
  81. })
  82. .then(function () {
  83. // delete the platform/ios/build/device/Payload folder
  84. return spawn('rm', [ '-rf', payloadFolder ], buildOutputDir);
  85. });
  86. })
  87. .then(function () {
  88. appPath = path.join(projectPath, 'build', 'device', projectName + '.app');
  89. var extraArgs = [];
  90. if (runOptions.argv) {
  91. // argv.slice(2) removes node and run.js, filterSupportedArgs removes the run.js args
  92. extraArgs = module.exports.filterSupportedArgs(runOptions.argv.slice(2));
  93. }
  94. return module.exports.deployToDevice(appPath, runOptions.target, extraArgs);
  95. }, function () {
  96. // if device connection check failed use emulator then
  97. return module.exports.deployToSim(appPath, runOptions.target);
  98. });
  99. } else {
  100. return module.exports.deployToSim(appPath, runOptions.target);
  101. }
  102. });
  103. };
  104. module.exports.filterSupportedArgs = filterSupportedArgs;
  105. module.exports.checkDeviceConnected = checkDeviceConnected;
  106. module.exports.deployToDevice = deployToDevice;
  107. module.exports.deployToSim = deployToSim;
  108. module.exports.startSim = startSim;
  109. module.exports.listDevices = listDevices;
  110. module.exports.listEmulators = listEmulators;
  111. /**
  112. * Filters the args array and removes supported args for the 'run' command.
  113. *
  114. * @return {Array} array with unsupported args for the 'run' command
  115. */
  116. function filterSupportedArgs (args) {
  117. var filtered = [];
  118. var sargs = ['--device', '--emulator', '--nobuild', '--list', '--target', '--debug', '--release'];
  119. var re = new RegExp(sargs.join('|'));
  120. args.forEach(function (element) {
  121. // supported args not found, we add
  122. // we do a regex search because --target can be "--target=XXX"
  123. if (element.search(re) === -1) {
  124. filtered.push(element);
  125. }
  126. }, this);
  127. return filtered;
  128. }
  129. /**
  130. * Checks if any iOS device is connected
  131. * @return {Promise} Fullfilled when any device is connected, rejected otherwise
  132. */
  133. function checkDeviceConnected () {
  134. return spawn('ios-deploy', ['-c', '-t', '1']);
  135. }
  136. /**
  137. * Deploy specified app package to connected device
  138. * using ios-deploy command
  139. * @param {String} appPath Path to application package
  140. * @return {Promise} Resolves when deploy succeeds otherwise rejects
  141. */
  142. function deployToDevice (appPath, target, extraArgs) {
  143. // Deploying to device...
  144. if (target) {
  145. return spawn('ios-deploy', ['--justlaunch', '-d', '-b', appPath, '-i', target].concat(extraArgs));
  146. } else {
  147. return spawn('ios-deploy', ['--justlaunch', '--no-wifi', '-d', '-b', appPath].concat(extraArgs));
  148. }
  149. }
  150. /**
  151. * Deploy specified app package to ios-sim simulator
  152. * @param {String} appPath Path to application package
  153. * @param {String} target Target device type
  154. * @return {Promise} Resolves when deploy succeeds otherwise rejects
  155. */
  156. function deployToSim (appPath, target) {
  157. // Select target device for emulator. Default is 'iPhone-6'
  158. if (!target) {
  159. return require('./list-emulator-images').run()
  160. .then(function (emulators) {
  161. if (emulators.length > 0) {
  162. target = emulators[0];
  163. }
  164. emulators.forEach(function (emulator) {
  165. if (emulator.indexOf('iPhone') === 0) {
  166. target = emulator;
  167. }
  168. });
  169. events.emit('log', 'No target specified for emulator. Deploying to ' + target + ' simulator');
  170. return startSim(appPath, target);
  171. });
  172. } else {
  173. return startSim(appPath, target);
  174. }
  175. }
  176. function startSim (appPath, target) {
  177. var logPath = path.join(cordovaPath, 'console.log');
  178. return iossim.launch(appPath, 'com.apple.CoreSimulator.SimDeviceType.' + target, logPath, '--exit');
  179. }
  180. function listDevices () {
  181. return require('./list-devices').run()
  182. .then(function (devices) {
  183. events.emit('log', 'Available iOS Devices:');
  184. devices.forEach(function (device) {
  185. events.emit('log', '\t' + device);
  186. });
  187. });
  188. }
  189. function listEmulators () {
  190. return require('./list-emulator-images').run()
  191. .then(function (emulators) {
  192. events.emit('log', 'Available iOS Simulators:');
  193. emulators.forEach(function (emulator) {
  194. events.emit('log', '\t' + emulator);
  195. });
  196. });
  197. }
  198. module.exports.help = function () {
  199. console.log('\nUsage: run [ --device | [ --emulator [ --target=<id> ] ] ] [ --debug | --release | --nobuild ]');
  200. // TODO: add support for building different archs
  201. // console.log(" [ --archs=\"<list of target architectures>\" ] ");
  202. console.log(' --device : Deploys and runs the project on the connected device.');
  203. console.log(' --emulator : Deploys and runs the project on an emulator.');
  204. console.log(' --target=<id> : Deploys and runs the project on the specified target.');
  205. console.log(' --debug : Builds project in debug mode. (Passed down to build command, if necessary)');
  206. console.log(' --release : Builds project in release mode. (Passed down to build command, if necessary)');
  207. console.log(' --nobuild : Uses pre-built package, or errors if project is not built.');
  208. // TODO: add support for building different archs
  209. // console.log(" --archs : Specific chip architectures (`anycpu`, `arm`, `x86`, `x64`).");
  210. console.log('');
  211. console.log('Examples:');
  212. console.log(' run');
  213. console.log(' run --device');
  214. console.log(' run --emulator --target=\"iPhone-6-Plus\"'); /* eslint no-useless-escape : 0 */
  215. console.log(' run --device --release');
  216. console.log(' run --emulator --debug');
  217. console.log('');
  218. process.exit(0);
  219. };