pluginHandlers.js 14 KB

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