You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sg...@apache.org on 2015/02/20 10:47:06 UTC

cordova-medic git commit: CB-8508 Android scripts rework, config patching changes

Repository: cordova-medic
Updated Branches:
  refs/heads/master 399c2140f -> aa2f08ba4


CB-8508 Android scripts rework, config patching changes

github close 33


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

Branch: refs/heads/master
Commit: aa2f08ba496448e7d44c40e68fe8cd2ee34664fe
Parents: 399c214
Author: alsorokin <al...@akvelon.com>
Authored: Thu Feb 19 11:41:20 2015 +0300
Committer: sgrebnov <v-...@microsoft.com>
Committed: Fri Feb 20 12:46:55 2015 +0300

----------------------------------------------------------------------
 build_android.js                    |  64 ++++++-----
 src/build/makers/android.js         | 191 ++++++++++++++++++++-----------
 src/build/makers/android/deploy.js  | 113 ------------------
 src/build/makers/android/devices.js |  66 -----------
 testchecker.js                      |  63 +++++-----
 updateconfig.js                     |  50 ++++++--
 6 files changed, 233 insertions(+), 314 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/aa2f08ba/build_android.js
----------------------------------------------------------------------
diff --git a/build_android.js b/build_android.js
index 038e95e..b852572 100644
--- a/build_android.js
+++ b/build_android.js
@@ -1,41 +1,47 @@
-var path = require ('path');
-var shell = require('shelljs');
-var buildinfo = require('./buildinfo');
-var config = require('./config');
-var android  = require('./src/build/makers/android');
-var argv = require('optimist').argv;
-var testcheck = require('./testchecker');
+var path         = require('path'),
+    buildinfo    = require('./buildinfo'),
+    config       = require('./config'),
+    android      = require('./src/build/makers/android'),
+    argv         = require('optimist').argv,
+    testcheck    = require('./testchecker'),
+    error_writer = require('./src/build/makers/error_writer');
 
 // this assumes that you start it in the sandbox
 
-var TEST_DIR=process.cwd();
-var BRANCH='master';
-var TOOL_DIR=path.join(TEST_DIR,'medic');
-var MSPEC_DIR=path.join(TEST_DIR,'mobilespec');
+var TEST_DIR = process.cwd(),
+    BRANCH = 'master',
+    MSPEC_DIR = path.join(TEST_DIR, 'mobilespec'),
+    TEST_OK = true;
 
-var TEST_OK=true;
+if (argv.branch) {
+    BRANCH = argv.branch;
+}
 
-if(argv.branch) BRANCH=argv.branch;
+var output_location = path.join(MSPEC_DIR, 'platforms', 'android'),
+    test_timeout = config.app.timeout || 10 * 60;
 
-var output_location = path.join(MSPEC_DIR,'platforms','android');
-
-buildinfo('Android', BRANCH, function (error, sha ) {
-    if(error) {
-        TEST_OK=false;
+buildinfo('Android', BRANCH, function (error, sha) {
+    if (error) {
+        TEST_OK = false;
     } else {
-        android(output_location, sha,'', config.app.entry, config.couchdb, function(err){
-            if(err) {
-                console.log('Android test prepare failed');
-                TEST_OK=false;
-            } else {
-                console.log('Android tests complete');
-                TEST_OK = true;
-            }
-       });
+        android(output_location, sha, config.couchdb, test_timeout)
+            .then(function () {
+                return testcheck(sha, config.couchdb.host);
+            }, function (err) {
+                TEST_OK = false;
+                error_writer('android', sha, 'Android tests execution failed.', err);
+            }).then(function (testCheckResult) {
+                TEST_OK = testCheckResult;
+
+                if (TEST_OK) {
+                    console.log('Android test execution completed');
+                }
+            });
     }
 });
 
 process.once('exit', function () {
-    if(!TEST_OK) process.exit(1);
+    if (!TEST_OK) {
+        process.exit(1);
+    }
 });
-

http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/aa2f08ba/src/build/makers/android.js
----------------------------------------------------------------------
diff --git a/src/build/makers/android.js b/src/build/makers/android.js
index 499ccc6..90886a1 100644
--- a/src/build/makers/android.js
+++ b/src/build/makers/android.js
@@ -1,77 +1,136 @@
 var shell        = require('shelljs'),
     path         = require('path'),
     error_writer = require('./error_writer'),
-    n            = require('ncallbacks'),
-    deploy       = require('./android/deploy'),
-    scan         = require('./android/devices'),
     fs           = require('fs'),
-    mspec        = require('./mobile_spec');
+    q            = require('q'),
+    testRunner   = require('./testRunner'),
+    device       = require('../../../../mobilespec/platforms/android/cordova/lib/device'),
+    emulator     = require('../../../../mobilespec/platforms/android/cordova/lib/emulator');
+
+module.exports = function (output, sha, couchdb_cfg, test_timeout) {
+
+    var noBuildMarker = '<!-- no build marker -->',
+        manifestFile = path.join('platforms', 'android', 'bin', 'AndroidManifest.xml'),
+        mobilespecPath = path.join(output, '..', '..');
 
-module.exports = function(output, sha, devices, entry_point, couchdb_cfg, callback) {
     function log(msg) {
         console.log('[ANDROID] ' + msg + ' (sha: ' + sha + ')');
     }
-      try {
-         // make sure android app got created first.
-         if (!fs.existsSync(output)) {
-              throw new Error('create must have failed as output path does not exist.');
-         }
-         var mspec_out = path.join(output, 'assets', 'www');
-         // add the medic configuration (sha,host) to destination folder
-         var medic_config='{"sha":"'+sha+'","couchdb":"'+couchdb_cfg.host+'","couchdbext":"'+couchdb_cfg.exthost+'"}';
-         fs.writeFileSync(path.join(output, 'assets', 'www','autotest','pages', 'medic.json'),medic_config,'utf-8');
-         log('Modifying Cordova android application.');
 
-         var configFile = path.join(output, 'res', 'xml', 'config.xml');
-         fs.writeFileSync(configFile, fs.readFileSync(configFile, 'utf-8').replace(/<content\s*src=".*"/gi, '<content src="' +entry_point + '"'), 'utf-8');
-         // make sure the couch db server is whitelisted
-         fs.writeFileSync(configFile, fs.readFileSync(configFile, 'utf-8').replace(/<access origin="http:..audio.ibeat.org" *.>/gi,'<access origin="http://audio.ibeat.org" /><access origin="'+couchdb_cfg.host+'" />', 'utf-8'));
-     } catch (e) {
-         error_writer('android', sha, 'Exception thrown modifying Android mobile spec application.', e.message);
-         callback(true);
-         return;
-     }
-     var pkgname= 'MainActivity';
-                    // compile
-                    log('Compiling.');
-                    var buildCmd = 'cd ' + output + ' && '+path.join('.','cordova','build')+' --debug';
-                    log('shell exec ' + buildCmd);
-                    shell.exec(buildCmd, {silent:true,async:true},function(code, compile_output) {
-                        log('Compile exit:'+code);
-                        if (code > 0) {
-                            error_writer('android', sha, 'Compilation error', compile_output);
-                            callback(true);
-                        } else {
-                            var binary_path = path.join(output, 'bin', pkgname+'-debug.apk');
-                            if( !fs.existsSync(binary_path)){
-                              binary_path=path.join(output, 'ant-build', pkgname+'-debug.apk');
-                            }
-                            // gradle apk
-                            if( !fs.existsSync(binary_path)){
-                              binary_path=path.join(output, 'build', 'outputs', 'apk', 'android-debug.apk');
-                            }
-                            log("binary path " + binary_path);
-                            var package = 'org.apache.mobilespec';
-                            if (devices) {
-                                // already have a specific set of devices to deploy to
-                                log('deploying to provided devices:'+devices);
-                                deploy(sha, devices, binary_path, package, callback);
-                            } else {
-                                // get list of connected devices
-                                scan(function(err, devices) {
-                                    if (err) {
-                                        // Could not obtain device list...
-                                        var error_message = devices;
-                                        log(error_message);
-                                        callback(true);
-                                    } else {
-                                        log('deploying to discovered devices:'+devices);
-                                        deploy(sha, devices, binary_path, package, callback);
-                                    }
-                                });
-                            }
-                        }
-                    });
+    function prepareMobileSpec() {
+        var d = q.defer();
+        try {
+            // make sure android app got created first.
+            if (!fs.existsSync(output)) {
+                throw new Error('create must have failed as output path does not exist.');
+            }
+            // add the medic configuration (sha, host) to destination folder
+            var medic_config = '{"sha":"' + sha + '","couchdb":"' + couchdb_cfg.host + '","couchdbext":"' + couchdb_cfg.exthost + '"}';
+            fs.writeFileSync(path.join(output, '..', '..', 'www', 'medic.json'), medic_config, 'utf-8');
+            d.resolve();
+        } catch (e) {
+            error_writer('android', sha, 'Exception thrown modifying Android mobile spec application.', e.message);
+            d.reject();
+        }
+        return d.promise;
+    }
 
-}
+    function build() {
+        var d = q.defer(),
+            build_cmd = path.join('..', 'cordova-cli', 'bin', 'cordova') + ' build -- --debug --ant';
+
+        log('Building...');
 
+        shell.exec(build_cmd, {
+            silent : true,
+            async : true
+        }, function (code, output) {
+            if (code > 0) {
+                d.reject('Build error! Exit code: ' + code + ', output: \n ' + output);
+            } else {
+                try {
+                    // appending no-build marker so that we could check later
+                    // that `cordova run -- --nobuild` works
+                    fs.appendFileSync(manifestFile, noBuildMarker, 'utf-8');
+                } catch (err) {
+                    log('Error while appending no-build marker to android manifest');
+                }
+                d.resolve();
+            }
+        });
+
+        return d.promise;
+    }
+
+    // makes sure if there is emulator started
+    // if there isn't, starts one
+    // if there are no emulator images, rejects promise
+    function prepareEmulator() {
+        return emulator.list_images()
+            .then(function (images) {
+                return (!images || images.length === 0) ? q.reject('No emulator images detected, please create one. You can do so by using \'android avd\' command.') : emulator.list_started();
+            })
+            .then(function (started) {
+                if (!started || started.length === 0) {
+                    return emulator.best_image()
+                        .then(function (best) {
+                            shell.cd(path.join(mobilespecPath, '..'));
+                            return emulator.start(best.name);
+                        })
+                        .then(function () {
+                            shell.cd(mobilespecPath);
+                        });
+                }
+            });
+    }
+
+    // makes sure if there is device connected
+    // if there are no devices, starts an emulator
+    function prepareDevice() {
+        return device.list().then(function (device_list) {
+            if (!device_list || device_list.length === 0) {
+                return prepareEmulator();
+            }
+        });
+    }
+
+    function run() {
+        var d = q.defer();
+        log('Running app...');
+        var cmd = path.join('..', 'cordova-cli', 'bin', 'cordova') + ' run android -- --nobuild';
+        shell.exec(cmd, {
+            silent : true,
+            async : true
+        }, function (code, output) {
+            if ((code > 0) || (output.indexOf('ERROR') >= 0)) {
+                log('Error launching mobile-spec on android, code: ' + code + ', output:\n' + output);
+                d.reject('Error launching mobile-spec on android');
+            } else {
+                d.resolve();
+            }
+        });
+        return d.promise;
+    }
+
+    function testNoBuild() {
+        try {
+            var manifestContent = fs.readFileSync(manifestFile, 'utf-8');
+            if (manifestContent.indexOf(noBuildMarker) === -1) {
+                throw new Error('NoBuild parameter test failed.');
+            }
+        } catch (err) {
+            log('error in testNoBuild: ' + JSON.stringify(err));
+        }
+    }
+
+    return prepareMobileSpec()
+        .then(function () {
+            shell.cd(mobilespecPath);
+        }).then(build)
+        .then(prepareDevice)
+        .then(run)
+        .then(function () {
+            log('Waiting for tests to complete...');
+            return testRunner.waitTestsCompleted(sha, 1000 * test_timeout);
+        }).then(testNoBuild);
+}

http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/aa2f08ba/src/build/makers/android/deploy.js
----------------------------------------------------------------------
diff --git a/src/build/makers/android/deploy.js b/src/build/makers/android/deploy.js
deleted file mode 100644
index 28f8c5b..0000000
--- a/src/build/makers/android/deploy.js
+++ /dev/null
@@ -1,113 +0,0 @@
-
-/*
-Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
-
-Licensed 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 shell = require('shelljs'),
-    cp    = require('child_process');
-
-function nCallbacks(count, callback) {
-    var n = count;
-    var failed = false;
-    return function (err) {
-        if(!!err) failed=true;
-
-        if (n < 0) callback('called too many times')
-
-        --n
-
-        if (n == 0) callback(failed)
-    }
-}
-
-module.exports = function deploy(sha, devices, path, id, callback) {
-    function log(msg) {
-        console.log('[ANDROID] [DEPLOY] ' + msg + ' (' + sha + ')');
-    }
-    function done() {
-        log('No Android devices connected. Aborting.');
-        callback();
-    }
-    var activity='.MainActivity';
-    var package='org.apache.mobilespec';
-    var component = package+'/'+activity;
-    var count = 0;
-    if (devices === undefined || !devices) done();
-    else {
-        for (var d in devices) if (devices.hasOwnProperty(d)) count++;
-        // deploy and run on each device
-        if (count === 0) done();
-        else {
-            log('Target: ' + count + ' Android(s).');
-            var end = nCallbacks(count, callback);
-            for (var device in devices) if (devices.hasOwnProperty(device)) (function(d) {
-                var cmd = 'adb -s ' + d + ' uninstall ' + id;
-                var uninstall = shell.exec(cmd, {silent:true,async:true},function(code, uninstall_output) {
-                    // NOTE: if uninstall failed with code > 0, keep going.
-                    log('Installing on device ' + d);
-                    cmd = 'adb -s ' + d + ' install -r ' + path;
-                    var install = shell.exec(cmd, {silent:true,async:true},function(code, install_output) {
-                        if (code > 0) {
-                            log('Error installing on device ' + d);
-                            end(true);
-                        } else {
-                            log('Running on device ' + d);
-                            cmd = 'adb -s ' + d + ' shell am start -n ' +component; // id + '/' + id + '.cordovaExample';
-                            var deploy = shell.exec(cmd, {silent:true,async:true},function(code, run_output) {
-                                if (code > 0) {
-                                    log('Error launching mobile-spec on device ' + d + ', continuing.');
-                                     end(true);
-                                } else {
-                                    log('Mobile-spec launched on device ' + d);
-                                    // Clear out logcat buffer for specific device
-                                    shell.exec('adb -s ' + d + ' logcat -c', {silent:true});
-                                    // Wait for mobile-spec to be done.
-                                    var logcat = cp.spawn('adb', ['-s', d, 'logcat']);
-                                    // set a timeout in case mobile-spec doesnt run to the end 
-                                    var timer = setTimeout(function() {
-                                        logcat.kill();
-                                        log('Mobile-spec timed out on ' + d + ', continuing.');
-                                        // TODO: write out an error if it times out
-                                        //error_writer('android', sha, 
-                                        end(true);
-                                    }, 1000 * 60 * 5);
-
-                                    // >>> DONE <<< gets logged when mobile-spec finished everything
-                                    logcat.stdout.on('data', function(stdout) {
-                                        var buf = stdout.toString();
-                                        if (buf.indexOf('[[[ TEST FAILED ]]]') > -1) {
-                                            log('Mobile-spec finished with failure on ' + d);
-                                            clearTimeout(timer);
-                                            logcat.kill();
-                                            end(true);
-                                        } else if (buf.indexOf('>>> DONE <<<') > -1) {
-                                            // kill process and clear timeout
-                                            log('Mobile-spec finished on ' + d);
-                                            clearTimeout(timer);
-                                            logcat.kill();
-                                            end(false);
-                                        } else if(buf.indexOf('Test Results URL')>-1 && buf.indexOf('<<<end test result>>>')>-1) {
-                                            var msg=buf.slice(buf.indexOf('Test Results URL'), buf.indexOf('<<<end test result>>>'))
-                                            console.log(msg);
-                                        }
-                                    });
-                                }
-                            });
-                        }
-                    });
-                });
-            }(device));
-        }
-    }
-};

http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/aa2f08ba/src/build/makers/android/devices.js
----------------------------------------------------------------------
diff --git a/src/build/makers/android/devices.js b/src/build/makers/android/devices.js
deleted file mode 100644
index 76bfe10..0000000
--- a/src/build/makers/android/devices.js
+++ /dev/null
@@ -1,66 +0,0 @@
-
-/*
-Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
-
-Licensed 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 shell = require('shelljs'),
-    n     = require('ncallbacks');
-
-/*
-  device object returned is of the form:
-  {
-      'id':{
-          model:'name',
-          version:'version'
-      }, ...
-  }
- */
-module.exports = function(callback) {
-    // TODO: perhaps add configuration option to specify android sdk tools location
-    shell.exec('adb devices', {silent:true, async:true}, function(code, output) {
-        if (code > 0) {
-            callback(true, 'Could not obtain device list when running `adb devices`');
-        } else {
-            try {
-                var devices = output.split('\n').slice(1);
-                devices = devices.filter(function(d) { return d.length>0 && d.indexOf('daemon') == -1 && d.indexOf('attached') == -1; });
-                devices = devices.map(function(d) { return d.split('\t')[0]; });
-                var devObj = {};
-                var end = n(devices.length, function() {
-                    callback(false, devObj);
-                });
-                devices.forEach(function(id) {
-                    var device_info = 'adb -s ' + id + ' shell cat system/build.prop';
-                    shell.exec(device_info + ' | grep "model"', {silent:true, async:true}, function(kode, ootput) {
-                        if (kode === 0) {
-                            var model = ootput.split('=')[1].split('\r').join('').split('\n').join('');
-                            shell.exec(device_info + ' | grep "build\.version\.release"', {silent:true, async:true}, function(cowed, putout) {
-                                if (cowed === 0) {
-                                    var version = putout.split('=')[1].split('\r').join('').split('\n').join('');
-                                    devObj[id] = {
-                                        model:model,
-                                        version:version
-                                    };
-                                }
-                                end();
-                            });
-                        } else end();
-                    });
-                });
-            } catch(e) {
-                callback(true, 'Error filtering output of `adb devices`');
-            }
-        }
-    });
-}

http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/aa2f08ba/testchecker.js
----------------------------------------------------------------------
diff --git a/testchecker.js b/testchecker.js
index e1465c7..22581db 100644
--- a/testchecker.js
+++ b/testchecker.js
@@ -1,80 +1,77 @@
 var http = require('http'),
-    url = require('url');
-    q = require('q'),
+    url = require('url'),
+    q = require('q');
 
-module.exports = function(sha, dbHost) {
+module.exports = function (sha, dbHost) {
 
-        function getDocumentIdBySha() {
+    function getDocumentIdBySha() {
         var options = {
-                host: url.parse(dbHost).hostname,
-                port: url.parse(dbHost).port,
-                path: '/mobilespec_results/_all_docs?start_key="' + sha + '"&limit=1'
-            },
+            host : url.parse(dbHost).hostname,
+            port : url.parse(dbHost).port,
+            path : '/mobilespec_results/_all_docs?start_key="' + sha + '"&limit=1'
+        },
             resultsDoc = '',
             d = q.defer();
 
-        http.get(options, function(result) {
-            result.on("data", function(chunk) {
+        http.get(options, function (result) {
+            result.on("data", function (chunk) {
                 resultsDoc += chunk.toString();
             });
-
             result.on('end', function () {
                 d.resolve(JSON.parse(resultsDoc).rows[0].id);
             });
-        }).on('error', function(e) {
+        }).on('error', function (e) {
             console.log("Got error: " + e.message);
             d.reject(e);
         });
-        
+
         return d.promise;
     };
-    
-    function getTestResult(resultId) {
 
+    function getTestResult(resultId) {
         var options = {
-                host: url.parse(dbHost).hostname,
-                port: url.parse(dbHost).port,
-                path: '/mobilespec_results/' + resultId
-            },
+            host : url.parse(dbHost).hostname,
+            port : url.parse(dbHost).port,
+            path : '/mobilespec_results/' + resultId
+        },
             d = q.defer(),
             resultsJSON = "",
             failure;
 
-        http.get(options, function(res) {
-            res.on("data", function(chunk) {
+        http.get(options, function (res) {
+            res.on("data", function (chunk) {
                 resultsJSON += chunk;
             });
-
-            res.on('end', function() {
+            res.on('end', function () {
                 d.resolve(JSON.parse(resultsJSON));
             });
-        }).on('error', function(e) {
+        }).on('error', function (e) {
             console.log("Got error: " + e.message);
             d.reject(e)
         });
-        
+
         return d.promise;
     };
-    
+
     function checkFailure(testResult) {
-        if(typeof testResult.mobilespec.failures == "undefined") {
+        if (typeof testResult.mobilespec.failures == "undefined" || testResult.mobilespec.failures === 0) {
             console.log("No failures were detected");
             return true;
         } else {
             console.log("Total failures: " + testResult.mobilespec.failures);
             console.log('Test failures were detected. Open ' + dbHost + '/_utils/document.html?mobilespec_results/' + testResult._id + ' for details');
             console.log("Failing tests:");
-            testResult.mobilespec.results.forEach(function(result) {
-                if(result.status === "failed") {
+            testResult.mobilespec.results.forEach(function (result) {
+                if (result.status === "failed") {
                     console.log(result.fullName);
                 }
             });
-    
+
             return false;
         }
     };
-    
+
     console.log('Starting results verification for ' + sha);
-    
+
     return getDocumentIdBySha().then(getTestResult).then(checkFailure);
-};
\ No newline at end of file
+};

http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/aa2f08ba/updateconfig.js
----------------------------------------------------------------------
diff --git a/updateconfig.js b/updateconfig.js
index f575903..b980bf6 100644
--- a/updateconfig.js
+++ b/updateconfig.js
@@ -1,16 +1,19 @@
 // /usr/bin/env node
 /*jshint node: true*/
 
-var fs   = require('fs'),
-    path = require('path'),
+var fs     = require('fs'),
+    path   = require('path'),
+    argv   = require('optimist').argv,
     config = require('./config');
 
 //get parameters, that should been written to config.xml
 var entry_point = config.app.entry,
     couch_host = config.couchdb.host;
 
-var configFile = path.join('mobilespec', 'config.xml');
-if (!fs.existsSync(configFile)){
+var configFile = path.join('mobilespec', 'config.xml'),
+    cspFile = path.join('mobilespec', 'www', 'csp-incl.js');
+
+if (!fs.existsSync(configFile)) {
     console.log('Config.xml file doesn\'t exists');
     process.exit(2);
 } else {
@@ -19,14 +22,15 @@ if (!fs.existsSync(configFile)){
         // replace/add start page preference
         // check if config.xml already contains <content /> element
         console.log('Setting entry point to ' + entry_point + ' in config.xml');
-        if (configContent.match(/<content\s*src=".*"\s*\/>/gi)){
+        if (configContent.match(/<content\s*src=".*"\s*\/>/gi)) {
             configContent = configContent.replace(
                 /<content\s*src=".*"\s*\/>/gi,
-                '<content src="' + entry_point + '" />');
+                '<content src="' + entry_point + '" />'
+            );
         } else {
             // add entry point to config
             configContent = configContent.split('</widget>').join('') +
-                '    <content src="' + entry_point + '"/>\n</widget>';
+                '    <content src="' + entry_point + '" />\n</widget>';
         }
 
         // add whitelisting rule allow access to couch server
@@ -34,7 +38,39 @@ if (!fs.existsSync(configFile)){
         configContent = configContent.split('</widget>').join('') +
             '    <access origin="' + couch_host + '" />\n</widget>';
 
+        if (argv.android) {
+            console.log('Increasing url loading timeout for android:');
+
+            var timeout = 120000,
+                timeoutRegex = /<preference\s*name\s*=\s*"?loadUrlTimeoutValue"?.*?((\/>)|(>.*?<\/\s*preference>))/i,
+                timeoutTag = '<preference name="loadUrlTimeoutValue" value="' + timeout + '" />',
+                timeoutTagWithPlatform = '    <platform name="android">\n        <preference name="loadUrlTimeoutValue" value="120000" />\n    </platform>\n',
+                platformRegex = /<platform\s*name\s*=\s*"android"\s*>/i,
+                widgetRegex = /<\/s*widget\s*>/i;
+
+            if (timeoutRegex.test(configContent)) {
+                configContent = configContent.replace(timeoutRegex, timeoutTag);
+                console.log('Found \'loadUrlTimeoutValue\' preference, replacing with desired value');
+            } else if (platformRegex.test(configContent)) {
+                var oldPlatformTag = platformRegex.exec(configContent)[0];
+                configContent = configContent.replace(platformRegex, oldPlatformTag + '\n        ' + timeoutTag);
+                console.log('Found platform tag, appending \'loadUrlTimeoutValue\' preference');
+            } else if (widgetRegex.test(configContent)) {
+                var oldWidgetTag = widgetRegex.exec(configContent)[0];
+                configContent = configContent.replace(widgetRegex, timeoutTagWithPlatform + oldWidgetTag);
+                console.log('Didn\'t find platform tag, adding preference with platform tag');
+            } else {
+                console.log('Warning: couldn\'t modify config.xml for android: no <widget> tag found!');
+            }
+        }
+
         fs.writeFileSync(configFile, configContent, 'utf-8');
+
+        // add couchdb address to csp rules
+        console.log('Adding CSP rule for CouchDB host: ' + couch_host);
+        var cspContent = fs.readFileSync(cspFile, 'utf-8');
+        cspContent = cspContent.replace('connect-src', 'connect-src ' + couch_host);
+        fs.writeFileSync(cspFile, cspContent, 'utf-8');
     } catch (e) {
         console.log(e);
         process.exit(2);


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