You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/09/24 20:56:18 UTC

[3/3] git commit: added build command invoking changes to app data via config.xml specs. also added plugin removal and specs

added build command invoking changes to app data via config.xml specs. also added plugin removal and specs


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/commit/6203358f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/tree/6203358f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/diff/6203358f

Branch: refs/heads/cordova-client
Commit: 6203358fd19f4e3ef046983c227ffc0c9efab5bc
Parents: 1bc7040
Author: Fil Maj <ma...@gmail.com>
Authored: Sun Sep 23 17:54:43 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Sun Sep 23 17:54:43 2012 -0700

----------------------------------------------------------------------
 spec/build.spec.js  |   77 +++++++++++++++++++++++++++++++---------------
 spec/plugin.spec.js |   71 ++++++++++++++++++++++++++++++++++++++++++
 src/plugin.js       |   50 ++++++++++++++++++++++++++++--
 3 files changed, 170 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/6203358f/spec/build.spec.js
----------------------------------------------------------------------
diff --git a/spec/build.spec.js b/spec/build.spec.js
index 49b4bac..067c267 100644
--- a/spec/build.spec.js
+++ b/spec/build.spec.js
@@ -1,5 +1,6 @@
 var cordova = require('../cordova'),
     wrench = require('wrench'),
+    et = require('elementtree'),
     mkdirp = wrench.mkdirSyncRecursive,
     path = require('path'),
     rmrf = wrench.rmdirSyncRecursive,
@@ -114,7 +115,6 @@ describe('build command', function() {
         beforeEach(function() {
             cordova.create(tempDir);
             process.chdir(tempDir);
-            cfg = config_parser(path.join(tempDir, 'www', 'config.xml'));
         });
 
         afterEach(function() {
@@ -122,32 +122,59 @@ describe('build command', function() {
         });
 
         describe('into Android builds', function() {
-          it('should interpolate app name', function () {
-              /*
-              var buildcb = jasmine.createSpy();
-              var cb = jasmine.createSpy();
-              var newName = "devil ether";
-
-              runs(function() {
-                  cordova.platform('add', 'android', cb);
-              });
-              waitsFor(function() { return cb.wasCalled; }, 'platform add android callback');
-
-              runs(function() {
-                  cfg.name(newName); // set a new name in the config.xml
-                  cordova.build(buildcb);
-              });
-              waitsFor(function() { return buildcb.wasCalled; }, 'build call', 20000);
-              runs(function() {
-                  // TODO
-              });
-              */
-          });
-          it('should interpolate package name');
+            it('should interpolate app name', function () {
+                var buildcb = jasmine.createSpy();
+                var cb = jasmine.createSpy();
+                var newName = "devil ether", s;
+
+                runs(function() {
+                    cordova.platform('add', 'android', cb);
+                });
+                waitsFor(function() { return cb.wasCalled; }, 'platform add android callback');
+
+                runs(function() {
+                    // intercept call to ./cordova/debug to speed things up
+                    s = spyOn(require('shelljs'), 'exec').andReturn({code:0});
+                    cfg = new config_parser(path.join(tempDir, 'www', 'config.xml'));
+                    cfg.name(newName); // set a new name in the config.xml
+                    cordova.build(buildcb);
+                });
+                waitsFor(function() { return buildcb.wasCalled; }, 'build call', 20000);
+
+                runs(function() {
+                    var strings = path.join(tempDir, 'platforms', 'android', 'res', 'values', 'strings.xml');
+                    var doc = new et.ElementTree(et.XML(fs.readFileSync(strings, 'utf-8')));
+                    expect(doc.find('string[@name="app_name"]').text).toBe('devil ether');
+                });
+            });
+            it('should interpolate package name');
         });
         describe('into iOS builds', function() {
-          it('should interpolate app name');
-          it('should interpolate package name');
+            it('should interpolate app name', function() {
+                var buildcb = jasmine.createSpy();
+                var cb = jasmine.createSpy();
+                var newName = "i keep getting older, they stay the same age", s;
+
+                runs(function() {
+                    cordova.platform('add', 'ios', cb);
+                });
+                waitsFor(function() { return cb.wasCalled; }, 'platform add ios callback');
+
+                runs(function() {
+                    // intercept call to ./cordova/debug to speed things up
+                    s = spyOn(require('shelljs'), 'exec').andReturn({code:0});
+                    cfg = new config_parser(path.join(tempDir, 'www', 'config.xml'));
+                    cfg.name(newName); // set a new name in the config.xml
+                    cordova.build(buildcb);
+                });
+                waitsFor(function() { return buildcb.wasCalled; }, 'build call', 20000);
+
+                runs(function() {
+                    var pbxproj = path.join(tempDir, 'platforms', 'ios', 'Hello_Cordova.xcodeproj', 'project.pbxproj');
+                    expect(fs.readFileSync(pbxproj, 'utf-8').match(/PRODUCT_NAME\s*=\s*"i keep getting older, they stay the same age"/)).not.toBeNull();
+                });
+            });
+            it('should interpolate package name');
         });
     });
 });

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/6203358f/spec/plugin.spec.js
----------------------------------------------------------------------
diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js
index b53cf89..2ba78a5 100644
--- a/spec/plugin.spec.js
+++ b/spec/plugin.spec.js
@@ -103,6 +103,8 @@ describe('plugin command', function() {
                     }).toThrow('Plugin "test" already added to project.');
                 });
             });
+            it('should throw if plugin www assets collide with existing project www assets', function() {
+            });
             it('should throw if plugin does not have a plugin.xml', function() {
                 var cb = jasmine.createSpy();
                 runs(function() {
@@ -156,6 +158,75 @@ describe('plugin command', function() {
     });
 
     describe('`remove`', function() {
+        beforeEach(function() {
+            cordova.create(tempDir);
+            process.chdir(tempDir);
+        });
+
+        afterEach(function() {
+            process.chdir(cwd);
+        });
+        describe('failure', function() {
+            it('should throw if your app has no platforms added', function() {
+                expect(function() {
+                    cordova.plugin('remove', testPlugin);
+                }).toThrow('You need at least one platform added to your app. Use `cordova platform add <platform>`.');
+            });
+            it('should throw if plugin is not added to project', function() {
+                var cb = jasmine.createSpy();
+                runs(function() {
+                    cordova.platform('add', 'ios', cb);
+                });
+                waitsFor(function() { return cb.wasCalled; }, 'ios platform add');
+                runs(function() {
+                    expect(function() {
+                        cordova.plugin('remove', 'test', function() {});
+                    }).toThrow('Plugin "test" not added to project.');
+                });
+            });
+        });
+        describe('success', function() {
+            it('should remove plugin www assets from project www folder', function() {
+                var cb = jasmine.createSpy();
+                var pluginCb = jasmine.createSpy();
+                var removeCb = jasmine.createSpy();
+                runs(function() {
+                    cordova.platform('add', 'ios', cb);
+                });
+                waitsFor(function() { return cb.wasCalled; }, 'ios platform add');
+                runs(function() {
+                    cordova.plugin('add', testPlugin, pluginCb);
+                });
+                waitsFor(function() { return pluginCb.wasCalled; }, 'test plugin add');
+                runs(function() {
+                    cordova.plugin('remove', 'test', removeCb);
+                });
+                waitsFor(function() { return removeCb.wasCalled; }, 'test plugin remove');
+                runs(function() {
+                    expect(fs.existsSync(path.join(tempDir, 'www', 'test.js'))).toBe(false);
+                });
+            });
+            it('should remove the full plugin from the plugins directory', function() {
+                var cb = jasmine.createSpy();
+                var pluginCb = jasmine.createSpy();
+                var removeCb = jasmine.createSpy();
+                runs(function() {
+                    cordova.platform('add', 'ios', cb);
+                });
+                waitsFor(function() { return cb.wasCalled; }, 'ios platform add');
+                runs(function() {
+                    cordova.plugin('add', testPlugin, pluginCb);
+                });
+                waitsFor(function() { return pluginCb.wasCalled; }, 'test plugin add');
+                runs(function() {
+                    cordova.plugin('remove', 'test', removeCb);
+                });
+                waitsFor(function() { return removeCb.wasCalled; }, 'test plugin remove');
+                runs(function() {
+                    expect(fs.existsSync(path.join(tempDir, 'plugins', 'test'))).toBe(false);
+                });
+            });
+        });
     });
 });
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/6203358f/src/plugin.js
----------------------------------------------------------------------
diff --git a/src/plugin.js b/src/plugin.js
index a5bdd4e..e5423d1 100644
--- a/src/plugin.js
+++ b/src/plugin.js
@@ -2,6 +2,7 @@ var cordova_util  = require('./util'),
     util          = require('util'),
     wrench        = require('wrench'),
     cpr           = wrench.copyDirSyncRecursive,
+    rmrf          = wrench.rmdirSyncRecursive,
     fs            = require('fs'),
     path          = require('path'),
     shell         = require('shelljs'),
@@ -101,9 +102,52 @@ module.exports = function plugin(command, target, callback) {
             if (callback) callback();
             break;
         case 'remove':
-            // TODO: remove plugin. requires pluginstall to
-            // support removal.
-            throw 'Plugin removal not supported yet! sadface';
+            if (platforms.length === 0) {
+                throw 'You need at least one platform added to your app. Use `cordova platform add <platform>`.';
+            }
+            // Check if we have the plugin.
+            if (plugins.indexOf(targetName) > -1) {
+                var pluginWww = path.join(pluginPath, target, 'www');
+                var wwwContents = ls(pluginWww);
+                var cli = path.join(__dirname, '..', 'node_modules', 'pluginstall', 'cli.js');
+
+                // Check if there is at least one match between plugin
+                // supported platforms and app platforms
+                var pluginXml = new plugin_parser(path.join(pluginPath, target, 'plugin.xml'));
+                var intersection = pluginXml.platforms.filter(function(e) {
+                    if (platforms.indexOf(e) == -1) return false;
+                    else return true;
+                });
+
+                // Iterate over all matchin app-plugin platforms in the project and uninstall the
+                // plugin.
+                intersection.forEach(function(platform) {
+                    var cmd = util.format('%s -d %s "%s" "%s"', cli, platform, path.join(projectRoot, 'platforms', platform), path.join(pluginPath, target));
+                    var plugin_cli = shell.exec(cmd, {silent:true});
+                    if (plugin_cli.code > 0) throw 'An error occured during plugin uninstallation for ' + platform + '. ' + cli.output;
+                });
+                
+                // Remove the plugin web assets to the www folder as well
+                // TODO: assumption that web assets go under www folder
+                // inside plugin dir; instead should read plugin.xml
+                wwwContents.forEach(function(asset) {
+                    asset = path.resolve(path.join(projectWww, asset));
+                    var info = fs.lstatSync(asset);
+                    if (info.isDirectory()) {
+                        rmrf(asset);
+                    } else {
+                        fs.unlinkSync(asset);
+                    }
+                });
+
+                // Finally remove the plugin dir from plugins/
+                rmrf(path.join(pluginPath, target));
+
+                if (callback) callback();
+            } else {
+                throw 'Plugin "' + targetName + '" not added to project.';
+            }
+            break;
         default:
             throw 'Unrecognized command "' + command + '". Use either `add`, `remove`, or `ls`.';
     }