Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

PluginManager.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Q = require('q');
  18. var fs = require('fs-extra');
  19. var path = require('path');
  20. var ActionStack = require('./ActionStack');
  21. var PlatformJson = require('./PlatformJson');
  22. var CordovaError = require('./CordovaError/CordovaError');
  23. var PlatformMunger = require('./ConfigChanges/ConfigChanges').PlatformMunger;
  24. var PluginInfoProvider = require('./PluginInfo/PluginInfoProvider');
  25. /**
  26. * @constructor
  27. * @class PluginManager
  28. * Represents an entity for adding/removing plugins for platforms
  29. *
  30. * @param {String} platform Platform name
  31. * @param {Object} locations - Platform files and directories
  32. * @param {IDEProject} ideProject The IDE project to add/remove plugin changes to/from
  33. */
  34. function PluginManager (platform, locations, ideProject) {
  35. this.platform = platform;
  36. this.locations = locations;
  37. this.project = ideProject;
  38. var platformJson = PlatformJson.load(locations.root, platform);
  39. this.munger = new PlatformMunger(platform, locations.root, platformJson, new PluginInfoProvider());
  40. }
  41. /**
  42. * @constructs PluginManager
  43. * A convenience shortcut to new PluginManager(...)
  44. *
  45. * @param {String} platform Platform name
  46. * @param {Object} locations - Platform files and directories
  47. * @param {IDEProject} ideProject The IDE project to add/remove plugin changes to/from
  48. * @returns new PluginManager instance
  49. */
  50. PluginManager.get = function (platform, locations, ideProject) {
  51. return new PluginManager(platform, locations, ideProject);
  52. };
  53. PluginManager.INSTALL = 'install';
  54. PluginManager.UNINSTALL = 'uninstall';
  55. module.exports = PluginManager;
  56. /**
  57. * Describes and implements common plugin installation/uninstallation routine. The flow is the following:
  58. * * Validate and set defaults for options. Note that options are empty by default. Everything
  59. * needed for platform IDE project must be passed from outside. Plugin variables (which
  60. * are the part of the options) also must be already populated with 'PACKAGE_NAME' variable.
  61. * * Collect all plugin's native and web files, get installers/uninstallers and process
  62. * all these via ActionStack.
  63. * * Save the IDE project, so the changes made by installers are persisted.
  64. * * Generate config changes munge for plugin and apply it to all required files
  65. * * Generate metadata for plugin and plugin modules and save it to 'cordova_plugins.js'
  66. *
  67. * @param {PluginInfo} plugin A PluginInfo structure representing plugin to install
  68. * @param {Object} [options={}] An installation options. It is expected but is not necessary
  69. * that options would contain 'variables' inner object with 'PACKAGE_NAME' field set by caller.
  70. *
  71. * @returns {Promise} Returns a Q promise, either resolved in case of success, rejected otherwise.
  72. */
  73. PluginManager.prototype.doOperation = function (operation, plugin, options) {
  74. if (operation !== PluginManager.INSTALL && operation !== PluginManager.UNINSTALL) { return Q.reject(new CordovaError('The parameter is incorrect. The opeation must be either "add" or "remove"')); }
  75. if (!plugin || plugin.constructor.name !== 'PluginInfo') { return Q.reject(new CordovaError('The parameter is incorrect. The first parameter should be a PluginInfo instance')); }
  76. // Set default to empty object to play safe when accesing properties
  77. options = options || {};
  78. var self = this;
  79. var actions = new ActionStack();
  80. // gather all files need to be handled during operation ...
  81. plugin.getFilesAndFrameworks(this.platform, options)
  82. .concat(plugin.getAssets(this.platform))
  83. .concat(plugin.getJsModules(this.platform))
  84. // ... put them into stack ...
  85. .forEach(function (item) {
  86. var installer = self.project.getInstaller(item.itemType);
  87. var uninstaller = self.project.getUninstaller(item.itemType);
  88. var actionArgs = [item, plugin, self.project, options];
  89. var action;
  90. if (operation === PluginManager.INSTALL) {
  91. action = actions.createAction.apply(actions, [installer, actionArgs, uninstaller, actionArgs]); /* eslint no-useless-call: 0 */
  92. } else /* op === PluginManager.UNINSTALL */{
  93. action = actions.createAction.apply(actions, [uninstaller, actionArgs, installer, actionArgs]); /* eslint no-useless-call: 0 */
  94. }
  95. actions.push(action);
  96. });
  97. // ... and run through the action stack
  98. return actions.process(this.platform)
  99. .then(function () {
  100. if (self.project.write) {
  101. self.project.write();
  102. }
  103. if (operation === PluginManager.INSTALL) {
  104. // Ignore passed `is_top_level` option since platform itself doesn't know
  105. // anything about managing dependencies - it's responsibility of caller.
  106. self.munger.add_plugin_changes(plugin, options.variables, /* is_top_level= */true, /* should_increment= */true, options.force);
  107. self.munger.platformJson.addPluginMetadata(plugin);
  108. } else {
  109. self.munger.remove_plugin_changes(plugin, /* is_top_level= */true);
  110. self.munger.platformJson.removePluginMetadata(plugin);
  111. }
  112. // Save everything (munge and plugin/modules metadata)
  113. self.munger.save_all();
  114. var metadata = self.munger.platformJson.generateMetadata();
  115. fs.writeFileSync(path.join(self.locations.www, 'cordova_plugins.js'), metadata, 'utf-8');
  116. // CB-11022 save plugin metadata to both www and platform_www if options.usePlatformWww is specified
  117. if (options.usePlatformWww) {
  118. fs.writeFileSync(path.join(self.locations.platformWww, 'cordova_plugins.js'), metadata, 'utf-8');
  119. }
  120. });
  121. };
  122. PluginManager.prototype.addPlugin = function (plugin, installOptions) {
  123. return this.doOperation(PluginManager.INSTALL, plugin, installOptions);
  124. };
  125. PluginManager.prototype.removePlugin = function (plugin, uninstallOptions) {
  126. return this.doOperation(PluginManager.UNINSTALL, plugin, uninstallOptions);
  127. };