You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ripple.apache.org by gt...@apache.org on 2013/11/06 03:21:50 UTC

[17/22] git commit: Better Cordova 3.0 Platform Handling

Better Cordova 3.0 Platform Handling

Will now serve up the content from the correct platform based on Ripples
emulated Device. Also will refresh the correct platforms project.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ripple/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ripple/commit/5188ccba
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ripple/tree/5188ccba
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ripple/diff/5188ccba

Branch: refs/heads/master
Commit: 5188ccbaf11b3b204435118602a16d298f9967e6
Parents: b772023
Author: Gord Tanner <gt...@gmail.com>
Authored: Sat Oct 19 17:49:05 2013 -0400
Committer: Gord Tanner <gt...@gmail.com>
Committed: Sat Oct 19 17:49:05 2013 -0400

----------------------------------------------------------------------
 lib/client/index.js                  |  2 +-
 lib/server/emulate.js                | 38 +++-----------------
 lib/server/emulate/cordovaProject.js | 59 +++++++++++++++++++++++++++++++
 lib/server/emulate/hosted.js         | 18 +++++-----
 lib/server/emulate/static.js         |  9 +++++
 5 files changed, 83 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/5188ccba/lib/client/index.js
----------------------------------------------------------------------
diff --git a/lib/client/index.js b/lib/client/index.js
index bdee787..0a6bf59 100644
--- a/lib/client/index.js
+++ b/lib/client/index.js
@@ -129,10 +129,10 @@ _self = {
              .andThen(fs.initialize, fs)
              .andThen(platform.initialize, platform)
              .andThen(devices.initialize, devices)
+             .andThen(setUserAgent) // See TODO above function def
              .andThen(widgetConfig.initialize, widgetConfig)
              .andThen(deviceSettings.initialize, deviceSettings)
              .andThen(ui.initialize, ui)
-             .andThen(setUserAgent) // See TODO above function def
              .start(booted);
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/5188ccba/lib/server/emulate.js
----------------------------------------------------------------------
diff --git a/lib/server/emulate.js b/lib/server/emulate.js
index 625078a..b0575e4 100644
--- a/lib/server/emulate.js
+++ b/lib/server/emulate.js
@@ -24,7 +24,9 @@ var proxy = require('./proxy'),
     express = require('express'),
     fs = require('fs'),
     path = require('path'),
-    hosted = require('./emulate/hosted');
+    cordovaProject = require('./emulate/cordovaProject'),
+    hosted = require('./emulate/hosted'),
+    static = require('./emulate/static');
 
 colors.mode = "console";
 
@@ -57,41 +59,11 @@ module.exports = {
 
         // TODO: How to make into a dynamic route (using options.route)? (set at build time right now)
         app.use("/ripple/assets", express.static(__dirname + "/../../pkg/hosted"));
+        app.use(cordovaProject.inject(options));
         app.use(hosted.inject(options));
 
         if (!options.remote) {
-            options.path = options.path.map(function (p) {
-                try {
-                    //look for the .cordova folder
-                    fs.statSync(path.join(p, ".cordova"))
-                    console.log("Cordova 3.0 Project detected...");
-                    var platforms = fs.readdirSync(path.join(p, "platforms"));
-                    if (platforms.indexOf('android') >= 0) {
-                        options.cordova = 'android';
-                        p = path.join(p, "platforms", "android", "assets", "www");
-                    }
-                    else if (platforms.indexOf('ios') >= 0) {
-                        options.cordova = 'ios';
-                        p = path.join(p, "platforms", "ios", "www");
-                    }
-                    else if (platforms.indexOf('firefoxos') >= 0) {
-                        options.cordova = 'firefoxos';
-                        p = path.join(p, "platforms", "firefoxos", "www");
-                    }
-                    else if (platforms.indexOf('blackberry10') >= 0) {
-                        options.cordova = 'blackberry10';
-                        p = path.join(p, "platforms", "blackberry10", "www");
-                    }
-                } catch (e) {
-                    //Not a cordova project
-                }
-
-                return p;
-            });
-
-            options.path.forEach(function (path) {
-                app.use("/", express.static(path));
-            });
+            app.use("/", static.inject(options));
         }
 
 // TODO: This should just talk about how to enable ripple via query params

http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/5188ccba/lib/server/emulate/cordovaProject.js
----------------------------------------------------------------------
diff --git a/lib/server/emulate/cordovaProject.js b/lib/server/emulate/cordovaProject.js
new file mode 100644
index 0000000..e2ebf40
--- /dev/null
+++ b/lib/server/emulate/cordovaProject.js
@@ -0,0 +1,59 @@
+var path = require('path'),
+    fs = require('fs');
+
+function buildPaths(opts) {
+    var paths = {
+        orig: opts.path[0]
+    };
+
+    try {
+        //look for the .cordova folder
+        fs.statSync(path.join(paths.orig, ".cordova"))
+        console.log("Cordova 3.0 Project detected...");
+        var platforms = fs.readdirSync(path.join(paths.orig, "platforms"));
+        if (platforms.indexOf('android') >= 0) {
+            opts.cordova = 'android';
+            paths.android = path.join(paths.orig, "platforms", "android", "assets", "www");
+        }
+
+        if (platforms.indexOf('ios') >= 0) {
+            opts.cordova = 'ios';
+            paths.ios = path.join(paths.orig, "platforms", "ios", "www");
+        }
+
+        if (platforms.indexOf('firefoxos') >= 0) {
+            opts.cordova = 'firefoxos';
+            paths.firefox = path.join(paths.orig, "platforms", "firefoxos", "www");
+        }
+
+        if (platforms.indexOf('blackberry10') >= 0) {
+            opts.cordova = 'blackberry10';
+            paths.blackberry = path.join(paths.orig, "platforms", "blackberry10", "www");
+        }
+    } catch (e) {
+        //Not a cordova project
+    }
+
+    return paths;
+}
+
+module.exports = {
+    inject: function (opts) {
+        var paths = buildPaths(opts);
+        return function (req, res, next) {
+            var pth = paths.orig,
+                userAgent = opts.userAgent || "";
+
+            if (opts.cordova) {
+                if (userAgent.match(/Android/)) { pth = paths.android; req.staticPlatform = "android"; }
+                else if (userAgent.match(/iPhone/)) { pth = paths.ios; req.staticPlatform = "ios"; }
+                else if (userAgent.match(/iPad/)) { pth = paths.ios; req.staticPlatform = "ios"; }
+                else if (userAgent.match(/BB10/)) { pth = paths.blackberry; req.staticPlatform = "blackberry" }
+                else { pth = paths.android; req.staticPlatform = "android" }
+            }
+
+            req.staticSource = pth;
+            next();
+        };
+    }
+};

http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/5188ccba/lib/server/emulate/hosted.js
----------------------------------------------------------------------
diff --git a/lib/server/emulate/hosted.js b/lib/server/emulate/hosted.js
index 1543362..d6e94a7 100644
--- a/lib/server/emulate/hosted.js
+++ b/lib/server/emulate/hosted.js
@@ -138,18 +138,18 @@ function localInjection(opts) {
         });
     }
 
+    function getFullPath(req) {
+        var file = path.resolve(req.staticSource + (req.path.match(/\/$/) ? path.join(req.path, "index.html") : req.path));
+        return fs.existsSync(file) ? file : null;
+    }
+
     function handle(req, res, next) {
         // TODO: DRY (see proxyRemote function)
         if (req.query.enableripple) {
             res.sendfile(path.join(HOSTED_PKG_DIR, "index.html"));
         } else if (req.path.match(/^\/$/) || req.path.match(/\.html/)) {
             //first matching file
-            var fullPath = opts.path.reduce(function (match, curr) {
-                if (match) return match;
-                var file = path.resolve(curr + (req.path.match(/\/$/) ? path.join(req.path, "index.html") : req.path));
-                return fs.existsSync(file) ? file : match;
-            }, null);
-
+            var fullPath = getFullPath(req);
             if (fullPath) {
                 inject(fullPath, req, res);
             } else {
@@ -161,9 +161,9 @@ function localInjection(opts) {
     };
 
     return function (req, res, next) {
-        if (req.query.enableripple && opts.cordova) {
-            console.log('refreshing project ..');
-            exec('cordova prepare ' + opts.cordova, function () {
+        if (req.query.enableripple && req.staticPlatform) {
+            console.log('refreshing project (platform: ' + req.staticPlatform + ') ...');
+            exec('cordova prepare ' + req.staticPlatform, function () {
                 handle(req, res, next);
             });
         }

http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/5188ccba/lib/server/emulate/static.js
----------------------------------------------------------------------
diff --git a/lib/server/emulate/static.js b/lib/server/emulate/static.js
new file mode 100644
index 0000000..09e56bb
--- /dev/null
+++ b/lib/server/emulate/static.js
@@ -0,0 +1,9 @@
+var express = require('express');
+
+module.exports = {
+    inject: function (opts) {
+        return function (req, res, next) {
+            express.static(req.staticSource)(req, res, next);
+        };
+    }
+};