| 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 |
|
|---|
| 10 | http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 11 |
|
|---|
| 12 | Unless required by applicable law or agreed to in writing,
|
|---|
| 13 | software distributed under the License is distributed on an
|
|---|
| 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|---|
| 15 | KIND, either express or implied. See the License for the
|
|---|
| 16 | specific language governing permissions and limitations
|
|---|
| 17 | under the License.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | var path = require('path');
|
|---|
| 21 | var Q = require('q');
|
|---|
| 22 |
|
|---|
| 23 | var AndroidProject = require('./lib/AndroidProject');
|
|---|
| 24 | var AndroidStudio = require('./lib/AndroidStudio');
|
|---|
| 25 | var PluginManager = require('cordova-common').PluginManager;
|
|---|
| 26 |
|
|---|
| 27 | var CordovaLogger = require('cordova-common').CordovaLogger;
|
|---|
| 28 | var selfEvents = require('cordova-common').events;
|
|---|
| 29 |
|
|---|
| 30 | var PLATFORM = 'android';
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | function setupEvents(externalEventEmitter) {
|
|---|
| 34 | if (externalEventEmitter) {
|
|---|
| 35 | // This will make the platform internal events visible outside
|
|---|
| 36 | selfEvents.forwardEventsTo(externalEventEmitter);
|
|---|
| 37 | return externalEventEmitter;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | // There is no logger if external emitter is not present,
|
|---|
| 41 | // so attach a console logger
|
|---|
| 42 | CordovaLogger.get().subscribe(selfEvents);
|
|---|
| 43 | return selfEvents;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Class, that acts as abstraction over particular platform. Encapsulates the
|
|---|
| 49 | * platform's properties and methods.
|
|---|
| 50 | *
|
|---|
| 51 | * Platform that implements own PlatformApi instance _should implement all
|
|---|
| 52 | * prototype methods_ of this class to be fully compatible with cordova-lib.
|
|---|
| 53 | *
|
|---|
| 54 | * The PlatformApi instance also should define the following field:
|
|---|
| 55 | *
|
|---|
| 56 | * * platform: String that defines a platform name.
|
|---|
| 57 | */
|
|---|
| 58 | function Api(platform, platformRootDir, events) {
|
|---|
| 59 | this.platform = PLATFORM;
|
|---|
| 60 | this.root = path.resolve(__dirname, '..');
|
|---|
| 61 |
|
|---|
| 62 | setupEvents(events);
|
|---|
| 63 |
|
|---|
| 64 | var self = this;
|
|---|
| 65 |
|
|---|
| 66 | this.locations = {
|
|---|
| 67 | root: self.root,
|
|---|
| 68 | www: path.join(self.root, 'assets/www'),
|
|---|
| 69 | res: path.join(self.root, 'res'),
|
|---|
| 70 | platformWww: path.join(self.root, 'platform_www'),
|
|---|
| 71 | configXml: path.join(self.root, 'res/xml/config.xml'),
|
|---|
| 72 | defaultConfigXml: path.join(self.root, 'cordova/defaults.xml'),
|
|---|
| 73 | strings: path.join(self.root, 'res/values/strings.xml'),
|
|---|
| 74 | manifest: path.join(self.root, 'AndroidManifest.xml'),
|
|---|
| 75 | build: path.join(self.root, 'build'),
|
|---|
| 76 | // NOTE: Due to platformApi spec we need to return relative paths here
|
|---|
| 77 | cordovaJs: 'bin/templates/project/assets/www/cordova.js',
|
|---|
| 78 | cordovaJsSrc: 'cordova-js-src'
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | // XXX Override some locations for Android Studio projects
|
|---|
| 82 | if(AndroidStudio.isAndroidStudioProject(self.root) === true) {
|
|---|
| 83 | selfEvents.emit('log', 'Android Studio project detected');
|
|---|
| 84 | this.android_studio = true;
|
|---|
| 85 | this.locations.configXml = path.join(self.root, 'app/src/main/res/xml/config.xml');
|
|---|
| 86 | this.locations.strings = path.join(self.root, 'app/src/main/res/xml/strings.xml');
|
|---|
| 87 | this.locations.manifest = path.join(self.root, 'app/src/main/AndroidManifest.xml');
|
|---|
| 88 | this.locations.www = path.join(self.root, 'app/src/main/assets/www');
|
|---|
| 89 | this.locations.res = path.join(self.root, 'app/src/main/res');
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Installs platform to specified directory and creates a platform project.
|
|---|
| 95 | *
|
|---|
| 96 | * @param {String} destination Destination directory, where insatll platform to
|
|---|
| 97 | * @param {ConfigParser} [config] ConfgiParser instance, used to retrieve
|
|---|
| 98 | * project creation options, such as package id and project name.
|
|---|
| 99 | * @param {Object} [options] An options object. The most common options are:
|
|---|
| 100 | * @param {String} [options.customTemplate] A path to custom template, that
|
|---|
| 101 | * should override the default one from platform.
|
|---|
| 102 | * @param {Boolean} [options.link] Flag that indicates that platform's
|
|---|
| 103 | * sources will be linked to installed platform instead of copying.
|
|---|
| 104 | * @param {EventEmitter} [events] An EventEmitter instance that will be used for
|
|---|
| 105 | * logging purposes. If no EventEmitter provided, all events will be logged to
|
|---|
| 106 | * console
|
|---|
| 107 | *
|
|---|
| 108 | * @return {Promise<PlatformApi>} Promise either fulfilled with PlatformApi
|
|---|
| 109 | * instance or rejected with CordovaError.
|
|---|
| 110 | */
|
|---|
| 111 | Api.createPlatform = function (destination, config, options, events) {
|
|---|
| 112 | events = setupEvents(events);
|
|---|
| 113 | var result;
|
|---|
| 114 | try {
|
|---|
| 115 | result = require('../../lib/create')
|
|---|
| 116 | .create(destination, config, options, events)
|
|---|
| 117 | .then(function (destination) {
|
|---|
| 118 | var PlatformApi = require(path.resolve(destination, 'cordova/Api'));
|
|---|
| 119 | return new PlatformApi(PLATFORM, destination, events);
|
|---|
| 120 | });
|
|---|
| 121 | }
|
|---|
| 122 | catch (e) {
|
|---|
| 123 | events.emit('error','createPlatform is not callable from the android project API.');
|
|---|
| 124 | throw(e);
|
|---|
| 125 | }
|
|---|
| 126 | return result;
|
|---|
| 127 | };
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * Updates already installed platform.
|
|---|
| 131 | *
|
|---|
| 132 | * @param {String} destination Destination directory, where platform installed
|
|---|
| 133 | * @param {Object} [options] An options object. The most common options are:
|
|---|
| 134 | * @param {String} [options.customTemplate] A path to custom template, that
|
|---|
| 135 | * should override the default one from platform.
|
|---|
| 136 | * @param {Boolean} [options.link] Flag that indicates that platform's
|
|---|
| 137 | * sources will be linked to installed platform instead of copying.
|
|---|
| 138 | * @param {EventEmitter} [events] An EventEmitter instance that will be used for
|
|---|
| 139 | * logging purposes. If no EventEmitter provided, all events will be logged to
|
|---|
| 140 | * console
|
|---|
| 141 | *
|
|---|
| 142 | * @return {Promise<PlatformApi>} Promise either fulfilled with PlatformApi
|
|---|
| 143 | * instance or rejected with CordovaError.
|
|---|
| 144 | */
|
|---|
| 145 | Api.updatePlatform = function (destination, options, events) {
|
|---|
| 146 | events = setupEvents(events);
|
|---|
| 147 | var result;
|
|---|
| 148 | try {
|
|---|
| 149 | result = require('../../lib/create')
|
|---|
| 150 | .update(destination, options, events)
|
|---|
| 151 | .then(function (destination) {
|
|---|
| 152 | var PlatformApi = require(path.resolve(destination, 'cordova/Api'));
|
|---|
| 153 | return new PlatformApi('android', destination, events);
|
|---|
| 154 | });
|
|---|
| 155 | }
|
|---|
| 156 | catch (e) {
|
|---|
| 157 | events.emit('error','updatePlatform is not callable from the android project API, you will need to do this manually.');
|
|---|
| 158 | throw(e);
|
|---|
| 159 | }
|
|---|
| 160 | return result;
|
|---|
| 161 | };
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Gets a CordovaPlatform object, that represents the platform structure.
|
|---|
| 165 | *
|
|---|
| 166 | * @return {CordovaPlatform} A structure that contains the description of
|
|---|
| 167 | * platform's file structure and other properties of platform.
|
|---|
| 168 | */
|
|---|
| 169 | Api.prototype.getPlatformInfo = function () {
|
|---|
| 170 | var result = {};
|
|---|
| 171 | result.locations = this.locations;
|
|---|
| 172 | result.root = this.root;
|
|---|
| 173 | result.name = this.platform;
|
|---|
| 174 | result.version = require('./version');
|
|---|
| 175 | result.projectConfig = this._config;
|
|---|
| 176 |
|
|---|
| 177 | return result;
|
|---|
| 178 | };
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Updates installed platform with provided www assets and new app
|
|---|
| 182 | * configuration. This method is required for CLI workflow and will be called
|
|---|
| 183 | * each time before build, so the changes, made to app configuration and www
|
|---|
| 184 | * code, will be applied to platform.
|
|---|
| 185 | *
|
|---|
| 186 | * @param {CordovaProject} cordovaProject A CordovaProject instance, that defines a
|
|---|
| 187 | * project structure and configuration, that should be applied to platform
|
|---|
| 188 | * (contains project's www location and ConfigParser instance for project's
|
|---|
| 189 | * config).
|
|---|
| 190 | *
|
|---|
| 191 | * @return {Promise} Return a promise either fulfilled, or rejected with
|
|---|
| 192 | * CordovaError instance.
|
|---|
| 193 | */
|
|---|
| 194 | Api.prototype.prepare = function (cordovaProject, prepareOptions) {
|
|---|
| 195 | return require('./lib/prepare').prepare.call(this, cordovaProject, prepareOptions);
|
|---|
| 196 | };
|
|---|
| 197 |
|
|---|
| 198 | /**
|
|---|
| 199 | * Installs a new plugin into platform. This method only copies non-www files
|
|---|
| 200 | * (sources, libs, etc.) to platform. It also doesn't resolves the
|
|---|
| 201 | * dependencies of plugin. Both of handling of www files, such as assets and
|
|---|
| 202 | * js-files and resolving dependencies are the responsibility of caller.
|
|---|
| 203 | *
|
|---|
| 204 | * @param {PluginInfo} plugin A PluginInfo instance that represents plugin
|
|---|
| 205 | * that will be installed.
|
|---|
| 206 | * @param {Object} installOptions An options object. Possible options below:
|
|---|
| 207 | * @param {Boolean} installOptions.link: Flag that specifies that plugin
|
|---|
| 208 | * sources will be symlinked to app's directory instead of copying (if
|
|---|
| 209 | * possible).
|
|---|
| 210 | * @param {Object} installOptions.variables An object that represents
|
|---|
| 211 | * variables that will be used to install plugin. See more details on plugin
|
|---|
| 212 | * variables in documentation:
|
|---|
| 213 | * https://cordova.apache.org/docs/en/4.0.0/plugin_ref_spec.md.html
|
|---|
| 214 | *
|
|---|
| 215 | * @return {Promise} Return a promise either fulfilled, or rejected with
|
|---|
| 216 | * CordovaError instance.
|
|---|
| 217 | */
|
|---|
| 218 | Api.prototype.addPlugin = function (plugin, installOptions) {
|
|---|
| 219 | var project = AndroidProject.getProjectFile(this.root);
|
|---|
| 220 | var self = this;
|
|---|
| 221 |
|
|---|
| 222 | installOptions = installOptions || {};
|
|---|
| 223 | installOptions.variables = installOptions.variables || {};
|
|---|
| 224 | // Add PACKAGE_NAME variable into vars
|
|---|
| 225 | if (!installOptions.variables.PACKAGE_NAME) {
|
|---|
| 226 | installOptions.variables.PACKAGE_NAME = project.getPackageName();
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | if(this.android_studio === true) {
|
|---|
| 230 | installOptions.android_studio = true;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | return Q()
|
|---|
| 234 | .then(function () {
|
|---|
| 235 | //CB-11964: Do a clean when installing the plugin code to get around
|
|---|
| 236 | //the Gradle bug introduced by the Android Gradle Plugin Version 2.2
|
|---|
| 237 | //TODO: Delete when the next version of Android Gradle plugin comes out
|
|---|
| 238 |
|
|---|
| 239 | // Since clean doesn't just clean the build, it also wipes out www, we need
|
|---|
| 240 | // to pass additional options.
|
|---|
| 241 |
|
|---|
| 242 | // Do some basic argument parsing
|
|---|
| 243 | var opts = {};
|
|---|
| 244 |
|
|---|
| 245 | // Skip cleaning prepared files when not invoking via cordova CLI.
|
|---|
| 246 | opts.noPrepare = true;
|
|---|
| 247 |
|
|---|
| 248 | if(!AndroidStudio.isAndroidStudioProject(self.root) && !project.isClean()) {
|
|---|
| 249 | return self.clean(opts);
|
|---|
| 250 | }
|
|---|
| 251 | })
|
|---|
| 252 | .then(function () {
|
|---|
| 253 | return PluginManager.get(self.platform, self.locations, project)
|
|---|
| 254 | .addPlugin(plugin, installOptions);
|
|---|
| 255 | })
|
|---|
| 256 | .then(function () {
|
|---|
| 257 | if (plugin.getFrameworks(this.platform).length === 0) return;
|
|---|
| 258 |
|
|---|
| 259 | selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
|
|---|
| 260 | require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles();
|
|---|
| 261 | }.bind(this))
|
|---|
| 262 | // CB-11022 Return truthy value to prevent running prepare after
|
|---|
| 263 | .thenResolve(true);
|
|---|
| 264 | };
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * Removes an installed plugin from platform.
|
|---|
| 268 | *
|
|---|
| 269 | * Since method accepts PluginInfo instance as input parameter instead of plugin
|
|---|
| 270 | * id, caller shoud take care of managing/storing PluginInfo instances for
|
|---|
| 271 | * future uninstalls.
|
|---|
| 272 | *
|
|---|
| 273 | * @param {PluginInfo} plugin A PluginInfo instance that represents plugin
|
|---|
| 274 | * that will be installed.
|
|---|
| 275 | *
|
|---|
| 276 | * @return {Promise} Return a promise either fulfilled, or rejected with
|
|---|
| 277 | * CordovaError instance.
|
|---|
| 278 | */
|
|---|
| 279 | Api.prototype.removePlugin = function (plugin, uninstallOptions) {
|
|---|
| 280 | var project = AndroidProject.getProjectFile(this.root);
|
|---|
| 281 |
|
|---|
| 282 | if(uninstallOptions && uninstallOptions.usePlatformWww === true && this.android_studio === true) {
|
|---|
| 283 | uninstallOptions.usePlatformWww = false;
|
|---|
| 284 | uninstallOptions.android_studio = true;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | return PluginManager.get(this.platform, this.locations, project)
|
|---|
| 288 | .removePlugin(plugin, uninstallOptions)
|
|---|
| 289 | .then(function () {
|
|---|
| 290 | if (plugin.getFrameworks(this.platform).length === 0) return;
|
|---|
| 291 |
|
|---|
| 292 | selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
|
|---|
| 293 | require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles();
|
|---|
| 294 | }.bind(this))
|
|---|
| 295 | // CB-11022 Return truthy value to prevent running prepare after
|
|---|
| 296 | .thenResolve(true);
|
|---|
| 297 | };
|
|---|
| 298 |
|
|---|
| 299 | /**
|
|---|
| 300 | * Builds an application package for current platform.
|
|---|
| 301 | *
|
|---|
| 302 | * @param {Object} buildOptions A build options. This object's structure is
|
|---|
| 303 | * highly depends on platform's specific. The most common options are:
|
|---|
| 304 | * @param {Boolean} buildOptions.debug Indicates that packages should be
|
|---|
| 305 | * built with debug configuration. This is set to true by default unless the
|
|---|
| 306 | * 'release' option is not specified.
|
|---|
| 307 | * @param {Boolean} buildOptions.release Indicates that packages should be
|
|---|
| 308 | * built with release configuration. If not set to true, debug configuration
|
|---|
| 309 | * will be used.
|
|---|
| 310 | * @param {Boolean} buildOptions.device Specifies that built app is intended
|
|---|
| 311 | * to run on device
|
|---|
| 312 | * @param {Boolean} buildOptions.emulator: Specifies that built app is
|
|---|
| 313 | * intended to run on emulator
|
|---|
| 314 | * @param {String} buildOptions.target Specifies the device id that will be
|
|---|
| 315 | * used to run built application.
|
|---|
| 316 | * @param {Boolean} buildOptions.nobuild Indicates that this should be a
|
|---|
| 317 | * dry-run call, so no build artifacts will be produced.
|
|---|
| 318 | * @param {String[]} buildOptions.archs Specifies chip architectures which
|
|---|
| 319 | * app packages should be built for. List of valid architectures is depends on
|
|---|
| 320 | * platform.
|
|---|
| 321 | * @param {String} buildOptions.buildConfig The path to build configuration
|
|---|
| 322 | * file. The format of this file is depends on platform.
|
|---|
| 323 | * @param {String[]} buildOptions.argv Raw array of command-line arguments,
|
|---|
| 324 | * passed to `build` command. The purpose of this property is to pass a
|
|---|
| 325 | * platform-specific arguments, and eventually let platform define own
|
|---|
| 326 | * arguments processing logic.
|
|---|
| 327 | *
|
|---|
| 328 | * @return {Promise<Object[]>} A promise either fulfilled with an array of build
|
|---|
| 329 | * artifacts (application packages) if package was built successfully,
|
|---|
| 330 | * or rejected with CordovaError. The resultant build artifact objects is not
|
|---|
| 331 | * strictly typed and may conatin arbitrary set of fields as in sample below.
|
|---|
| 332 | *
|
|---|
| 333 | * {
|
|---|
| 334 | * architecture: 'x86',
|
|---|
| 335 | * buildType: 'debug',
|
|---|
| 336 | * path: '/path/to/build',
|
|---|
| 337 | * type: 'app'
|
|---|
| 338 | * }
|
|---|
| 339 | *
|
|---|
| 340 | * The return value in most cases will contain only one item but in some cases
|
|---|
| 341 | * there could be multiple items in output array, e.g. when multiple
|
|---|
| 342 | * arhcitectures is specified.
|
|---|
| 343 | */
|
|---|
| 344 | Api.prototype.build = function (buildOptions) {
|
|---|
| 345 | var self = this;
|
|---|
| 346 | return require('./lib/check_reqs').run()
|
|---|
| 347 | .then(function () {
|
|---|
| 348 | return require('./lib/build').run.call(self, buildOptions);
|
|---|
| 349 | })
|
|---|
| 350 | .then(function (buildResults) {
|
|---|
| 351 | // Cast build result to array of build artifacts
|
|---|
| 352 | return buildResults.apkPaths.map(function (apkPath) {
|
|---|
| 353 | return {
|
|---|
| 354 | buildType: buildResults.buildType,
|
|---|
| 355 | buildMethod: buildResults.buildMethod,
|
|---|
| 356 | path: apkPath,
|
|---|
| 357 | type: 'apk'
|
|---|
| 358 | };
|
|---|
| 359 | });
|
|---|
| 360 | });
|
|---|
| 361 | };
|
|---|
| 362 |
|
|---|
| 363 | /**
|
|---|
| 364 | * Builds an application package for current platform and runs it on
|
|---|
| 365 | * specified/default device. If no 'device'/'emulator'/'target' options are
|
|---|
| 366 | * specified, then tries to run app on default device if connected, otherwise
|
|---|
| 367 | * runs the app on emulator.
|
|---|
| 368 | *
|
|---|
| 369 | * @param {Object} runOptions An options object. The structure is the same
|
|---|
| 370 | * as for build options.
|
|---|
| 371 | *
|
|---|
| 372 | * @return {Promise} A promise either fulfilled if package was built and ran
|
|---|
| 373 | * successfully, or rejected with CordovaError.
|
|---|
| 374 | */
|
|---|
| 375 | Api.prototype.run = function(runOptions) {
|
|---|
| 376 | var self = this;
|
|---|
| 377 | return require('./lib/check_reqs').run()
|
|---|
| 378 | .then(function () {
|
|---|
| 379 | return require('./lib/run').run.call(self, runOptions);
|
|---|
| 380 | });
|
|---|
| 381 | };
|
|---|
| 382 |
|
|---|
| 383 | /**
|
|---|
| 384 | * Cleans out the build artifacts from platform's directory, and also
|
|---|
| 385 | * cleans out the platform www directory if called without options specified.
|
|---|
| 386 | *
|
|---|
| 387 | * @return {Promise} Return a promise either fulfilled, or rejected with
|
|---|
| 388 | * CordovaError.
|
|---|
| 389 | */
|
|---|
| 390 | Api.prototype.clean = function(cleanOptions) {
|
|---|
| 391 | var self = this;
|
|---|
| 392 | return require('./lib/check_reqs').run()
|
|---|
| 393 | .then(function () {
|
|---|
| 394 | return require('./lib/build').runClean.call(self, cleanOptions);
|
|---|
| 395 | })
|
|---|
| 396 | .then(function () {
|
|---|
| 397 | return require('./lib/prepare').clean.call(self, cleanOptions);
|
|---|
| 398 | });
|
|---|
| 399 | };
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 | /**
|
|---|
| 404 | * Performs a requirements check for current platform. Each platform defines its
|
|---|
| 405 | * own set of requirements, which should be resolved before platform can be
|
|---|
| 406 | * built successfully.
|
|---|
| 407 | *
|
|---|
| 408 | * @return {Promise<Requirement[]>} Promise, resolved with set of Requirement
|
|---|
| 409 | * objects for current platform.
|
|---|
| 410 | */
|
|---|
| 411 | Api.prototype.requirements = function() {
|
|---|
| 412 | return require('./lib/check_reqs').check_all();
|
|---|
| 413 | };
|
|---|
| 414 |
|
|---|
| 415 | module.exports = Api;
|
|---|