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