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