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

pluginHandlers.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. 'use strict';
  18. var fs = require('fs');
  19. var path = require('path');
  20. var shell = require('shelljs');
  21. var util = require('util');
  22. var events = require('cordova-common').events;
  23. var CordovaError = require('cordova-common').CordovaError;
  24. // These frameworks are required by cordova-ios by default. We should never add/remove them.
  25. var keep_these_frameworks = [
  26. 'MobileCoreServices.framework',
  27. 'CoreGraphics.framework',
  28. 'AssetsLibrary.framework'
  29. ];
  30. var handlers = {
  31. 'source-file': {
  32. install: function (obj, plugin, project, options) {
  33. installHelper('source-file', obj, plugin.dir, project.projectDir, plugin.id, options, project);
  34. },
  35. uninstall: function (obj, plugin, project, options) {
  36. uninstallHelper('source-file', obj, project.projectDir, plugin.id, options, project);
  37. }
  38. },
  39. 'header-file': {
  40. install: function (obj, plugin, project, options) {
  41. installHelper('header-file', obj, plugin.dir, project.projectDir, plugin.id, options, project);
  42. },
  43. uninstall: function (obj, plugin, project, options) {
  44. uninstallHelper('header-file', obj, project.projectDir, plugin.id, options, project);
  45. }
  46. },
  47. 'resource-file': {
  48. install: function (obj, plugin, project, options) {
  49. var src = obj.src;
  50. var target = obj.target;
  51. var srcFile = path.resolve(plugin.dir, src);
  52. if (!target) {
  53. target = path.basename(src);
  54. }
  55. var destFile = path.resolve(project.resources_dir, target);
  56. if (!fs.existsSync(srcFile)) {
  57. throw new CordovaError('Cannot find resource file "' + srcFile + '" for plugin ' + plugin.id + ' in iOS platform');
  58. }
  59. if (fs.existsSync(destFile)) {
  60. throw new CordovaError('File already exists at destination "' + destFile + '" for resource file specified by plugin ' + plugin.id + ' in iOS platform');
  61. }
  62. project.xcode.addResourceFile(path.join('Resources', target));
  63. var link = !!(options && options.link);
  64. copyFile(plugin.dir, src, project.projectDir, destFile, link);
  65. },
  66. uninstall: function (obj, plugin, project, options) {
  67. var src = obj.src;
  68. var target = obj.target;
  69. if (!target) {
  70. target = path.basename(src);
  71. }
  72. var destFile = path.resolve(project.resources_dir, target);
  73. project.xcode.removeResourceFile(path.join('Resources', target));
  74. shell.rm('-rf', destFile);
  75. }
  76. },
  77. 'framework': { // CB-5238 custom frameworks only
  78. install: function (obj, plugin, project, options) {
  79. var src = obj.src;
  80. var custom = !!(obj.custom); // convert to boolean (if truthy/falsy)
  81. var embed = !!(obj.embed); // convert to boolean (if truthy/falsy)
  82. var link = !embed; // either link or embed can be true, but not both. the other has to be false
  83. if (!custom) {
  84. var keepFrameworks = keep_these_frameworks;
  85. if (keepFrameworks.indexOf(src) < 0) {
  86. if (obj.type === 'podspec') {
  87. // podspec handled in Api.js
  88. } else {
  89. project.frameworks[src] = project.frameworks[src] || 0;
  90. project.frameworks[src]++;
  91. let opt = { customFramework: false, embed: false, link: true, weak: obj.weak };
  92. events.emit('verbose', util.format('Adding non-custom framework to project... %s -> %s', src, JSON.stringify(opt)));
  93. project.xcode.addFramework(src, opt);
  94. events.emit('verbose', util.format('Non-custom framework added to project. %s -> %s', src, JSON.stringify(opt)));
  95. }
  96. }
  97. return;
  98. }
  99. var srcFile = path.resolve(plugin.dir, src);
  100. var targetDir = path.resolve(project.plugins_dir, plugin.id, path.basename(src));
  101. if (!fs.existsSync(srcFile)) throw new CordovaError('Cannot find framework "' + srcFile + '" for plugin ' + plugin.id + ' in iOS platform');
  102. if (fs.existsSync(targetDir)) throw new CordovaError('Framework "' + targetDir + '" for plugin ' + plugin.id + ' already exists in iOS platform');
  103. var symlink = !!(options && options.link);
  104. copyFile(plugin.dir, src, project.projectDir, targetDir, symlink); // frameworks are directories
  105. // CB-10773 translate back slashes to forward on win32
  106. var project_relative = fixPathSep(path.relative(project.projectDir, targetDir));
  107. // CB-11233 create Embed Frameworks Build Phase if does not exist
  108. var existsEmbedFrameworks = project.xcode.buildPhaseObject('PBXCopyFilesBuildPhase', 'Embed Frameworks');
  109. if (!existsEmbedFrameworks && embed) {
  110. events.emit('verbose', '"Embed Frameworks" Build Phase (Embedded Binaries) does not exist, creating it.');
  111. project.xcode.addBuildPhase([], 'PBXCopyFilesBuildPhase', 'Embed Frameworks', null, 'frameworks');
  112. }
  113. let opt = { customFramework: true, embed: embed, link: link, sign: true };
  114. events.emit('verbose', util.format('Adding custom framework to project... %s -> %s', src, JSON.stringify(opt)));
  115. project.xcode.addFramework(project_relative, opt);
  116. events.emit('verbose', util.format('Custom framework added to project. %s -> %s', src, JSON.stringify(opt)));
  117. },
  118. uninstall: function (obj, plugin, project, options) {
  119. var src = obj.src;
  120. if (!obj.custom) { // CB-9825 cocoapod integration for plugins
  121. var keepFrameworks = keep_these_frameworks;
  122. if (keepFrameworks.indexOf(src) < 0) {
  123. if (obj.type !== 'podspec') {
  124. // this should be refactored
  125. project.frameworks[src] = project.frameworks[src] || 1;
  126. project.frameworks[src]--;
  127. if (project.frameworks[src] < 1) {
  128. // Only remove non-custom framework from xcode project
  129. // if there is no references remains
  130. project.xcode.removeFramework(src);
  131. delete project.frameworks[src];
  132. }
  133. }
  134. }
  135. return;
  136. }
  137. var targetDir = fixPathSep(path.resolve(project.plugins_dir, plugin.id, path.basename(src)));
  138. var pbxFile = project.xcode.removeFramework(targetDir, { customFramework: true });
  139. if (pbxFile) {
  140. project.xcode.removeFromPbxEmbedFrameworksBuildPhase(pbxFile);
  141. }
  142. shell.rm('-rf', targetDir);
  143. }
  144. },
  145. 'lib-file': {
  146. install: function (obj, plugin, project, options) {
  147. events.emit('verbose', '<lib-file> install is not supported for iOS plugins');
  148. },
  149. uninstall: function (obj, plugin, project, options) {
  150. events.emit('verbose', '<lib-file> uninstall is not supported for iOS plugins');
  151. }
  152. },
  153. 'asset': {
  154. install: function (obj, plugin, project, options) {
  155. if (!obj.src) {
  156. throw new CordovaError(generateAttributeError('src', 'asset', plugin.id));
  157. }
  158. if (!obj.target) {
  159. throw new CordovaError(generateAttributeError('target', 'asset', plugin.id));
  160. }
  161. copyFile(plugin.dir, obj.src, project.www, obj.target);
  162. if (options && options.usePlatformWww) copyFile(plugin.dir, obj.src, project.platformWww, obj.target);
  163. },
  164. uninstall: function (obj, plugin, project, options) {
  165. var target = obj.target;
  166. if (!target) {
  167. throw new CordovaError(generateAttributeError('target', 'asset', plugin.id));
  168. }
  169. removeFile(project.www, target);
  170. removeFileF(path.resolve(project.www, 'plugins', plugin.id));
  171. if (options && options.usePlatformWww) {
  172. removeFile(project.platformWww, target);
  173. removeFileF(path.resolve(project.platformWww, 'plugins', plugin.id));
  174. }
  175. }
  176. },
  177. 'js-module': {
  178. install: function (obj, plugin, project, options) {
  179. // Copy the plugin's files into the www directory.
  180. var moduleSource = path.resolve(plugin.dir, obj.src);
  181. var moduleName = plugin.id + '.' + (obj.name || path.basename(obj.src, path.extname(obj.src)));
  182. // Read in the file, prepend the cordova.define, and write it back out.
  183. var scriptContent = fs.readFileSync(moduleSource, 'utf-8').replace(/^\ufeff/, ''); // Window BOM
  184. if (moduleSource.match(/.*\.json$/)) {
  185. scriptContent = 'module.exports = ' + scriptContent;
  186. }
  187. scriptContent = 'cordova.define("' + moduleName + '", function(require, exports, module) {\n' + scriptContent + '\n});\n';
  188. var moduleDestination = path.resolve(project.www, 'plugins', plugin.id, obj.src);
  189. shell.mkdir('-p', path.dirname(moduleDestination));
  190. fs.writeFileSync(moduleDestination, scriptContent, 'utf-8');
  191. if (options && options.usePlatformWww) {
  192. var platformWwwDestination = path.resolve(project.platformWww, 'plugins', plugin.id, obj.src);
  193. shell.mkdir('-p', path.dirname(platformWwwDestination));
  194. fs.writeFileSync(platformWwwDestination, scriptContent, 'utf-8');
  195. }
  196. },
  197. uninstall: function (obj, plugin, project, options) {
  198. var pluginRelativePath = path.join('plugins', plugin.id, obj.src);
  199. removeFileAndParents(project.www, pluginRelativePath);
  200. if (options && options.usePlatformWww) removeFileAndParents(project.platformWww, pluginRelativePath);
  201. }
  202. }
  203. };
  204. module.exports.getInstaller = function (type) {
  205. if (handlers[type] && handlers[type].install) {
  206. return handlers[type].install;
  207. }
  208. events.emit('warn', '<' + type + '> is not supported for iOS plugins');
  209. };
  210. module.exports.getUninstaller = function (type) {
  211. if (handlers[type] && handlers[type].uninstall) {
  212. return handlers[type].uninstall;
  213. }
  214. events.emit('warn', '<' + type + '> is not supported for iOS plugins');
  215. };
  216. function installHelper (type, obj, plugin_dir, project_dir, plugin_id, options, project) {
  217. var srcFile = path.resolve(plugin_dir, obj.src);
  218. var targetDir = path.resolve(project.plugins_dir, plugin_id, obj.targetDir || '');
  219. var destFile = path.join(targetDir, path.basename(obj.src));
  220. var project_ref;
  221. var link = !!(options && options.link);
  222. if (link) {
  223. var trueSrc = fs.realpathSync(srcFile);
  224. // Create a symlink in the expected place, so that uninstall can use it.
  225. if (options && options.force) {
  226. copyFile(plugin_dir, trueSrc, project_dir, destFile, link);
  227. } else {
  228. copyNewFile(plugin_dir, trueSrc, project_dir, destFile, link);
  229. }
  230. // Xcode won't save changes to a file if there is a symlink involved.
  231. // Make the Xcode reference the file directly.
  232. // Note: Can't use path.join() here since it collapses 'Plugins/..', and xcode
  233. // library special-cases Plugins/ prefix.
  234. project_ref = 'Plugins/' + fixPathSep(path.relative(fs.realpathSync(project.plugins_dir), trueSrc));
  235. } else {
  236. if (options && options.force) {
  237. copyFile(plugin_dir, srcFile, project_dir, destFile, link);
  238. } else {
  239. copyNewFile(plugin_dir, srcFile, project_dir, destFile, link);
  240. }
  241. project_ref = 'Plugins/' + fixPathSep(path.relative(project.plugins_dir, destFile));
  242. }
  243. if (type === 'header-file') {
  244. project.xcode.addHeaderFile(project_ref);
  245. } else if (obj.framework) {
  246. var opt = { weak: obj.weak };
  247. var project_relative = path.join(path.basename(project.xcode_path), project_ref);
  248. project.xcode.addFramework(project_relative, opt);
  249. project.xcode.addToLibrarySearchPaths({ path: project_ref });
  250. } else {
  251. project.xcode.addSourceFile(project_ref, obj.compilerFlags ? { compilerFlags: obj.compilerFlags } : {});
  252. }
  253. }
  254. function uninstallHelper (type, obj, project_dir, plugin_id, options, project) {
  255. var targetDir = path.resolve(project.plugins_dir, plugin_id, obj.targetDir || '');
  256. var destFile = path.join(targetDir, path.basename(obj.src));
  257. var project_ref;
  258. var link = !!(options && options.link);
  259. if (link) {
  260. var trueSrc = fs.readlinkSync(destFile);
  261. project_ref = 'Plugins/' + fixPathSep(path.relative(fs.realpathSync(project.plugins_dir), trueSrc));
  262. } else {
  263. project_ref = 'Plugins/' + fixPathSep(path.relative(project.plugins_dir, destFile));
  264. }
  265. shell.rm('-rf', targetDir);
  266. if (type === 'header-file') {
  267. project.xcode.removeHeaderFile(project_ref);
  268. } else if (obj.framework) {
  269. var project_relative = path.join(path.basename(project.xcode_path), project_ref);
  270. project.xcode.removeFramework(project_relative);
  271. project.xcode.removeFromLibrarySearchPaths({ path: project_ref });
  272. } else {
  273. project.xcode.removeSourceFile(project_ref);
  274. }
  275. }
  276. var pathSepFix = new RegExp(path.sep.replace(/\\/, '\\\\'), 'g');
  277. function fixPathSep (file) {
  278. return file.replace(pathSepFix, '/');
  279. }
  280. function copyFile (plugin_dir, src, project_dir, dest, link) {
  281. src = path.resolve(plugin_dir, src);
  282. if (!fs.existsSync(src)) throw new CordovaError('"' + src + '" not found!');
  283. // check that src path is inside plugin directory
  284. var real_path = fs.realpathSync(src);
  285. var real_plugin_path = fs.realpathSync(plugin_dir);
  286. if (real_path.indexOf(real_plugin_path) !== 0) { throw new CordovaError('File "' + src + '" is located outside the plugin directory "' + plugin_dir + '"'); }
  287. dest = path.resolve(project_dir, dest);
  288. // check that dest path is located in project directory
  289. if (dest.indexOf(project_dir) !== 0) { throw new CordovaError('Destination "' + dest + '" for source file "' + src + '" is located outside the project'); }
  290. shell.mkdir('-p', path.dirname(dest));
  291. if (link) {
  292. linkFileOrDirTree(src, dest);
  293. } else if (fs.statSync(src).isDirectory()) {
  294. // XXX shelljs decides to create a directory when -R|-r is used which sucks. http://goo.gl/nbsjq
  295. shell.cp('-Rf', path.join(src, '/*'), dest);
  296. } else {
  297. shell.cp('-f', src, dest);
  298. }
  299. }
  300. // Same as copy file but throws error if target exists
  301. function copyNewFile (plugin_dir, src, project_dir, dest, link) {
  302. var target_path = path.resolve(project_dir, dest);
  303. if (fs.existsSync(target_path)) { throw new CordovaError('"' + target_path + '" already exists!'); }
  304. copyFile(plugin_dir, src, project_dir, dest, !!link);
  305. }
  306. function linkFileOrDirTree (src, dest) {
  307. if (fs.existsSync(dest)) {
  308. shell.rm('-Rf', dest);
  309. }
  310. if (fs.statSync(src).isDirectory()) {
  311. shell.mkdir('-p', dest);
  312. fs.readdirSync(src).forEach(function (entry) {
  313. linkFileOrDirTree(path.join(src, entry), path.join(dest, entry));
  314. });
  315. } else {
  316. fs.linkSync(src, dest);
  317. }
  318. }
  319. // checks if file exists and then deletes. Error if doesn't exist
  320. function removeFile (project_dir, src) {
  321. var file = path.resolve(project_dir, src);
  322. shell.rm('-Rf', file);
  323. }
  324. // deletes file/directory without checking
  325. function removeFileF (file) {
  326. shell.rm('-Rf', file);
  327. }
  328. function removeFileAndParents (baseDir, destFile, stopper) {
  329. stopper = stopper || '.';
  330. var file = path.resolve(baseDir, destFile);
  331. if (!fs.existsSync(file)) return;
  332. removeFileF(file);
  333. // check if directory is empty
  334. var curDir = path.dirname(file);
  335. while (curDir !== path.resolve(baseDir, stopper)) {
  336. if (fs.existsSync(curDir) && fs.readdirSync(curDir).length === 0) {
  337. fs.rmdirSync(curDir);
  338. curDir = path.resolve(curDir, '..');
  339. } else {
  340. // directory not empty...do nothing
  341. break;
  342. }
  343. }
  344. }
  345. function generateAttributeError (attribute, element, id) {
  346. return 'Required attribute "' + attribute + '" not specified in <' + element + '> element from plugin: ' + id;
  347. }