Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

Api.js 15KB

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