cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [16/25] cordova-browser git commit: added parser, mostly handling plugin add/remove now
Date Thu, 25 May 2017 20:38:36 GMT
added parser, mostly handling plugin add/remove now


Project: http://git-wip-us.apache.org/repos/asf/cordova-browser/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-browser/commit/d7cdec35
Tree: http://git-wip-us.apache.org/repos/asf/cordova-browser/tree/d7cdec35
Diff: http://git-wip-us.apache.org/repos/asf/cordova-browser/diff/d7cdec35

Branch: refs/heads/master
Commit: d7cdec35482fac862a8af3a266523d90ebc98696
Parents: c846b47
Author: Jesse MacFadyen <purplecabbage@gmail.com>
Authored: Thu May 4 17:17:55 2017 -0700
Committer: Jesse MacFadyen <purplecabbage@gmail.com>
Committed: Thu May 4 17:17:55 2017 -0700

----------------------------------------------------------------------
 bin/template/cordova/Api.js             | 274 ++++++++++++++++++++++++++-
 bin/template/cordova/browser_handler.js | 106 +++++++++++
 bin/template/cordova/browser_parser.js  |  20 +-
 3 files changed, 386 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-browser/blob/d7cdec35/bin/template/cordova/Api.js
----------------------------------------------------------------------
diff --git a/bin/template/cordova/Api.js b/bin/template/cordova/Api.js
index 1ac420e..ac1060e 100644
--- a/bin/template/cordova/Api.js
+++ b/bin/template/cordova/Api.js
@@ -11,9 +11,18 @@ var fs = require('fs');
 
 var CordovaLogger = require('cordova-common').CordovaLogger;
 var ConfigParser = require('cordova-common').ConfigParser;
+var ActionStack = require('cordova-common').ActionStack;
+var PluginInfo = require('cordova-common').PluginInfo;
 var selfEvents = require('cordova-common').events;
 var xmlHelpers = require('cordova-common').xmlHelpers;
 
+
+
+var PlatformJson = require('cordova-common').PlatformJson;
+
+var PlatformMunger = require('cordova-common').ConfigChanges.PlatformMunger;
+var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
+
 var BrowserParser = require('./browser_parser');
 var PLATFORM_NAME = 'browser';
 
@@ -37,6 +46,7 @@ function Api(platform, platformRootDir, events) {
     this.events = setupEvents(events);
 
     this.parser = new BrowserParser(this.root);
+    this._handler = require('./browser_handler');
 
     this.locations = {
         platformRootDir: platformRootDir,
@@ -51,6 +61,10 @@ function Api(platform, platformRootDir, events) {
         cordovaJs: 'bin/templates/project/assets/www/cordova.js',
         cordovaJsSrc: 'cordova-js-src'
     };
+
+    this._platformJson = PlatformJson.load(this.root, platform);
+    this._pluginInfoProvider = new PluginInfoProvider();
+    this._munger = new PlatformMunger(platform, this.root, this._platformJson, this._pluginInfoProvider);
 }
 
 Api.createPlatform = function (dest, config, options, events) {
@@ -152,16 +166,270 @@ Api.prototype.prepare = function (cordovaProject,options) {
     return this.parser.update_project(this.config, options);
 };
 
-Api.prototype.addPlugin = function (plugin, installOptions) {
-    //console.log("NotImplemented :: browser-platform:Api:addPlugin ",plugin, installOptions);
-    return Promise.resolve();
+Api.prototype.addPlugin = function (pluginInfo, installOptions) {
+
+    // console.log(new Error().stack);
+
+
+    if (!pluginInfo) {
+        return Promise.reject('The parameter is incorrect. The first parameter ' +
+            'should be valid PluginInfo instance');
+    }
+
+    installOptions = installOptions || {};
+    installOptions.variables = installOptions.variables || {};
+    // CB-10108 platformVersion option is required for proper plugin installation
+    installOptions.platformVersion = installOptions.platformVersion ||
+        this.getPlatformInfo().version;
+
+    var self = this;
+    var actions = new ActionStack();
+    var projectFile = this._handler.parseProjectFile && this._handler.parseProjectFile(this.root);
+
+    // gather all files needs to be handled during install
+    pluginInfo.getFilesAndFrameworks(this.platform)
+        .concat(pluginInfo.getAssets(this.platform))
+        .concat(pluginInfo.getJsModules(this.platform))
+    .forEach(function(item) {
+        actions.push(actions.createAction(
+            self._getInstaller(item.itemType),
+            [item, pluginInfo.dir, pluginInfo.id, installOptions, projectFile],
+            self._getUninstaller(item.itemType),
+            [item, pluginInfo.dir, pluginInfo.id, installOptions, projectFile]));
+    });
+
+    // run through the action stack
+    return actions.process(this.platform, this.root)
+    .then(function () {
+        if (projectFile) {
+            projectFile.write();
+        }
+
+        // Add PACKAGE_NAME variable into vars
+        if (!installOptions.variables.PACKAGE_NAME) {
+            installOptions.variables.PACKAGE_NAME = self._handler.package_name(self.root);
+        }
+
+        self._munger
+            // Ignore passed `is_top_level` option since platform itself doesn't know
+            // anything about managing dependencies - it's responsibility of caller.
+            .add_plugin_changes(pluginInfo, installOptions.variables, /*is_top_level=*/true,
/*should_increment=*/true)
+            .save_all();
+
+        var targetDir = installOptions.usePlatformWww ?
+            self.getPlatformInfo().locations.platformWww :
+            self.getPlatformInfo().locations.www;
+
+        self._addModulesInfo(pluginInfo, targetDir);
+    });
 };
 
 Api.prototype.removePlugin = function (plugin, uninstallOptions) {
     //console.log("NotImplemented :: browser-platform:Api:removePlugin ",plugin, uninstallOptions);
+
+    uninstallOptions = uninstallOptions || {};
+    // CB-10108 platformVersion option is required for proper plugin installation
+    uninstallOptions.platformVersion = uninstallOptions.platformVersion ||
+        this.getPlatformInfo().version;
+
+    var self = this;
+    var actions = new ActionStack();
+    var projectFile = this._handler.parseProjectFile && this._handler.parseProjectFile(this.root);
+
+    // queue up plugin files
+    plugin.getFilesAndFrameworks(this.platform)
+        .concat(plugin.getAssets(this.platform))
+        .concat(plugin.getJsModules(this.platform))
+    .forEach(function(item) {
+        actions.push(actions.createAction(
+            self._getUninstaller(item.itemType), [item, plugin.dir, plugin.id, uninstallOptions,
projectFile],
+            self._getInstaller(item.itemType), [item, plugin.dir, plugin.id, uninstallOptions,
projectFile]));
+    });
+
+    // run through the action stack
+    return actions.process(this.platform, this.root)
+    .then(function() {
+        if (projectFile) {
+            projectFile.write();
+        }
+
+        self._munger
+            // Ignore passed `is_top_level` option since platform itself doesn't know
+            // anything about managing dependencies - it's responsibility of caller.
+            .remove_plugin_changes(plugin, /*is_top_level=*/true)
+            .save_all();
+
+        var targetDir = uninstallOptions.usePlatformWww ?
+            self.getPlatformInfo().locations.platformWww :
+            self.getPlatformInfo().locations.www;
+
+        self._removeModulesInfo(plugin, targetDir);
+        // Remove stale plugin directory
+        // TODO: this should be done by plugin files uninstaller
+        shell.rm('-rf', path.resolve(self.root, 'Plugins', plugin.id));
+    });
+
     return Promise.resolve();
 };
 
+Api.prototype._getInstaller = function(type) {
+    var self = this;
+    return function (item, plugin_dir, plugin_id, options, project) {
+        var installer = self._handler[type];
+
+        if(!installer) {
+            console.log("unrecognized type " + type)
+            return;
+        }
+        else {
+            var wwwDest = options.usePlatformWww ?
+                    self.getPlatformInfo().locations.platformWww :
+                    self._handler.www_dir(self.root);
+
+            if(type === 'asset') {
+                installer.install(item, plugin_dir, wwwDest);
+            }
+            else if(type === 'js-module') {
+                installer.install(item, plugin_dir, plugin_id, wwwDest);
+            }
+            else {
+                installer.install(item, plugin_dir, self.root, plugin_id, options, project);
+            }
+
+// var installerArgs = type === 'asset' ? [wwwDest] :
+//     type === 'js-module' ? [plugin_id, wwwDest]:
+//     [self.root, plugin_id, options, project];
+// installer.install.apply(null, [item, plugin_dir].concat(installerArgs));
+        }
+    };
+};
+
+Api.prototype._getUninstaller = function(type) {
+    var self = this;
+    return function (item, plugin_dir, plugin_id, options, project) {
+        var installer = self._handler[type];
+
+        if(!installer) {
+            console.log("browser plugin uninstall: unrecognized type, skipping : " + type);
+            return;
+        }
+        else {
+            var wwwDest = options.usePlatformWww ?
+                self.getPlatformInfo().locations.platformWww :
+                self._handler.www_dir(self.root);
+
+            if(['asset','js-module'].indexOf(type) > -1) {
+                return installer.uninstall(item, wwwDest, plugin_id);
+            }
+            else {
+                return installer.uninstall(item, self.root, plugin_id, options, project);
+            }
+
+        }
+    };
+};
+
+/**
+ * Removes the specified modules from list of installed modules and updates
+ *   platform_json and cordova_plugins.js on disk.
+ *
+ * @param   {PluginInfo}  plugin  PluginInfo instance for plugin, which modules
+ *   needs to be added.
+ * @param   {String}  targetDir  The directory, where updated cordova_plugins.js
+ *   should be written to.
+ */
+Api.prototype._addModulesInfo = function(plugin, targetDir) {
+    var installedModules = this._platformJson.root.modules || [];
+
+    var installedPaths = installedModules.map(function (installedModule) {
+        return installedModule.file;
+    });
+
+    var modulesToInstall = plugin.getJsModules(this.platform)
+    .filter(function (moduleToInstall) {
+        return installedPaths.indexOf(moduleToInstall.file) === -1;
+    }).map(function (moduleToInstall) {
+        var moduleName = plugin.id + '.' + ( moduleToInstall.name || moduleToInstall.src.match(/([^\/]+)\.js/)[1]
);
+        var obj = {
+            file: ['plugins', plugin.id, moduleToInstall.src].join('/'),
+            id: moduleName,
+            pluginId: plugin.id
+        };
+        if (moduleToInstall.clobbers.length > 0) {
+            obj.clobbers = moduleToInstall.clobbers.map(function(o) { return o.target; });
+        }
+        if (moduleToInstall.merges.length > 0) {
+            obj.merges = moduleToInstall.merges.map(function(o) { return o.target; });
+        }
+        if (moduleToInstall.runs) {
+            obj.runs = true;
+        }
+
+        return obj;
+    });
+
+    this._platformJson.root.modules = installedModules.concat(modulesToInstall);
+    if (!this._platformJson.root.plugin_metadata) {
+        this._platformJson.root.plugin_metadata = {};
+    }
+    this._platformJson.root.plugin_metadata[plugin.id] = plugin.version;
+
+    this._writePluginModules(targetDir);
+    this._platformJson.save();
+};
+/**
+ * Fetches all installed modules, generates cordova_plugins contents and writes
+ *   it to file.
+ *
+ * @param   {String}  targetDir  Directory, where write cordova_plugins.js to.
+ *   Ususally it is either <platform>/www or <platform>/platform_www
+ *   directories.
+ */
+Api.prototype._writePluginModules = function (targetDir) {
+    // 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';
+    final_contents += 'module.exports = ' + JSON.stringify(this._platformJson.root.modules,
null, '    ') + ';\n';
+    final_contents += 'module.exports.metadata = \n';
+    final_contents += '// TOP OF METADATA\n';
+    final_contents += JSON.stringify(this._platformJson.root.plugin_metadata || {}, null,
'    ') + '\n';
+    final_contents += '// BOTTOM OF METADATA\n';
+    final_contents += '});'; // Close cordova.define.
+
+    shell.mkdir('-p', targetDir);
+    fs.writeFileSync(path.join(targetDir, 'cordova_plugins.js'), final_contents, 'utf-8');
+};
+
+
+/**
+ * Removes the specified modules from list of installed modules and updates
+ *   platform_json and cordova_plugins.js on disk.
+ *
+ * @param   {PluginInfo}  plugin  PluginInfo instance for plugin, which modules
+ *   needs to be removed.
+ * @param   {String}  targetDir  The directory, where updated cordova_plugins.js
+ *   should be written to.
+ */
+Api.prototype._removeModulesInfo = function(plugin, targetDir) {
+    var installedModules = this._platformJson.root.modules || [];
+    var modulesToRemove = plugin.getJsModules(this.platform)
+    .map(function (jsModule) {
+        return  ['plugins', plugin.id, jsModule.src].join('/');
+    });
+
+    var updatedModules = installedModules
+    .filter(function (installedModule) {
+        return (modulesToRemove.indexOf(installedModule.file) === -1);
+    });
+
+    this._platformJson.root.modules = updatedModules;
+    if (this._platformJson.root.plugin_metadata) {
+        delete this._platformJson.root.plugin_metadata[plugin.id];
+    }
+
+    this._writePluginModules(targetDir);
+    this._platformJson.save();
+};
+
 Api.prototype.build = function (buildOptions) {
     var self = this;
     return require('./lib/check_reqs').run()

http://git-wip-us.apache.org/repos/asf/cordova-browser/blob/d7cdec35/bin/template/cordova/browser_handler.js
----------------------------------------------------------------------
diff --git a/bin/template/cordova/browser_handler.js b/bin/template/cordova/browser_handler.js
new file mode 100644
index 0000000..101fc13
--- /dev/null
+++ b/bin/template/cordova/browser_handler.js
@@ -0,0 +1,106 @@
+/**
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+*/
+
+
+var path = require('path');
+var fs = require('fs');
+var shell = require('shelljs');
+
+
+var events = require('cordova-common').events;
+
+module.exports = {
+    www_dir: function(project_dir) {
+        return path.join(project_dir, 'www');
+    },
+    package_name:function(project_dir) {
+        //return common.package_name(project_dir, this.www_dir(project_dir));
+        console.log('package_name called with ' + project_dir);
+        return 'bob';
+    },
+    'js-module': {
+        install: function (jsModule, plugin_dir, plugin_id, www_dir) {
+            // Copy the plugin's files into the www directory.
+            var moduleSource = path.resolve(plugin_dir, jsModule.src);
+            // Get module name based on existing 'name' attribute or filename
+            // Must use path.extname/path.basename instead of path.parse due to CB-9981
+            var moduleName = plugin_id + '.' + (jsModule.name || path.basename(jsModule.src,
path.extname (jsModule.src)));
+
+            // Read in the file, prepend the cordova.define, and write it back out.
+            var scriptContent = fs.readFileSync(moduleSource, 'utf-8').replace(/^\ufeff/,
''); // Window BOM
+            if (moduleSource.match(/.*\.json$/)) {
+                scriptContent = 'module.exports = ' + scriptContent;
+            }
+            scriptContent = 'cordova.define("' + moduleName + '", function(require, exports,
module) { ' + scriptContent + '\n});\n';
+
+            var moduleDestination = path.resolve(www_dir, 'plugins', plugin_id, jsModule.src);
+            shell.mkdir('-p', path.dirname(moduleDestination));
+            fs.writeFileSync(moduleDestination, scriptContent, 'utf-8');
+        },
+        uninstall: function (jsModule, www_dir, plugin_id) {
+            var pluginRelativePath = path.join('plugins', plugin_id, jsModule.src);
+            // common.removeFileAndParents(www_dir, pluginRelativePath);
+            console.log("js-module uninstall called : " + pluginRelativePath);
+        }
+    },
+    'source-file':{
+        install:function(obj, plugin_dir, project_dir, plugin_id, options) {
+            // var dest = path.join(obj.targetDir, path.basename(obj.src));
+            // common.copyFile(plugin_dir, obj.src, project_dir, dest);
+            console.log("install called");
+        },
+        uninstall:function(obj, project_dir, plugin_id, options) {
+            // var dest = path.join(obj.targetDir, path.basename(obj.src));
+            // common.removeFile(project_dir, dest);
+            console.log("uninstall called");
+        }
+    },
+    'header-file': {
+        install:function(obj, plugin_dir, project_dir, plugin_id, options) {
+            events.emit('verbose', 'header-fileinstall is not supported for browser');
+        },
+        uninstall:function(obj, project_dir, plugin_id, options) {
+            events.emit('verbose', 'header-file.uninstall is not supported for browser');
+        }
+    },
+    'resource-file':{
+        install:function(obj, plugin_dir, project_dir, plugin_id, options) {
+            events.emit('verbose', 'resource-file.install is not supported for browser');
+        },
+        uninstall:function(obj, project_dir, plugin_id, options) {
+            events.emit('verbose', 'resource-file.uninstall is not supported for browser');
+        }
+    },
+    'framework': {
+        install:function(obj, plugin_dir, project_dir, plugin_id, options) {
+            events.emit('verbose', 'framework.install is not supported for browser');
+        },
+        uninstall:function(obj, project_dir, plugin_id, options) {
+            events.emit('verbose', 'framework.uninstall is not supported for browser');
+        }
+    },
+    'lib-file': {
+        install:function(obj, plugin_dir, project_dir, plugin_id, options) {
+            events.emit('verbose', 'lib-file.install is not supported for browser');
+        },
+        uninstall:function(obj, project_dir, plugin_id, options) {
+            events.emit('verbose', 'lib-file.uninstall is not supported for browser');
+        }
+    }
+};

http://git-wip-us.apache.org/repos/asf/cordova-browser/blob/d7cdec35/bin/template/cordova/browser_parser.js
----------------------------------------------------------------------
diff --git a/bin/template/cordova/browser_parser.js b/bin/template/cordova/browser_parser.js
index fb0702d..7efa837 100644
--- a/bin/template/cordova/browser_parser.js
+++ b/bin/template/cordova/browser_parser.js
@@ -76,31 +76,29 @@ browser_parser.prototype.update_www = function() {
     console.log("projectRoot = " + projectRoot);
     console.log("app_www = " + app_www);
 
-
     // Clear the www dir
     shell.rm('-rf', this.www_dir());
     shell.mkdir(this.www_dir());
 
     // Copy over all app www assets
     var srcPath = path.join(app_www,'*');
-    console.log('srcPath = ' + srcPath);
+
     var ls = shell.ls(srcPath);
-    console.log('ls ' + ls);
-    shell.cp('-Rf', srcPath, this.www_dir());
+    shell.cp('-rf', srcPath, this.www_dir());
     // Copy over stock platform www assets (cordova.js)
-    //shell.cp('-rf', path.join(platform_www, '*'), this.www_dir());
+    shell.cp('-rf', path.join(platform_www, '*'), this.www_dir());
 };
 
 browser_parser.prototype.update_overrides = function() {
     console.log("update_overrides");
     return;
 
-    var projectRoot = util.isCordova(this.path);
-    var mergesPath = path.join(util.appDir(projectRoot), 'merges', 'browser');
-    if(fs.existsSync(mergesPath)) {
-        var overrides = path.join(mergesPath, '*');
-        shell.cp('-rf', overrides, this.www_dir());
-    }
+    // var projectRoot = util.isCordova(this.path);
+    // var mergesPath = path.join(util.appDir(projectRoot), 'merges', 'browser');
+    // if(fs.existsSync(mergesPath)) {
+    //     var overrides = path.join(mergesPath, '*');
+    //     shell.cp('-rf', overrides, this.www_dir());
+    // }
 };
 
 browser_parser.prototype.config_xml = function(){


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message