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 2013/02/18 21:05:40 UTC

[1/4] git commit: addition of merges functionality

addition of merges functionality

merges functionality which allows for merging in www content per
platform.  For more detail see the following email thread with
brian.leroux@gmail.com
-----------------
Dude, first thing I have to say that I love this, I think its a really
great addition, and will clean up project code a tonne. Some
feedback/queries:

- don't like the folder name. Its precious, I know, but I think we can
some up w/ some better than isn't camel cased. www-overrides or maybe
just "clobbers" or "merges" seems more descriptive to me (in my ideal
fantasy world platforms wouldn't even be in revision control and would
be a build artifact. we're not there yet obviously!)

- if I have something in the overrides folder that IS NOT in the www
equiv would it just get copied/merged in? seems this would be so but
this isn't clear

This def belongs in the cordova-cli project. What I also like is that
we could really start envisioning more shim type code working here.
(For cases like Chrome Packaged Apps or what have you.)

On Sun, Feb 10, 2013 at 3:22 AM, Michael Wolf
<Mi...@cynergy.com> wrote:
Awesome.  Its funny you mention the platform name, when I first did
this in our build system I ended up following the iOS pattern of doing
like ~platform name like they do ~iphone or ~ipad.  However it just
became a pain as you had to change file names and more importantly it
confused as to what was purely in browser versus what would go into the
platform builds.  So what I ended up having us do was have an overrides
folder that just has the same file structure of the www such that you
just copy over and what evers in the overrides folder overwrites the
www file which had been copied in.  see below as an example.  Anywho…
happy to contribute is this the map git
https://github.com/apache/cordova-cli ?

mw

On 2/9/13 5:52 AM, "Brian LeRoux" <b...@brian.io> wrote:

I love this idea. A much older prototype of the CLI stuff had some
smarts that when it detected a file with a platform name in the string
it copied it only to that platform.

Do a pull request and link in this thread for discussion. I think it
would be a great addition to the workflow.

On Fri, Feb 8, 2013 at 6:15 PM, Michael Wolf <Mi...@cynergy.com>
wrote:
Hey Man:

I have been loving the updates in the phone gap cli.  Have a feature Id
love
to add, its something which I had in our internal cynergy build scripts
to
support building for multiple platforms at once and mocking in, in
browser.
The idea is a platform overrides folder, which will override
css/js/html/images etc when deployed to a specific platform.  For
example
images you want to replace on android but not iOS, or js code you want
to
mock in browser, but then replace w/ a real implementation when
building for
android/ios, or instead of writing conditional platform logic for a fix
in
iOS or android you replace the actual js file on each platform.   This
is a
strategy which we have used on our projects since the early halo days,
and
it has helped a lot in simplifying production code…. Particualry when
you
try and build for a common base between say something for ios/android
and
windows phone. Anywho….

I have an implantation working in the cordova cli node scripts.  How
might I
go about swinging this idea by team, more than happy to add the feature.

mw


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

Branch: refs/heads/merges
Commit: 908eb663324497d3cc04aefedccbe6ae0b054bb5
Parents: c8e05a8
Author: michael.wolf <mi...@MWolf-MBP.local>
Authored: Mon Feb 11 22:28:58 2013 -0500
Committer: Fil Maj <ma...@gmail.com>
Committed: Mon Feb 18 10:56:01 2013 -0800

----------------------------------------------------------------------
 src/build.js                      |  123 ++++++++++++++++++++++++++++++++
 src/create.js                     |    1 +
 src/metadata/android_parser.js    |   10 +++-
 src/metadata/blackberry_parser.js |    9 +++
 src/metadata/ios_parser.js        |    9 +++
 src/platform.js                   |    8 ++
 6 files changed, 159 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/908eb663/src/build.js
----------------------------------------------------------------------
diff --git a/src/build.js b/src/build.js
new file mode 100644
index 0000000..722047a
--- /dev/null
+++ b/src/build.js
@@ -0,0 +1,123 @@
+/**
+    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 cordova_util  = require('./util'),
+    path          = require('path'),
+    config_parser = require('./config_parser'),
+    platform      = require('./platform'),
+    fs            = require('fs'),
+    shell         = require('shelljs'),
+    ls            = fs.readdirSync,
+    et            = require('elementtree'),
+    android_parser= require('./metadata/android_parser'),
+    blackberry_parser= require('./metadata/blackberry_parser'),
+    ios_parser    = require('./metadata/ios_parser'),
+    hooker        = require('./hooker'),
+    n             = require('ncallbacks'),
+    prompt        = require('prompt'),
+    util          = require('util');
+
+function shell_out_to_debug(projectRoot, platform, callback) {
+    var cmd = path.join(projectRoot, 'platforms', platform);
+    // TODO: this is bb10 only for now
+    // TODO: PLATFORM LIBRARY INCONSISTENCY
+    if (platform == 'blackberry') {
+        cmd = 'ant -f "' + path.join(cmd, 'build.xml') + '" qnx load-device';
+    } else {
+        cmd = '"' + cmd + '/cordova/build"';
+    }
+    shell.exec(cmd, {silent:true, async:true}, function(code, output) {
+        if (code > 0) {
+            throw new Error('An error occurred while building the ' + platform + ' project. ' + output);
+        } else {
+            callback();
+        }
+    });
+}
+
+module.exports = function build(platforms, callback) {
+    var projectRoot = cordova_util.isCordova(process.cwd());
+
+    if (!projectRoot) {
+        throw new Error('Current working directory is not a Cordova-based project.');
+    }
+
+    var xml = path.join(projectRoot, 'www', 'config.xml');
+    var assets = path.join(projectRoot, 'www');
+    var cfg = new config_parser(xml);
+
+    if (arguments.length === 0 || (platforms instanceof Array && platforms.length === 0)) {
+        platforms = ls(path.join(projectRoot, 'platforms'));
+    } else if (typeof platforms == 'string') platforms = [platforms];
+    else if (platforms instanceof Function && callback === undefined) {
+        callback = platforms;
+        platforms = ls(path.join(projectRoot, 'platforms'));
+    }
+
+    if (platforms.length === 0) throw new Error('No platforms added to this project. Please use `cordova platform add <platform>`.');
+
+    var hooks = new hooker(projectRoot);
+    if (!(hooks.fire('before_build'))) {
+        throw new Error('before_build hooks exited with non-zero code. Aborting.');
+    }
+
+    var end = n(platforms.length, function() {
+        if (!(hooks.fire('after_build'))) {
+            throw new Error('after_build hooks exited with non-zero code. Aborting.');
+        }
+        if (callback) callback();
+    });
+
+    // Iterate over each added platform 
+    platforms.forEach(function(platform) {
+        // Figure out paths based on platform
+        var parser, platformPath;
+        switch (platform) {
+            case 'android':
+                platformPath = path.join(projectRoot, 'platforms', 'android');
+                parser = new android_parser(platformPath);
+
+                // Update the related platform project from the config
+                parser.update_project(cfg);
+                parser.update_overrides();
+                shell_out_to_debug(projectRoot, 'android', end);
+                break;
+            case 'blackberry':
+                platformPath = path.join(projectRoot, 'platforms', 'blackberry');
+                parser = new blackberry_parser(platformPath);
+                
+                // Update the related platform project from the config
+                parser.update_project(cfg, function() {
+                    parser.update_overrides();
+                    // Shell it
+                    shell_out_to_debug(projectRoot, 'blackberry', end);
+                });
+                break;
+            case 'ios':
+                platformPath = path.join(projectRoot, 'platforms', 'ios');
+                parser = new ios_parser(platformPath);
+
+                // Update the related platform project from the config
+                parser.update_project(cfg, function() {
+                    parser.update_overrides();
+                    shell_out_to_debug(projectRoot, 'ios', end);
+                });
+                break;
+        }
+    });
+};

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/908eb663/src/create.js
----------------------------------------------------------------------
diff --git a/src/create.js b/src/create.js
index c2f20bd..c4ddb1d 100644
--- a/src/create.js
+++ b/src/create.js
@@ -59,6 +59,7 @@ module.exports = function create (dir, id, name) {
     // Create basic project structure.
     shell.mkdir('-p', dotCordova);
     shell.mkdir('-p', path.join(dir, 'platforms'));
+    shell.mkdir('-p', path.join(dir, 'merges'));
     shell.mkdir('-p', path.join(dir, 'plugins'));
     var hooks = path.join(dotCordova, 'hooks');
     shell.mkdir('-p', hooks);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/908eb663/src/metadata/android_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/android_parser.js b/src/metadata/android_parser.js
index a79101f..be29de1 100644
--- a/src/metadata/android_parser.js
+++ b/src/metadata/android_parser.js
@@ -1,4 +1,3 @@
-
 /**
     Licensed to the Apache Software Foundation (ASF) under one
     or more contributor license agreements.  See the NOTICE file
@@ -144,6 +143,15 @@ module.exports.prototype = {
         // delete any .svn folders copied over
         util.deleteSvnFolders(platformWww);
     },
+
+    // update the overrides folder into the www folder
+    update_overrides:function() {
+        var projectRoot = util.isCordova(this.path);
+        var project_www = path.join(this.path, 'assets','www');
+        var overrides = path.join(projectRoot, 'merges','android');
+        shell.cp('-rf', overrides+'/*',project_www);
+    },
+
     update_project:function(cfg, callback) {
         this.update_from_config(cfg);
         this.update_www();

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/908eb663/src/metadata/blackberry_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/blackberry_parser.js b/src/metadata/blackberry_parser.js
index e10fa83..91d0f60 100644
--- a/src/metadata/blackberry_parser.js
+++ b/src/metadata/blackberry_parser.js
@@ -120,6 +120,15 @@ module.exports.prototype = {
 
         util.deleteSvnFolders(platformWww);
     },
+
+    // update the overrides folder into the www folder
+    update_overrides:function() {
+        var projectRoot = util.isCordova(this.path);
+        var platformWww = path.join(this.path, 'www');
+        var overrides = path.join(projectRoot, 'merges','blackberry');
+        shell.cp('-rf', overrides+'/*',platformWww);
+    },
+
     write_project_properties:function() {
         // TODO: eventually support all blackberry sub-platforms
         var projectRoot = util.isCordova(this.path);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/908eb663/src/metadata/ios_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/ios_parser.js b/src/metadata/ios_parser.js
index 9a9f306..03455ed 100644
--- a/src/metadata/ios_parser.js
+++ b/src/metadata/ios_parser.js
@@ -153,6 +153,15 @@ module.exports.prototype = {
 
         util.deleteSvnFolders(project_www);
     },
+
+    // update the overrides folder into the www folder
+    update_overrides:function() {
+        var projectRoot = util.isCordova(this.path);
+        var project_www = path.join(this.path, 'www');
+        var overrides = path.join(projectRoot, 'merges','ios');
+        shell.cp('-rf', overrides+'/*',project_www);
+    },
+
     update_project:function(cfg, callback) {
         var self = this;
         this.update_from_config(cfg, function() {

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/908eb663/src/platform.js
----------------------------------------------------------------------
diff --git a/src/platform.js b/src/platform.js
index cd3098b..4705130 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -38,6 +38,10 @@ module.exports = function platform(command, targets, callback) {
     var hooks = new hooker(projectRoot),
         end;
 
+    var createOverrides = function(target){
+        shell.mkdir('-p', path.join('merges',target));
+    };
+
     if (arguments.length === 0) command = 'ls';
     if (targets) {
         if (!(targets instanceof Array)) targets = [targets];
@@ -91,12 +95,14 @@ module.exports = function platform(command, targets, callback) {
                                 case 'android':
                                     var android = new android_parser(output);
                                     android.update_project(cfg);
+                                    createOverrides(target);
                                     hooks.fire('after_platform_add');
                                     end();
                                     break;
                                 case 'ios':
                                     var ios = new ios_parser(output);
                                     ios.update_project(cfg, function() {
+                                        createOverrides(target);
                                         hooks.fire('after_platform_add');
                                         end();
                                     });
@@ -104,6 +110,7 @@ module.exports = function platform(command, targets, callback) {
                                 case 'blackberry':
                                     var bb = new blackberry_parser(output);
                                     bb.update_project(cfg, function() {
+                                        createOverrides(target);
                                         hooks.fire('after_platform_add');
                                         end();
                                     });
@@ -119,6 +126,7 @@ module.exports = function platform(command, targets, callback) {
             targets.forEach(function(target) {
                 hooks.fire('before_platform_rm');
                 shell.rm('-rf', path.join(projectRoot, 'platforms', target));
+                shell.rm('-rf', path.join(projectRoot,'merges',target));
                 hooks.fire('after_platform_rm');
             });
             break;