2013-08-13 08:07:23 +08:00
|
|
|
/*
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
or more contributor license agreements. See the NOTICE file
|
|
|
|
distributed with this work for additional information
|
|
|
|
regarding copyright ownership. The ASF licenses this file
|
|
|
|
to you under the Apache License, Version 2.0 (the
|
|
|
|
"License"); you may not use this file except in compliance
|
|
|
|
with the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing,
|
|
|
|
software distributed under the License is distributed on an
|
|
|
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
KIND, either express or implied. See the License for the
|
|
|
|
specific language governing permissions and limitations
|
|
|
|
under the License.
|
|
|
|
*/
|
|
|
|
|
2017-06-14 02:42:20 +08:00
|
|
|
var emulator = require('./emulator');
|
2020-11-21 05:12:18 +08:00
|
|
|
const target = require('./target');
|
2019-08-09 00:53:10 +08:00
|
|
|
var PackageType = require('./PackageType');
|
2021-04-09 14:37:56 +08:00
|
|
|
const { events } = require('cordova-common');
|
2013-08-13 08:07:23 +08:00
|
|
|
|
2021-04-09 14:37:56 +08:00
|
|
|
/**
|
|
|
|
* Builds a target spec from a runOptions object
|
|
|
|
*
|
|
|
|
* @param {{target?: string, device?: boolean, emulator?: boolean}} runOptions
|
|
|
|
* @return {target.TargetSpec}
|
|
|
|
*/
|
|
|
|
function buildTargetSpec (runOptions) {
|
|
|
|
const spec = {};
|
2016-02-19 02:27:40 +08:00
|
|
|
if (runOptions.target) {
|
2021-04-09 14:37:56 +08:00
|
|
|
spec.id = runOptions.target;
|
2016-02-19 02:27:40 +08:00
|
|
|
} else if (runOptions.device) {
|
2021-04-09 14:37:56 +08:00
|
|
|
spec.type = 'device';
|
2016-02-19 02:27:40 +08:00
|
|
|
} else if (runOptions.emulator) {
|
2021-04-09 14:37:56 +08:00
|
|
|
spec.type = 'emulator';
|
2016-02-19 02:27:40 +08:00
|
|
|
}
|
2021-04-09 14:37:56 +08:00
|
|
|
return spec;
|
|
|
|
}
|
2016-02-19 02:27:40 +08:00
|
|
|
|
2021-04-09 14:37:56 +08:00
|
|
|
function formatResolvedTarget ({ id, type }) {
|
|
|
|
return `${type} ${id}`;
|
2016-02-19 02:27:40 +08:00
|
|
|
}
|
|
|
|
|
2015-10-20 15:30:31 +08:00
|
|
|
/**
|
|
|
|
* Runs the application on a device if available. If no device is found, it will
|
|
|
|
* use a started emulator. If no started emulators are found it will attempt
|
|
|
|
* to start an avd. If no avds are found it will error out.
|
|
|
|
*
|
|
|
|
* @param {Object} runOptions various run/build options. See Api.js build/run
|
|
|
|
* methods for reference.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
2013-08-13 08:07:23 +08:00
|
|
|
*/
|
2017-06-14 02:42:20 +08:00
|
|
|
module.exports.run = function (runOptions) {
|
2018-09-06 10:06:18 +08:00
|
|
|
runOptions = runOptions || {};
|
2013-08-13 08:07:23 +08:00
|
|
|
|
2015-10-20 15:30:31 +08:00
|
|
|
var self = this;
|
2021-04-09 14:37:56 +08:00
|
|
|
const spec = buildTargetSpec(runOptions);
|
|
|
|
|
|
|
|
return target.resolve(spec).then(function (resolvedTarget) {
|
|
|
|
events.emit('log', `Deploying to ${formatResolvedTarget(resolvedTarget)}`);
|
2014-12-30 03:43:46 +08:00
|
|
|
|
2018-09-06 10:06:18 +08:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
const buildOptions = require('./build').parseBuildOptions(runOptions, null, self.root);
|
2019-08-09 00:53:10 +08:00
|
|
|
|
|
|
|
// Android app bundles cannot be deployed directly to the device
|
|
|
|
if (buildOptions.packageType === PackageType.BUNDLE) {
|
|
|
|
const packageTypeErrorMessage = 'Package type "bundle" is not supported during cordova run.';
|
|
|
|
events.emit('error', packageTypeErrorMessage);
|
|
|
|
throw packageTypeErrorMessage;
|
|
|
|
}
|
|
|
|
|
2020-07-04 00:54:24 +08:00
|
|
|
resolve(self._builder.fetchBuildResults(buildOptions.buildType, buildOptions.arch));
|
2020-11-21 05:12:18 +08:00
|
|
|
}).then(async function (buildResults) {
|
2021-04-09 14:37:56 +08:00
|
|
|
if (resolvedTarget.type === 'emulator') {
|
2020-11-21 05:12:18 +08:00
|
|
|
await emulator.wait_for_boot(resolvedTarget.id);
|
2014-09-25 04:07:31 +08:00
|
|
|
}
|
2018-09-06 10:06:18 +08:00
|
|
|
|
2020-11-21 05:12:18 +08:00
|
|
|
return target.install(resolvedTarget, buildResults);
|
2014-09-25 04:07:31 +08:00
|
|
|
});
|
2013-09-27 23:48:55 +08:00
|
|
|
});
|
2015-02-04 04:10:50 +08:00
|
|
|
};
|