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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing,
  9. * software distributed under the License is distributed on an
  10. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  11. * KIND, either express or implied. See the License for the
  12. * specific language governing permissions and limitations
  13. * under the License.
  14. *
  15. */
  16. var fs = require('fs');
  17. var path = require('path');
  18. var shell = require('shelljs');
  19. var events = require('cordova-common').events;
  20. var CordovaError = require('cordova-common').CordovaError;
  21. var handlers = {
  22. 'source-file': {
  23. install: function (obj, plugin, project, options) {
  24. if (!obj.src) throw new CordovaError(generateAttributeError('src', 'source-file', plugin.id));
  25. if (!obj.targetDir) throw new CordovaError(generateAttributeError('target-dir', 'source-file', plugin.id));
  26. var dest = getInstallDestination(obj);
  27. if (options && options.force) {
  28. copyFile(plugin.dir, obj.src, project.projectDir, dest, !!(options && options.link));
  29. } else {
  30. copyNewFile(plugin.dir, obj.src, project.projectDir, dest, !!(options && options.link));
  31. }
  32. },
  33. uninstall: function (obj, plugin, project, options) {
  34. var dest = getInstallDestination(obj);
  35. // TODO: Add Koltin extension to uninstall, since they are handled like Java files
  36. if (obj.src.endsWith('java')) {
  37. deleteJava(project.projectDir, dest);
  38. } else {
  39. // Just remove the file, not the whole parent directory
  40. removeFile(project.projectDir, dest);
  41. }
  42. }
  43. },
  44. 'lib-file': {
  45. install: function (obj, plugin, project, options) {
  46. var dest = path.join('app/libs', path.basename(obj.src));
  47. copyFile(plugin.dir, obj.src, project.projectDir, dest, !!(options && options.link));
  48. },
  49. uninstall: function (obj, plugin, project, options) {
  50. var dest = path.join('app/libs', path.basename(obj.src));
  51. removeFile(project.projectDir, dest);
  52. }
  53. },
  54. 'resource-file': {
  55. install: function (obj, plugin, project, options) {
  56. var dest = path.join('app', 'src', 'main', obj.target);
  57. copyFile(plugin.dir, obj.src, project.projectDir, dest, !!(options && options.link));
  58. },
  59. uninstall: function (obj, plugin, project, options) {
  60. var dest = path.join('app', 'src', 'main', obj.target);
  61. removeFile(project.projectDir, dest);
  62. }
  63. },
  64. 'framework': {
  65. install: function (obj, plugin, project, options) {
  66. var src = obj.src;
  67. if (!src) throw new CordovaError(generateAttributeError('src', 'framework', plugin.id));
  68. events.emit('verbose', 'Installing Android library: ' + src);
  69. var parentDir = obj.parent ? path.resolve(project.projectDir, obj.parent) : project.projectDir;
  70. var subDir;
  71. if (obj.custom) {
  72. var subRelativeDir = project.getCustomSubprojectRelativeDir(plugin.id, src);
  73. copyNewFile(plugin.dir, src, project.projectDir, subRelativeDir, !!(options && options.link));
  74. subDir = path.resolve(project.projectDir, subRelativeDir);
  75. } else {
  76. obj.type = 'sys';
  77. subDir = src;
  78. }
  79. if (obj.type === 'gradleReference') {
  80. project.addGradleReference(parentDir, subDir);
  81. } else if (obj.type === 'sys') {
  82. project.addSystemLibrary(parentDir, subDir);
  83. } else {
  84. project.addSubProject(parentDir, subDir);
  85. }
  86. },
  87. uninstall: function (obj, plugin, project, options) {
  88. var src = obj.src;
  89. if (!src) throw new CordovaError(generateAttributeError('src', 'framework', plugin.id));
  90. events.emit('verbose', 'Uninstalling Android library: ' + src);
  91. var parentDir = obj.parent ? path.resolve(project.projectDir, obj.parent) : project.projectDir;
  92. var subDir;
  93. if (obj.custom) {
  94. var subRelativeDir = project.getCustomSubprojectRelativeDir(plugin.id, src);
  95. removeFile(project.projectDir, subRelativeDir);
  96. subDir = path.resolve(project.projectDir, subRelativeDir);
  97. // If it's the last framework in the plugin, remove the parent directory.
  98. var parDir = path.dirname(subDir);
  99. if (fs.existsSync(parDir) && fs.readdirSync(parDir).length === 0) {
  100. fs.rmdirSync(parDir);
  101. }
  102. } else {
  103. obj.type = 'sys';
  104. subDir = src;
  105. }
  106. if (obj.type === 'gradleReference') {
  107. project.removeGradleReference(parentDir, subDir);
  108. } else if (obj.type === 'sys') {
  109. project.removeSystemLibrary(parentDir, subDir);
  110. } else {
  111. project.removeSubProject(parentDir, subDir);
  112. }
  113. }
  114. },
  115. asset: {
  116. install: function (obj, plugin, project, options) {
  117. if (!obj.src) {
  118. throw new CordovaError(generateAttributeError('src', 'asset', plugin.id));
  119. }
  120. if (!obj.target) {
  121. throw new CordovaError(generateAttributeError('target', 'asset', plugin.id));
  122. }
  123. copyFile(plugin.dir, obj.src, project.www, obj.target);
  124. if (options && options.usePlatformWww) {
  125. // CB-11022 copy file to both directories if usePlatformWww is specified
  126. copyFile(plugin.dir, obj.src, project.platformWww, obj.target);
  127. }
  128. },
  129. uninstall: function (obj, plugin, project, options) {
  130. var target = obj.target || obj.src;
  131. if (!target) throw new CordovaError(generateAttributeError('target', 'asset', plugin.id));
  132. removeFileF(path.resolve(project.www, target));
  133. removeFileF(path.resolve(project.www, 'plugins', plugin.id));
  134. if (options && options.usePlatformWww) {
  135. // CB-11022 remove file from both directories if usePlatformWww is specified
  136. removeFileF(path.resolve(project.platformWww, target));
  137. removeFileF(path.resolve(project.platformWww, 'plugins', plugin.id));
  138. }
  139. }
  140. },
  141. 'js-module': {
  142. install: function (obj, plugin, project, options) {
  143. // Copy the plugin's files into the www directory.
  144. var moduleSource = path.resolve(plugin.dir, obj.src);
  145. var moduleName = plugin.id + '.' + (obj.name || path.basename(obj.src, path.extname(obj.src)));
  146. // Read in the file, prepend the cordova.define, and write it back out.
  147. var scriptContent = fs.readFileSync(moduleSource, 'utf-8').replace(/^\ufeff/, ''); // Window BOM
  148. if (moduleSource.match(/.*\.json$/)) {
  149. scriptContent = 'module.exports = ' + scriptContent;
  150. }
  151. scriptContent = 'cordova.define("' + moduleName + '", function(require, exports, module) {\n' + scriptContent + '\n});\n';
  152. var wwwDest = path.resolve(project.www, 'plugins', plugin.id, obj.src);
  153. shell.mkdir('-p', path.dirname(wwwDest));
  154. fs.writeFileSync(wwwDest, scriptContent, 'utf-8');
  155. if (options && options.usePlatformWww) {
  156. // CB-11022 copy file to both directories if usePlatformWww is specified
  157. var platformWwwDest = path.resolve(project.platformWww, 'plugins', plugin.id, obj.src);
  158. shell.mkdir('-p', path.dirname(platformWwwDest));
  159. fs.writeFileSync(platformWwwDest, scriptContent, 'utf-8');
  160. }
  161. },
  162. uninstall: function (obj, plugin, project, options) {
  163. var pluginRelativePath = path.join('plugins', plugin.id, obj.src);
  164. removeFileAndParents(project.www, pluginRelativePath);
  165. if (options && options.usePlatformWww) {
  166. // CB-11022 remove file from both directories if usePlatformWww is specified
  167. removeFileAndParents(project.platformWww, pluginRelativePath);
  168. }
  169. }
  170. }
  171. };
  172. module.exports.getInstaller = function (type) {
  173. if (handlers[type] && handlers[type].install) {
  174. return handlers[type].install;
  175. }
  176. events.emit('verbose', '<' + type + '> is not supported for android plugins');
  177. };
  178. module.exports.getUninstaller = function (type) {
  179. if (handlers[type] && handlers[type].uninstall) {
  180. return handlers[type].uninstall;
  181. }
  182. events.emit('verbose', '<' + type + '> is not supported for android plugins');
  183. };
  184. function copyFile (plugin_dir, src, project_dir, dest, link) {
  185. src = path.resolve(plugin_dir, src);
  186. if (!fs.existsSync(src)) throw new CordovaError('"' + src + '" not found!');
  187. // check that src path is inside plugin directory
  188. var real_path = fs.realpathSync(src);
  189. var real_plugin_path = fs.realpathSync(plugin_dir);
  190. if (real_path.indexOf(real_plugin_path) !== 0) { throw new CordovaError('File "' + src + '" is located outside the plugin directory "' + plugin_dir + '"'); }
  191. dest = path.resolve(project_dir, dest);
  192. // check that dest path is located in project directory
  193. if (dest.indexOf(project_dir) !== 0) { throw new CordovaError('Destination "' + dest + '" for source file "' + src + '" is located outside the project'); }
  194. shell.mkdir('-p', path.dirname(dest));
  195. if (link) {
  196. symlinkFileOrDirTree(src, dest);
  197. } else if (fs.statSync(src).isDirectory()) {
  198. // XXX shelljs decides to create a directory when -R|-r is used which sucks. http://goo.gl/nbsjq
  199. shell.cp('-Rf', src + '/*', dest);
  200. } else {
  201. shell.cp('-f', src, dest);
  202. }
  203. }
  204. // Same as copy file but throws error if target exists
  205. function copyNewFile (plugin_dir, src, project_dir, dest, link) {
  206. var target_path = path.resolve(project_dir, dest);
  207. if (fs.existsSync(target_path)) { throw new CordovaError('"' + target_path + '" already exists!'); }
  208. copyFile(plugin_dir, src, project_dir, dest, !!link);
  209. }
  210. function symlinkFileOrDirTree (src, dest) {
  211. if (fs.existsSync(dest)) {
  212. shell.rm('-Rf', dest);
  213. }
  214. if (fs.statSync(src).isDirectory()) {
  215. shell.mkdir('-p', dest);
  216. fs.readdirSync(src).forEach(function (entry) {
  217. symlinkFileOrDirTree(path.join(src, entry), path.join(dest, entry));
  218. });
  219. } else {
  220. fs.symlinkSync(path.relative(fs.realpathSync(path.dirname(dest)), src), dest);
  221. }
  222. }
  223. // checks if file exists and then deletes. Error if doesn't exist
  224. function removeFile (project_dir, src) {
  225. var file = path.resolve(project_dir, src);
  226. shell.rm('-Rf', file);
  227. }
  228. // deletes file/directory without checking
  229. function removeFileF (file) {
  230. shell.rm('-Rf', file);
  231. }
  232. // Sometimes we want to remove some java, and prune any unnecessary empty directories
  233. function deleteJava (project_dir, destFile) {
  234. removeFileAndParents(project_dir, destFile, 'src');
  235. }
  236. function removeFileAndParents (baseDir, destFile, stopper) {
  237. stopper = stopper || '.';
  238. var file = path.resolve(baseDir, destFile);
  239. if (!fs.existsSync(file)) return;
  240. removeFileF(file);
  241. // check if directory is empty
  242. var curDir = path.dirname(file);
  243. while (curDir !== path.resolve(baseDir, stopper)) {
  244. if (fs.existsSync(curDir) && fs.readdirSync(curDir).length === 0) {
  245. fs.rmdirSync(curDir);
  246. curDir = path.resolve(curDir, '..');
  247. } else {
  248. // directory not empty...do nothing
  249. break;
  250. }
  251. }
  252. }
  253. function generateAttributeError (attribute, element, id) {
  254. return 'Required attribute "' + attribute + '" not specified in <' + element + '> element from plugin: ' + id;
  255. }
  256. function getInstallDestination (obj) {
  257. var APP_MAIN_PREFIX = 'app/src/main';
  258. var PATH_SEPARATOR = '/';
  259. var PATH_SEP_MATCH = '\\' + PATH_SEPARATOR;
  260. var PATH_SEP_OR_EOL_MATCH = '(\\' + PATH_SEPARATOR + '|$)';
  261. var appReg = new RegExp('^app' + PATH_SEP_OR_EOL_MATCH);
  262. var libsReg = new RegExp('^libs' + PATH_SEP_OR_EOL_MATCH);
  263. var srcReg = new RegExp('^src' + PATH_SEP_OR_EOL_MATCH);
  264. var srcMainReg = new RegExp('^src' + PATH_SEP_MATCH + 'main' + PATH_SEP_OR_EOL_MATCH);
  265. if (appReg.test(obj.targetDir)) {
  266. // If any source file is using the new app directory structure,
  267. // don't penalize it
  268. return path.join(obj.targetDir, path.basename(obj.src));
  269. } else {
  270. // Plugin using deprecated target directory structure (GH-580)
  271. if (obj.src.endsWith('.java')) {
  272. return path.join(APP_MAIN_PREFIX, 'java', obj.targetDir.replace(srcReg, ''),
  273. path.basename(obj.src));
  274. } else if (obj.src.endsWith('.aidl')) {
  275. return path.join(APP_MAIN_PREFIX, 'aidl', obj.targetDir.replace(srcReg, ''),
  276. path.basename(obj.src));
  277. } else if (libsReg.test(obj.targetDir)) {
  278. if (obj.src.endsWith('.so')) {
  279. return path.join(APP_MAIN_PREFIX, 'jniLibs', obj.targetDir.replace(libsReg, ''),
  280. path.basename(obj.src));
  281. } else {
  282. return path.join('app', obj.targetDir, path.basename(obj.src));
  283. }
  284. } else if (srcMainReg.test(obj.targetDir)) {
  285. return path.join('app', obj.targetDir, path.basename(obj.src));
  286. }
  287. // For all other source files not using the new app directory structure,
  288. // add 'app/src/main' to the targetDir
  289. return path.join(APP_MAIN_PREFIX, obj.targetDir, path.basename(obj.src));
  290. }
  291. }