CB-11022 Duplicate www files to both destinations on plugin operations

This closes #289
This commit is contained in:
Vladimir Kotikov 2016-04-07 13:24:41 +03:00
parent d28ca67edb
commit 9b8558dbde
3 changed files with 163 additions and 33 deletions

View File

@ -241,11 +241,14 @@ Api.prototype.addPlugin = function (plugin, installOptions) {
require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles(); require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles();
} }
var targetDir = installOptions.usePlatformWww ? var targetDirs = [self.locations.www];
self.locations.platformWww : // CB-11022 if usePlatformWww is specified we need to mirror modules metadata to both directories
self.locations.www; if (installOptions.usePlatformWww) targetDirs.push(self.locations.platformWww);
self._addModulesInfo(plugin, targetDirs);
self._addModulesInfo(plugin, targetDir); // CB-11022 Indicate to caller that we have copied js-files and assets
// to both 'www' and 'platform_www' so prepare is not needed
return true;
}); });
}; };
@ -299,11 +302,14 @@ Api.prototype.removePlugin = function (plugin, uninstallOptions) {
require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles(); require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles();
} }
var targetDir = uninstallOptions.usePlatformWww ? var targetDirs = [self.locations.www];
self.locations.platformWww : // CB-11022 if usePlatformWww is specified we need to mirror modules metadata to both directories
self.locations.www; if (uninstallOptions.usePlatformWww) targetDirs.push(self.locations.platformWww);
self._removeModulesInfo(plugin, targetDirs);
self._removeModulesInfo(plugin, targetDir); // CB-11022 Indicate to caller that we have copied js-files and assets
// to both 'www' and 'platform_www' so prepare is not needed
return true;
}); });
}; };
@ -425,10 +431,10 @@ module.exports = Api;
* *
* @param {PluginInfo} plugin PluginInfo instance for plugin, which modules * @param {PluginInfo} plugin PluginInfo instance for plugin, which modules
* needs to be added. * needs to be added.
* @param {String} targetDir The directory, where updated cordova_plugins.js * @param {String[]} targetDirs The directories, where updated cordova_plugins.js
* should be written to. * should be written to.
*/ */
Api.prototype._addModulesInfo = function(plugin, targetDir) { Api.prototype._addModulesInfo = function(plugin, targetDirs) {
var installedModules = this._platformJson.root.modules || []; var installedModules = this._platformJson.root.modules || [];
var installedPaths = installedModules.map(function (installedModule) { var installedPaths = installedModules.map(function (installedModule) {
@ -463,7 +469,7 @@ Api.prototype._addModulesInfo = function(plugin, targetDir) {
} }
this._platformJson.root.plugin_metadata[plugin.id] = plugin.version; this._platformJson.root.plugin_metadata[plugin.id] = plugin.version;
this._writePluginModules(targetDir); this._writePluginModules(targetDirs);
this._platformJson.save(); this._platformJson.save();
}; };
@ -473,10 +479,10 @@ Api.prototype._addModulesInfo = function(plugin, targetDir) {
* *
* @param {PluginInfo} plugin PluginInfo instance for plugin, which modules * @param {PluginInfo} plugin PluginInfo instance for plugin, which modules
* needs to be removed. * needs to be removed.
* @param {String} targetDir The directory, where updated cordova_plugins.js * @param {String[]} targetDirs The directories, where updated cordova_plugins.js
* should be written to. * should be written to.
*/ */
Api.prototype._removeModulesInfo = function(plugin, targetDir) { Api.prototype._removeModulesInfo = function(plugin, targetDirs) {
var installedModules = this._platformJson.root.modules || []; var installedModules = this._platformJson.root.modules || [];
var modulesToRemove = plugin.getJsModules(this.platform) var modulesToRemove = plugin.getJsModules(this.platform)
.map(function (jsModule) { .map(function (jsModule) {
@ -493,19 +499,18 @@ Api.prototype._removeModulesInfo = function(plugin, targetDir) {
delete this._platformJson.root.plugin_metadata[plugin.id]; delete this._platformJson.root.plugin_metadata[plugin.id];
} }
this._writePluginModules(targetDir); this._writePluginModules(targetDirs);
this._platformJson.save(); this._platformJson.save();
}; };
/** /**
* Fetches all installed modules, generates cordova_plugins contents and writes * Fetches all installed modules, generates cordova_plugins contents and writes
* it to file. * it to cordova_plugins.js file at specified directories.
* *
* @param {String} targetDir Directory, where write cordova_plugins.js to. * @param {String[]} targetDirs Directories, where write cordova_plugins.js to.
* Ususally it is either <platform>/www or <platform>/platform_www * Ususally it is either <platform>/www or <platform>/platform_www or both.
* directories.
*/ */
Api.prototype._writePluginModules = function (targetDir) { Api.prototype._writePluginModules = function (targetDirs) {
// Write out moduleObjects as JSON wrapped in a cordova module to cordova_plugins.js // Write out moduleObjects as JSON wrapped in a cordova module to cordova_plugins.js
var final_contents = 'cordova.define(\'cordova/plugin_list\', function(require, exports, module) {\n'; var final_contents = 'cordova.define(\'cordova/plugin_list\', function(require, exports, module) {\n';
final_contents += 'module.exports = ' + JSON.stringify(this._platformJson.root.modules, null, ' ') + ';\n'; final_contents += 'module.exports = ' + JSON.stringify(this._platformJson.root.modules, null, ' ') + ';\n';
@ -516,6 +521,8 @@ Api.prototype._writePluginModules = function (targetDir) {
final_contents += '// BOTTOM OF METADATA\n'; final_contents += '// BOTTOM OF METADATA\n';
final_contents += '});'; // Close cordova.define. final_contents += '});'; // Close cordova.define.
targetDirs.forEach(function (targetDir) {
shell.mkdir('-p', targetDir); shell.mkdir('-p', targetDir);
fs.writeFileSync(path.join(targetDir, 'cordova_plugins.js'), final_contents, 'utf-8'); fs.writeFileSync(path.join(targetDir, 'cordova_plugins.js'), final_contents, 'utf-8');
});
}; };

View File

@ -128,17 +128,24 @@ var handlers = {
throw new CordovaError('<asset> tag without required "target" attribute'); throw new CordovaError('<asset> tag without required "target" attribute');
} }
var www = options.usePlatformWww ? project.platformWww : project.www; copyFile(plugin.dir, obj.src, project.www, obj.target);
copyFile(plugin.dir, obj.src, www, obj.target); if (options && options.usePlatformWww) {
// CB-11022 copy file to both directories if usePlatformWww is specified
copyFile(plugin.dir, obj.src, project.platformWww, obj.target);
}
}, },
uninstall:function(obj, plugin, project, options) { uninstall:function(obj, plugin, project, options) {
var target = obj.target || obj.src; var target = obj.target || obj.src;
if (!target) throw new CordovaError('<asset> tag without required "target" attribute'); if (!target) throw new CordovaError('<asset> tag without required "target" attribute');
var www = options.usePlatformWww ? project.platformWww : project.www; removeFileF(path.resolve(project.www, target));
removeFile(www, target); removeFileF(path.resolve(project.www, 'plugins', plugin.id));
removeFileF(path.resolve(www, 'plugins', plugin.id)); if (options && options.usePlatformWww) {
// CB-11022 remove file from both directories if usePlatformWww is specified
removeFileF(path.resolve(project.platformWww, target));
removeFileF(path.resolve(project.platformWww, 'plugins', plugin.id));
}
} }
}, },
'js-module': { 'js-module': {
@ -154,15 +161,24 @@ var handlers = {
} }
scriptContent = 'cordova.define("' + moduleName + '", function(require, exports, module) {\n' + scriptContent + '\n});\n'; scriptContent = 'cordova.define("' + moduleName + '", function(require, exports, module) {\n' + scriptContent + '\n});\n';
var www = options.usePlatformWww ? project.platformWww : project.www; var wwwDest = path.resolve(project.www, 'plugins', plugin.id, obj.src);
var moduleDestination = path.resolve(www, 'plugins', plugin.id, obj.src); shell.mkdir('-p', path.dirname(wwwDest));
shell.mkdir('-p', path.dirname(moduleDestination)); fs.writeFileSync(wwwDest, scriptContent, 'utf-8');
fs.writeFileSync(moduleDestination, scriptContent, 'utf-8');
if (options && options.usePlatformWww) {
// CB-11022 copy file to both directories if usePlatformWww is specified
var platformWwwDest = path.resolve(project.platformWww, 'plugins', plugin.id, obj.src);
shell.mkdir('-p', path.dirname(platformWwwDest));
fs.writeFileSync(platformWwwDest, scriptContent, 'utf-8');
}
}, },
uninstall: function (obj, plugin, project, options) { uninstall: function (obj, plugin, project, options) {
var pluginRelativePath = path.join('plugins', plugin.id, obj.src); var pluginRelativePath = path.join('plugins', plugin.id, obj.src);
var www = options.usePlatformWww ? project.platformWww : project.www; removeFileAndParents(project.www, pluginRelativePath);
removeFileAndParents(www, pluginRelativePath); if (options && options.usePlatformWww) {
// CB-11022 remove file from both directories if usePlatformWww is specified
removeFileAndParents(project.platformWww, pluginRelativePath);
}
} }
} }
}; };

View File

@ -50,6 +50,7 @@ describe('android project handler', function() {
beforeEach(function() { beforeEach(function() {
shell.mkdir('-p', temp); shell.mkdir('-p', temp);
dummyProject = AndroidProject.getProjectFile(temp); dummyProject = AndroidProject.getProjectFile(temp);
copyFileSpy.reset();
common.__set__('copyFile', copyFileSpy); common.__set__('copyFile', copyFileSpy);
}); });
@ -161,6 +162,51 @@ describe('android project handler', function() {
expect(dummyProject.addGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString); expect(dummyProject.addGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString);
}); });
}); });
describe('of <js-module> elements', function() {
var jsModule = {src: 'www/dummyPlugin.js'};
var wwwDest, platformWwwDest;
beforeEach(function () {
spyOn(fs, 'writeFileSync');
wwwDest = path.resolve(dummyProject.www, 'plugins', dummyPluginInfo.id, jsModule.src);
platformWwwDest = path.resolve(dummyProject.platformWww, 'plugins', dummyPluginInfo.id, jsModule.src);
});
it('should put module to both www and platform_www when options.usePlatformWww flag is specified', function () {
android['js-module'].install(jsModule, dummyPluginInfo, dummyProject, {usePlatformWww: true});
expect(fs.writeFileSync).toHaveBeenCalledWith(wwwDest, jasmine.any(String), 'utf-8');
expect(fs.writeFileSync).toHaveBeenCalledWith(platformWwwDest, jasmine.any(String), 'utf-8');
});
it('should put module to www only when options.usePlatformWww flag is not specified', function () {
android['js-module'].install(jsModule, dummyPluginInfo, dummyProject);
expect(fs.writeFileSync).toHaveBeenCalledWith(wwwDest, jasmine.any(String), 'utf-8');
expect(fs.writeFileSync).not.toHaveBeenCalledWith(platformWwwDest, jasmine.any(String), 'utf-8');
});
});
describe('of <asset> elements', function() {
var asset = {src: 'www/dummyPlugin.js', target: 'foo/dummy.js'};
var wwwDest, platformWwwDest;
beforeEach(function () {
wwwDest = path.resolve(dummyProject.www, asset.target);
platformWwwDest = path.resolve(dummyProject.platformWww, asset.target);
});
it('should put asset to both www and platform_www when options.usePlatformWww flag is specified', function () {
android.asset.install(asset, dummyPluginInfo, dummyProject, {usePlatformWww: true});
expect(copyFileSpy).toHaveBeenCalledWith(dummyPluginInfo.dir, asset.src, dummyProject.www, asset.target);
expect(copyFileSpy).toHaveBeenCalledWith(dummyPluginInfo.dir, asset.src, dummyProject.platformWww, asset.target);
});
it('should put asset to www only when options.usePlatformWww flag is not specified', function () {
android.asset.install(asset, dummyPluginInfo, dummyProject);
expect(copyFileSpy).toHaveBeenCalledWith(dummyPluginInfo.dir, asset.src, dummyProject.www, asset.target);
expect(copyFileSpy).not.toHaveBeenCalledWith(dummyPluginInfo.dir, asset.src, dummyProject.platformWww, asset.target);
});
});
}); });
describe('uninstallation', function() { describe('uninstallation', function() {
@ -254,5 +300,66 @@ describe('android project handler', function() {
expect(dummyProject.removeGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString); expect(dummyProject.removeGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString);
}); });
}); });
describe('of <js-module> elements', function() {
var jsModule = {src: 'www/dummyPlugin.js'};
var wwwDest, platformWwwDest;
beforeEach(function () {
wwwDest = path.resolve(dummyProject.www, 'plugins', dummyPluginInfo.id, jsModule.src);
platformWwwDest = path.resolve(dummyProject.platformWww, 'plugins', dummyPluginInfo.id, jsModule.src);
spyOn(shell, 'rm');
var existsSyncOrig = fs.existsSync;
spyOn(fs, 'existsSync').andCallFake(function (file) {
if ([wwwDest, platformWwwDest].indexOf(file) >= 0 ) return true;
return existsSyncOrig.call(fs, file);
});
});
it('should put module to both www and platform_www when options.usePlatformWww flag is specified', function () {
android['js-module'].uninstall(jsModule, dummyPluginInfo, dummyProject, {usePlatformWww: true});
expect(shell.rm).toHaveBeenCalledWith('-Rf', wwwDest);
expect(shell.rm).toHaveBeenCalledWith('-Rf', platformWwwDest);
});
it('should put module to www only when options.usePlatformWww flag is not specified', function () {
android['js-module'].uninstall(jsModule, dummyPluginInfo, dummyProject);
expect(shell.rm).toHaveBeenCalledWith('-Rf', wwwDest);
expect(shell.rm).not.toHaveBeenCalledWith('-Rf', platformWwwDest);
});
});
describe('of <asset> elements', function() {
var asset = {src: 'www/dummyPlugin.js', target: 'foo/dummy.js'};
var wwwDest, platformWwwDest;
beforeEach(function () {
wwwDest = path.resolve(dummyProject.www, asset.target);
platformWwwDest = path.resolve(dummyProject.platformWww, asset.target);
spyOn(shell, 'rm');
var existsSyncOrig = fs.existsSync;
spyOn(fs, 'existsSync').andCallFake(function (file) {
if ([wwwDest, platformWwwDest].indexOf(file) >= 0 ) return true;
return existsSyncOrig.call(fs, file);
});
});
it('should put module to both www and platform_www when options.usePlatformWww flag is specified', function () {
android.asset.uninstall(asset, dummyPluginInfo, dummyProject, {usePlatformWww: true});
expect(shell.rm).toHaveBeenCalledWith(jasmine.any(String), wwwDest);
expect(shell.rm).toHaveBeenCalledWith(jasmine.any(String), platformWwwDest);
});
it('should put module to www only when options.usePlatformWww flag is not specified', function () {
android.asset.uninstall(asset, dummyPluginInfo, dummyProject);
expect(shell.rm).toHaveBeenCalledWith(jasmine.any(String), wwwDest);
expect(shell.rm).not.toHaveBeenCalledWith(jasmine.any(String), platformWwwDest);
});
});
}); });
}); });