You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 lines
14 KiB

8 years ago
  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 path = require('path');
  18. var AndroidProject = require('./lib/AndroidProject');
  19. var PluginManager = require('cordova-common').PluginManager;
  20. var CordovaLogger = require('cordova-common').CordovaLogger;
  21. var selfEvents = require('cordova-common').events;
  22. var PLATFORM = 'android';
  23. function setupEvents(externalEventEmitter) {
  24. if (externalEventEmitter) {
  25. // This will make the platform internal events visible outside
  26. selfEvents.forwardEventsTo(externalEventEmitter);
  27. return externalEventEmitter;
  28. }
  29. // There is no logger if external emitter is not present,
  30. // so attach a console logger
  31. CordovaLogger.get().subscribe(selfEvents);
  32. return selfEvents;
  33. }
  34. /**
  35. * Class, that acts as abstraction over particular platform. Encapsulates the
  36. * platform's properties and methods.
  37. *
  38. * Platform that implements own PlatformApi instance _should implement all
  39. * prototype methods_ of this class to be fully compatible with cordova-lib.
  40. *
  41. * The PlatformApi instance also should define the following field:
  42. *
  43. * * platform: String that defines a platform name.
  44. */
  45. function Api(platform, platformRootDir, events) {
  46. this.platform = PLATFORM;
  47. this.root = path.resolve(__dirname, '..');
  48. setupEvents(events);
  49. var self = this;
  50. this.locations = {
  51. root: self.root,
  52. www: path.join(self.root, 'assets/www'),
  53. platformWww: path.join(self.root, 'platform_www'),
  54. configXml: path.join(self.root, 'res/xml/config.xml'),
  55. defaultConfigXml: path.join(self.root, 'cordova/defaults.xml'),
  56. strings: path.join(self.root, 'res/values/strings.xml'),
  57. manifest: path.join(self.root, 'AndroidManifest.xml'),
  58. // NOTE: Due to platformApi spec we need to return relative paths here
  59. cordovaJs: 'bin/templates/project/assets/www/cordova.js',
  60. cordovaJsSrc: 'cordova-js-src'
  61. };
  62. }
  63. /**
  64. * Installs platform to specified directory and creates a platform project.
  65. *
  66. * @param {String} destination Destination directory, where insatll platform to
  67. * @param {ConfigParser} [config] ConfgiParser instance, used to retrieve
  68. * project creation options, such as package id and project name.
  69. * @param {Object} [options] An options object. The most common options are:
  70. * @param {String} [options.customTemplate] A path to custom template, that
  71. * should override the default one from platform.
  72. * @param {Boolean} [options.link] Flag that indicates that platform's
  73. * sources will be linked to installed platform instead of copying.
  74. * @param {EventEmitter} [events] An EventEmitter instance that will be used for
  75. * logging purposes. If no EventEmitter provided, all events will be logged to
  76. * console
  77. *
  78. * @return {Promise<PlatformApi>} Promise either fulfilled with PlatformApi
  79. * instance or rejected with CordovaError.
  80. */
  81. Api.createPlatform = function (destination, config, options, events) {
  82. events = setupEvents(events);
  83. return require('../../lib/create')
  84. .create(destination, config, options, events)
  85. .then(function (destination) {
  86. var PlatformApi = require(path.resolve(destination, 'cordova/Api'));
  87. return new PlatformApi(PLATFORM, destination, events);
  88. });
  89. };
  90. /**
  91. * Updates already installed platform.
  92. *
  93. * @param {String} destination Destination directory, where platform installed
  94. * @param {Object} [options] An options object. The most common options are:
  95. * @param {String} [options.customTemplate] A path to custom template, that
  96. * should override the default one from platform.
  97. * @param {Boolean} [options.link] Flag that indicates that platform's
  98. * sources will be linked to installed platform instead of copying.
  99. * @param {EventEmitter} [events] An EventEmitter instance that will be used for
  100. * logging purposes. If no EventEmitter provided, all events will be logged to
  101. * console
  102. *
  103. * @return {Promise<PlatformApi>} Promise either fulfilled with PlatformApi
  104. * instance or rejected with CordovaError.
  105. */
  106. Api.updatePlatform = function (destination, options, events) {
  107. events = setupEvents(events);
  108. return require('../../lib/create')
  109. .update(destination, options, events)
  110. .then(function (destination) {
  111. var PlatformApi = require(path.resolve(destination, 'cordova/Api'));
  112. return new PlatformApi('android', destination, events);
  113. });
  114. };
  115. /**
  116. * Gets a CordovaPlatform object, that represents the platform structure.
  117. *
  118. * @return {CordovaPlatform} A structure that contains the description of
  119. * platform's file structure and other properties of platform.
  120. */
  121. Api.prototype.getPlatformInfo = function () {
  122. var result = {};
  123. result.locations = this.locations;
  124. result.root = this.root;
  125. result.name = this.platform;
  126. result.version = require('./version');
  127. result.projectConfig = this._config;
  128. return result;
  129. };
  130. /**
  131. * Updates installed platform with provided www assets and new app
  132. * configuration. This method is required for CLI workflow and will be called
  133. * each time before build, so the changes, made to app configuration and www
  134. * code, will be applied to platform.
  135. *
  136. * @param {CordovaProject} cordovaProject A CordovaProject instance, that defines a
  137. * project structure and configuration, that should be applied to platform
  138. * (contains project's www location and ConfigParser instance for project's
  139. * config).
  140. *
  141. * @return {Promise} Return a promise either fulfilled, or rejected with
  142. * CordovaError instance.
  143. */
  144. Api.prototype.prepare = function (cordovaProject, prepareOptions) {
  145. return require('./lib/prepare').prepare.call(this, cordovaProject, prepareOptions);
  146. };
  147. /**
  148. * Installs a new plugin into platform. This method only copies non-www files
  149. * (sources, libs, etc.) to platform. It also doesn't resolves the
  150. * dependencies of plugin. Both of handling of www files, such as assets and
  151. * js-files and resolving dependencies are the responsibility of caller.
  152. *
  153. * @param {PluginInfo} plugin A PluginInfo instance that represents plugin
  154. * that will be installed.
  155. * @param {Object} installOptions An options object. Possible options below:
  156. * @param {Boolean} installOptions.link: Flag that specifies that plugin
  157. * sources will be symlinked to app's directory instead of copying (if
  158. * possible).
  159. * @param {Object} installOptions.variables An object that represents
  160. * variables that will be used to install plugin. See more details on plugin
  161. * variables in documentation:
  162. * https://cordova.apache.org/docs/en/4.0.0/plugin_ref_spec.md.html
  163. *
  164. * @return {Promise} Return a promise either fulfilled, or rejected with
  165. * CordovaError instance.
  166. */
  167. Api.prototype.addPlugin = function (plugin, installOptions) {
  168. var project = AndroidProject.getProjectFile(this.root);
  169. installOptions = installOptions || {};
  170. installOptions.variables = installOptions.variables || {};
  171. // Add PACKAGE_NAME variable into vars
  172. if (!installOptions.variables.PACKAGE_NAME) {
  173. installOptions.variables.PACKAGE_NAME = project.getPackageName();
  174. }
  175. return PluginManager.get(this.platform, this.locations, project)
  176. .addPlugin(plugin, installOptions)
  177. .then(function () {
  178. if (plugin.getFrameworks(this.platform).length === 0) return;
  179. selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
  180. require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles();
  181. }.bind(this))
  182. // CB-11022 Return truthy value to prevent running prepare after
  183. .thenResolve(true);
  184. };
  185. /**
  186. * Removes an installed plugin from platform.
  187. *
  188. * Since method accepts PluginInfo instance as input parameter instead of plugin
  189. * id, caller shoud take care of managing/storing PluginInfo instances for
  190. * future uninstalls.
  191. *
  192. * @param {PluginInfo} plugin A PluginInfo instance that represents plugin
  193. * that will be installed.
  194. *
  195. * @return {Promise} Return a promise either fulfilled, or rejected with
  196. * CordovaError instance.
  197. */
  198. Api.prototype.removePlugin = function (plugin, uninstallOptions) {
  199. var project = AndroidProject.getProjectFile(this.root);
  200. return PluginManager.get(this.platform, this.locations, project)
  201. .removePlugin(plugin, uninstallOptions)
  202. .then(function () {
  203. if (plugin.getFrameworks(this.platform).length === 0) return;
  204. selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
  205. require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles();
  206. }.bind(this))
  207. // CB-11022 Return truthy value to prevent running prepare after
  208. .thenResolve(true);
  209. };
  210. /**
  211. * Builds an application package for current platform.
  212. *
  213. * @param {Object} buildOptions A build options. This object's structure is
  214. * highly depends on platform's specific. The most common options are:
  215. * @param {Boolean} buildOptions.debug Indicates that packages should be
  216. * built with debug configuration. This is set to true by default unless the
  217. * 'release' option is not specified.
  218. * @param {Boolean} buildOptions.release Indicates that packages should be
  219. * built with release configuration. If not set to true, debug configuration
  220. * will be used.
  221. * @param {Boolean} buildOptions.device Specifies that built app is intended
  222. * to run on device
  223. * @param {Boolean} buildOptions.emulator: Specifies that built app is
  224. * intended to run on emulator
  225. * @param {String} buildOptions.target Specifies the device id that will be
  226. * used to run built application.
  227. * @param {Boolean} buildOptions.nobuild Indicates that this should be a
  228. * dry-run call, so no build artifacts will be produced.
  229. * @param {String[]} buildOptions.archs Specifies chip architectures which
  230. * app packages should be built for. List of valid architectures is depends on
  231. * platform.
  232. * @param {String} buildOptions.buildConfig The path to build configuration
  233. * file. The format of this file is depends on platform.
  234. * @param {String[]} buildOptions.argv Raw array of command-line arguments,
  235. * passed to `build` command. The purpose of this property is to pass a
  236. * platform-specific arguments, and eventually let platform define own
  237. * arguments processing logic.
  238. *
  239. * @return {Promise<Object[]>} A promise either fulfilled with an array of build
  240. * artifacts (application packages) if package was built successfully,
  241. * or rejected with CordovaError. The resultant build artifact objects is not
  242. * strictly typed and may conatin arbitrary set of fields as in sample below.
  243. *
  244. * {
  245. * architecture: 'x86',
  246. * buildType: 'debug',
  247. * path: '/path/to/build',
  248. * type: 'app'
  249. * }
  250. *
  251. * The return value in most cases will contain only one item but in some cases
  252. * there could be multiple items in output array, e.g. when multiple
  253. * arhcitectures is specified.
  254. */
  255. Api.prototype.build = function (buildOptions) {
  256. var self = this;
  257. return require('./lib/check_reqs').run()
  258. .then(function () {
  259. return require('./lib/build').run.call(self, buildOptions);
  260. })
  261. .then(function (buildResults) {
  262. // Cast build result to array of build artifacts
  263. return buildResults.apkPaths.map(function (apkPath) {
  264. return {
  265. buildType: buildResults.buildType,
  266. buildMethod: buildResults.buildMethod,
  267. path: apkPath,
  268. type: 'apk'
  269. };
  270. });
  271. });
  272. };
  273. /**
  274. * Builds an application package for current platform and runs it on
  275. * specified/default device. If no 'device'/'emulator'/'target' options are
  276. * specified, then tries to run app on default device if connected, otherwise
  277. * runs the app on emulator.
  278. *
  279. * @param {Object} runOptions An options object. The structure is the same
  280. * as for build options.
  281. *
  282. * @return {Promise} A promise either fulfilled if package was built and ran
  283. * successfully, or rejected with CordovaError.
  284. */
  285. Api.prototype.run = function(runOptions) {
  286. var self = this;
  287. return require('./lib/check_reqs').run()
  288. .then(function () {
  289. return require('./lib/run').run.call(self, runOptions);
  290. });
  291. };
  292. /**
  293. * Cleans out the build artifacts from platform's directory.
  294. *
  295. * @return {Promise} Return a promise either fulfilled, or rejected with
  296. * CordovaError.
  297. */
  298. Api.prototype.clean = function(cleanOptions) {
  299. var self = this;
  300. return require('./lib/check_reqs').run()
  301. .then(function () {
  302. return require('./lib/build').runClean.call(self, cleanOptions);
  303. })
  304. .then(function () {
  305. return require('./lib/prepare').clean.call(self, cleanOptions);
  306. });
  307. };
  308. /**
  309. * Performs a requirements check for current platform. Each platform defines its
  310. * own set of requirements, which should be resolved before platform can be
  311. * built successfully.
  312. *
  313. * @return {Promise<Requirement[]>} Promise, resolved with set of Requirement
  314. * objects for current platform.
  315. */
  316. Api.prototype.requirements = function() {
  317. return require('./lib/check_reqs').check_all();
  318. };
  319. module.exports = Api;