You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ge...@apache.org on 2015/02/23 23:29:55 UTC

[3/8] cordova-lib git commit: restore plugins and platforms on prepare

restore plugins and platforms on prepare

Collect the restore logic to restore-util.js and uses it from prepare to restore plugins and
platforms during prepare


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

Branch: refs/heads/master
Commit: 8a313a083bb2c4e39d3230ecbe090d7852bf5454
Parents: 086e1c7
Author: Gorkem Ercan <go...@gmail.com>
Authored: Mon Feb 9 19:50:43 2015 -0500
Committer: Gorkem Ercan <go...@gmail.com>
Committed: Thu Feb 19 12:53:00 2015 -0500

----------------------------------------------------------------------
 cordova-lib/src/cordova/prepare.js      |  28 ++++---
 cordova-lib/src/cordova/restore-util.js | 114 +++++++++++++++++++++++++++
 2 files changed, 132 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/8a313a08/cordova-lib/src/cordova/prepare.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/prepare.js b/cordova-lib/src/cordova/prepare.js
index e0c1a66..678d8ae 100644
--- a/cordova-lib/src/cordova/prepare.js
+++ b/cordova-lib/src/cordova/prepare.js
@@ -24,12 +24,14 @@ var cordova_util      = require('./util'),
     fs                = require('fs'),
     shell             = require('shelljs'),
     et                = require('elementtree'),
-    HooksRunner            = require('../hooks/HooksRunner'),
+    HooksRunner       = require('../hooks/HooksRunner'),
     events            = require('../events'),
     Q                 = require('q'),
     plugman           = require('../plugman/plugman'),
     PlatformMunger    = require('../plugman/util/config-changes').PlatformMunger,
-    PlatformJson      = require('../plugman/util/PlatformJson');
+    PlatformJson      = require('../plugman/util/PlatformJson'),
+    restore           = require('./restore-util');
+    
 
 var PluginInfoProvider = require('../PluginInfoProvider');
 
@@ -47,17 +49,23 @@ function prepare(options) {
         };
     }
 
-    options = cordova_util.preProcessOptions(options);
-
-    var paths = options.platforms.map(function(p) {
+    var hooksRunner = new HooksRunner(projectRoot);
+    return hooksRunner.fire('before_prepare', options)
+    .then(function(){
+      return restore.installPlatformsFromConfigXML(options.platforms);
+    })
+    .then(function(){
+      options = cordova_util.preProcessOptions(options);
+      var paths = options.platforms.map(function(p) {
         var platform_path = path.join(projectRoot, 'platforms', p);
         var parser = (new platforms[p].parser(platform_path));
         return parser.www_dir();
-    });
-    options.paths = paths;
-
-    var hooksRunner = new HooksRunner(projectRoot);
-    return hooksRunner.fire('before_prepare', options)
+      });
+      options.paths = paths;
+    })
+    .then(function(){
+      return restore.installPluginsFromConfigXML(options);
+    })
     .then(function() {
         var pluginInfoProvider = new PluginInfoProvider();
 

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/8a313a08/cordova-lib/src/cordova/restore-util.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/restore-util.js b/cordova-lib/src/cordova/restore-util.js
new file mode 100644
index 0000000..9b32853
--- /dev/null
+++ b/cordova-lib/src/cordova/restore-util.js
@@ -0,0 +1,114 @@
+/**
+    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'),
+    ConfigParser     = require('../configparser/ConfigParser'),
+    path             = require('path'),
+    Q                = require('q'),
+    fs               = require('fs'),
+    plugin           = require('./plugin'),
+    events           = require('../events'),
+    cordova          = require('./cordova');
+
+exports.installPluginsFromConfigXML = installPluginsFromConfigXML;
+exports.installPlatformsFromConfigXML = installPlatformsFromConfigXML;
+
+
+function installPlatformsFromConfigXML(platforms){
+    var projectHome = cordova_util.cdProjectRoot();
+    var configPath = cordova_util.projectConfig(projectHome);
+    var cfg = new ConfigParser(configPath);
+
+    var engines = cfg.getEngines(projectHome);
+    var targets = engines.map(function(engine){
+        var platformPath = path.join(projectHome, 'platforms', engine.id);
+        var platformAlreadyAdded = fs.existsSync(platformPath);
+
+       //if no platforms are specified we skip.
+       if( (platforms && platforms.indexOf(engine.id)> -1 ) && !platformAlreadyAdded ){
+         var t = engine.id;
+         if(engine.version){
+           t += '@'+engine.version;
+         }
+         return t;
+       }
+    });
+
+    if(!targets || !targets.length  ){
+        return Q.all('No platforms are listed in config.xml to restore');
+    }
+    
+    // Run platform add for all the platforms seperately 
+    // so that failure on one does not affect the other.
+    var promises = targets.map(function(target){
+       if(target){
+         events.emit('log', 'Restoring platform '+target+ ' referenced on config.xml');
+         var p = cordova.raw.platform('add',target);
+         return p;
+       }
+       return Q();
+    });
+    return Q.allSettled(promises).then(
+        function (results) {
+            for(var i =0; i<results.length; i++){
+                //log the rejections otherwise they are lost
+                if(results[i].state ==='rejected'){
+                    events.emit('log', results[i].reason.message);
+                }
+            }
+        });
+}
+
+//returns a Promise
+function installPluginsFromConfigXML(args) {
+    //Install plugins that are listed on config.xml
+    var projectRoot = cordova_util.cdProjectRoot();
+    var configPath = cordova_util.projectConfig(projectRoot);
+    var cfg = new ConfigParser(configPath);
+    var plugins_dir = path.join(projectRoot, 'plugins');
+
+    // Get all configured plugins
+    var features = cfg.getFeatureIdList();
+    if (0 === features.length) {
+        return Q.all('No config.xml plugins to install');
+    }
+
+    return features.reduce(function(soFar, featureId) {
+        var pluginPath =  path.join(plugins_dir, featureId);
+        if (fs.existsSync(pluginPath)) {
+            // Plugin already exists
+            return soFar;
+        }
+        return soFar.then(function() {
+            events.emit('log', 'Discovered ' + featureId + ' in config.xml. Installing to the project');
+            var feature = cfg.getFeature(featureId);
+
+            // Install from given URL if defined or using a plugin id
+            var installFrom = feature.url || feature.installPath || feature.id;
+            if( feature.version && !feature.url && !feature.installPath ){
+                installFrom += ('@' + feature.version);
+            }
+            // Add feature preferences as CLI variables if have any
+            var options = {cli_variables: feature.variables, 
+                            searchpath: args.searchpath };
+
+            return plugin('add', installFrom, options);
+        });
+    }, Q());
+}


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