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/12 22:44:04 UTC

[1/2] git commit: fleshed out plugin addition. tests!

Updated Branches:
  refs/heads/cordova-client f55d0e862 -> ac2b66dbc


fleshed out plugin addition. tests!


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/ac2b66db
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/tree/ac2b66db
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/diff/ac2b66db

Branch: refs/heads/cordova-client
Commit: ac2b66dbcae7afd1888cd5a880d19e1b622906e6
Parents: c2ceb9a
Author: Fil Maj <ma...@gmail.com>
Authored: Wed Sep 12 13:43:41 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Wed Sep 12 13:43:41 2012 -0700

----------------------------------------------------------------------
 spec/fixtures/plugins/android/plugin.xml |   14 ++++
 spec/fixtures/plugins/test/plugin.xml    |    1 +
 spec/plugin.spec.js                      |   97 +++++++++++++++++++++++--
 src/plugin.js                            |   30 +++++++--
 4 files changed, 132 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/ac2b66db/spec/fixtures/plugins/android/plugin.xml
----------------------------------------------------------------------
diff --git a/spec/fixtures/plugins/android/plugin.xml b/spec/fixtures/plugins/android/plugin.xml
new file mode 100644
index 0000000..d8f5619
--- /dev/null
+++ b/spec/fixtures/plugins/android/plugin.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    id="ca.filmaj.AndroidPlugin"
+    version="4.2.0">
+
+    <name>Android Plugin</name>
+
+    <asset src="www/android.js" target="android.js" />
+    <platform name="android">
+    </platform>
+</plugin>
+
+

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/ac2b66db/spec/fixtures/plugins/android/src/android/Android.java
----------------------------------------------------------------------
diff --git a/spec/fixtures/plugins/android/src/android/Android.java b/spec/fixtures/plugins/android/src/android/Android.java
new file mode 100644
index 0000000..e69de29

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/ac2b66db/spec/fixtures/plugins/android/www/android.js
----------------------------------------------------------------------
diff --git a/spec/fixtures/plugins/android/www/android.js b/spec/fixtures/plugins/android/www/android.js
new file mode 100644
index 0000000..e69de29

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/ac2b66db/spec/fixtures/plugins/test/plugin.xml
----------------------------------------------------------------------
diff --git a/spec/fixtures/plugins/test/plugin.xml b/spec/fixtures/plugins/test/plugin.xml
index 5d2a829..9ab851c 100644
--- a/spec/fixtures/plugins/test/plugin.xml
+++ b/spec/fixtures/plugins/test/plugin.xml
@@ -10,6 +10,7 @@
     <platform name="android">
     </platform>
     <platform name="ios">
+        <plugins-plist key="TestPlugin" string="Test" />
     </platform>
     <platform name="blackberry">
     </platform>

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/ac2b66db/spec/plugin.spec.js
----------------------------------------------------------------------
diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js
index 54bbc4b..b0fd6dc 100644
--- a/spec/plugin.spec.js
+++ b/spec/plugin.spec.js
@@ -6,7 +6,10 @@ var cordova = require('../cordova'),
     fs = require('fs'),
     tempDir = path.join(__dirname, '..', 'temp'),
     fixturesDir = path.join(__dirname, 'fixtures'),
-    testPlugin = path.join(fixturesDir, 'plugins', 'test');
+    testPlugin = path.join(fixturesDir, 'plugins', 'test'),
+    androidPlugin = path.join(fixturesDir, 'plugins', 'android');
+
+var cwd = process.cwd();
 
 describe('plugin command', function() {
     beforeEach(function() {
@@ -16,7 +19,6 @@ describe('plugin command', function() {
     });
 
     it('should run inside a Cordova-based project', function() {
-        var cwd = process.cwd();
         this.after(function() {
             process.chdir(cwd);
         });
@@ -30,7 +32,6 @@ describe('plugin command', function() {
         }).not.toThrow();
     });
     it('should not run outside of a Cordova-based project', function() {
-        var cwd = process.cwd();
         this.after(function() {
             process.chdir(cwd);
         });
@@ -43,8 +44,6 @@ describe('plugin command', function() {
     });
 
     describe('`ls`', function() {
-        var cwd = process.cwd();
-
         beforeEach(function() {
             cordova.create(tempDir);
         });
@@ -61,6 +60,94 @@ describe('plugin command', function() {
     });
 
     describe('`add`', 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('add', testPlugin);
+                }).toThrow('You need at least one platform added to your app. Use `cordova platform add <platform>`.');
+            });
+            it('should throw if plugin does not support any app platforms', 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('add', androidPlugin);
+                    }).toThrow('Plugin "android" does not support any of your application\'s platforms. Plugin platforms: android; your application\'s platforms: ios');
+                });
+            });
+            it('should throw if plugin is already added to project', function() {
+                var cb = jasmine.createSpy();
+                var pluginCb = 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() {
+                    expect(function() {
+                        cordova.plugin('add', testPlugin, pluginCb);
+                    }).toThrow('Plugin "test" already added to project.');
+                });
+            });
+            it('should throw if plugin does not have a plugin.xml', 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('add', fixturesDir);
+                    }).toThrow('Plugin "fixtures" does not have a plugin.xml in the root. Plugin must support the Cordova Plugin Specification: https://github.com/alunny/cordova-plugin-spec');
+                });
+            });
+        });
+        describe('success', function() {
+            it('should add plugin www assets to project www folder', function() {
+                var cb = jasmine.createSpy();
+                var pluginCb = 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() {
+                    expect(fs.existsSync(path.join(tempDir, 'www', 'test.js'))).toBe(true);
+                });
+            });
+            it('should add the full plugin to the plugins directory', function() {
+                var cb = jasmine.createSpy();
+                var pluginCb = 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() {
+                    expect(fs.existsSync(path.join(tempDir, 'plugins', 'test'))).toBe(true);
+                });
+            });
+        });
     });
 
     describe('`remove`', function() {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/ac2b66db/src/plugin.js
----------------------------------------------------------------------
diff --git a/src/plugin.js b/src/plugin.js
index eb1e71a..20755c3 100644
--- a/src/plugin.js
+++ b/src/plugin.js
@@ -5,6 +5,7 @@ var cordova_util  = require('./util'),
     fs            = require('fs'),
     path          = require('path'),
     config_parser = require('./config_parser'),
+    plugin_parser = require('./plugin_parser'),
     exec          = require('child_process').exec,
     asyncblock    = require('asyncblock'),
     ls            = fs.readdirSync;
@@ -17,9 +18,10 @@ module.exports = function plugin(command, target, callback) {
     }
     if (arguments.length === 0) command = 'ls';
 
-    // Grab config info for the project
-    var xml = path.join(projectRoot, 'www', 'config.xml');
     var projectWww = path.join(projectRoot, 'www');
+
+    // Grab config info for the project
+    var xml = path.join(projectWww, 'config.xml');
     var cfg = new config_parser(xml);
     var platforms = cfg.ls_platforms();
 
@@ -39,6 +41,9 @@ module.exports = function plugin(command, target, callback) {
             } else return 'No plugins added. Use `cordova plugin add <plugin>`.';
             break;
         case 'add':
+            if (platforms.length === 0) {
+                throw 'You need at least one platform added to your app. Use `cordova platform add <platform>`.';
+            }
             // Check if we already have the plugin.
             // TODO edge case: if a new platform is added, then you want
             // to re-add the plugin to the new platform.
@@ -53,16 +58,26 @@ module.exports = function plugin(command, target, callback) {
                 throw 'Plugin "' + targetName + '" does not have a plugin.xml in the root. Plugin must support the Cordova Plugin Specification: https://github.com/alunny/cordova-plugin-spec';
             }
 
+            // Check if there is at least one match between plugin
+            // supported platforms and app platforms
+            var pluginXml = new plugin_parser(path.join(target, 'plugin.xml'));
+            var intersection = pluginXml.platforms.filter(function(e) {
+                if (platforms.indexOf(e) == -1) return false;
+                else return true;
+            });
+            if (intersection.length === 0) {
+                throw 'Plugin "' + targetName + '" does not support any of your application\'s platforms. Plugin platforms: ' + pluginXml.platforms.join(', ') + '; your application\'s platforms: ' + platforms.join(', ');
+            }
+
             var pluginWww = path.join(target, 'www');
             var wwwContents = ls(pluginWww);
             var cli = path.join(__dirname, '..', 'node_modules', 'pluginstall', 'cli.js');
 
             asyncblock(function(flow) {
-                // Iterate over all platforms in the project and install the
+                // Iterate over all matchin app-plugin platforms in the project and install the
                 // plugin.
-                platforms.forEach(function(platform) {
+                intersection.forEach(function(platform) {
                     var cmd = util.format('%s %s "%s" "%s"', cli, platform, path.join(projectRoot, 'platforms', platform), target);
-                    console.log('executing ' + cmd);
                     var key = 'pluginstall-' + platform;
                     exec(cmd, flow.set({
                       key:key,
@@ -87,6 +102,11 @@ module.exports = function plugin(command, target, callback) {
                         fs.writeFileSync(wwwPath, fs.readFileSync(asset));
                     }
                 });
+
+                // Finally copy the plugin into the project
+                cpr(target, path.join(pluginPath, targetName));
+
+                if (callback) callback();
             });
 
             break;