You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2013/03/04 20:32:51 UTC

[51/91] [abbrv] git commit: updating configuration support

updating configuration support


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

Branch: refs/heads/master
Commit: 9e2b9da8714c9102f4f82d7a77399a7e72c073e6
Parents: 7a4f5f3
Author: Anis Kadri <an...@gmail.com>
Authored: Thu Nov 29 15:30:16 2012 -0800
Committer: Anis Kadri <an...@gmail.com>
Committed: Thu Nov 29 15:30:16 2012 -0800

----------------------------------------------------------------------
 platforms/ios.js |  148 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 97 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/9e2b9da8/platforms/ios.js
----------------------------------------------------------------------
diff --git a/platforms/ios.js b/platforms/ios.js
index c7b174c..88e2da8 100644
--- a/platforms/ios.js
+++ b/platforms/ios.js
@@ -6,56 +6,48 @@ var path = require('path')
   , plist = require('plist')
   , bplist = require('bplist-parser')
   , shell = require('shelljs')
+  , xml_helpers = require(path.join(__dirname, '..', 'util', 'xml-helpers'))
+  , getConfigChanges = require(path.join(__dirname, '..', 'util', 'config-changes'))
   , assetsDir = 'www';    // relative path to project's web assets
 
 exports.handlePlugin = function (action, project_dir, plugin_dir, plugin_et) {
     var plugin_id = plugin_et._root.attrib['id']
       , version = plugin_et._root.attrib['version']
-      , external_hosts = []
       , i = 0
       , matched;
-
     // grab and parse pbxproj
     // we don't want CordovaLib's xcode project
-    var files = glob.sync(project_dir + '/*.xcodeproj/project.pbxproj');
+    var project_files = glob.sync(project_dir + '/*.xcodeproj/project.pbxproj');
     
-    if (!files.length) throw "does not appear to be an xcode project";
-    var pbxPath = files[0];
+    if (!project_files.length) throw "does not appear to be an xcode project";
+    var pbxPath = project_files[0];
 
-    var xcodeproj = xcode.project(files[0]);
+    var xcodeproj = xcode.project(project_files[0]);
     xcodeproj.parseSync();
 
-    // grab and parse plist file
-    files = glob.sync(project_dir + '/**/{PhoneGap,Cordova}.plist');
+    // grab and parse plist file or config.xml
+    var config_files = (glob.sync(project_dir + '/**/{PhoneGap,Cordova}.plist').length == 0 ? 
+                        glob.sync(project_dir + '/**/config.xml') :
+                        glob.sync(project_dir + '/**/{PhoneGap,Cordova}.plist')
+                       );
 
-    if (!files.length) throw "does not appear to be a PhoneGap project";
+    if (!config_files.length) {
+        throw "does not appear to be a PhoneGap project";
+    }
 
-    files = files.filter(function (val) {
+    config_files = config_files.filter(function (val) {
         return !(/^build\//.test(val));
     });
 
-    var plistPath = files[0];
-    var pluginsDir = path.resolve(files[0], '..', 'Plugins');
-    var resourcesDir = path.resolve(files[0], '..', 'Resources');
-
-    // determine if this is a binary or ascii plist and choose the parser
-    // this is temporary until binary support is added to node-plist
-    if( isBinaryPlist(plistPath) ) {
-        pl = bplist;
-    } else {
-        pl = plist; 
-    }
-
-    var plistObj = pl.parseFileSync(plistPath);
+    var pluginsDir = path.resolve(config_files[0], '..', 'Plugins');
+    var resourcesDir = path.resolve(config_files[0], '..', 'Resources');
 
     var assets = plugin_et.findall('./asset'),
-        hosts = plugin_et.findall('./access'),
         platformTag = plugin_et.find('./platform[@name="ios"]'),
         sourceFiles = platformTag.findall('./source-file'),
         headerFiles = platformTag.findall('./header-file'),
         resourceFiles = platformTag.findall('./resource-file'),
-        frameworks = platformTag.findall('./framework'),
-        plistEle = platformTag.find('./plugins-plist');
+        frameworks = platformTag.findall('./framework');
 
     // move asset files into www
     assets.forEach(function (asset) {
@@ -120,7 +112,7 @@ exports.handlePlugin = function (action, project_dir, plugin_dir, plugin_et) {
 
         if (action == 'install') {
             xcodeproj.addResourceFile('Resources/' + path.basename(src));
-            var st = fs.statSync(srcFile);    
+            var st = fs.statSync(srcFile);
             if (st.isDirectory()) {
                 shell.cp('-R', srcFile, resourcesDir);
             } else {
@@ -142,6 +134,46 @@ exports.handlePlugin = function (action, project_dir, plugin_dir, plugin_et) {
         }
     });
 
+    updateConfig(action, config_files[0], plugin_et);
+
+    // write out xcodeproj file
+    fs.writeFileSync(pbxPath, xcodeproj.writeSync());
+}
+
+function getRelativeDir(file) {
+    var targetDir = file.attrib['target-dir'],
+        preserveDirs = file.attrib['preserve-dirs'];
+
+    if (preserveDirs && preserveDirs.toLowerCase() == 'true') {
+        return path.dirname(file.attrib['src']);
+    } else if (targetDir) {
+        return targetDir;
+    } else {
+        return '';
+    }
+}
+
+// determine if a plist file is binary
+function isBinaryPlist(filename) {
+    // I wish there was a synchronous way to read only the first 6 bytes of a
+    // file. This is wasteful :/ 
+    var buf = '' + fs.readFileSync(filename, 'utf8');
+    // binary plists start with a magic header, "bplist"
+    return buf.substring(0, 6) === 'bplist';
+}
+
+function updatePlistFile(action, config_path, plugin_et) {
+    var hosts = plugin_et.findall('./access'),
+        platformTag = plugin_et.find('./platform[@name="ios"]'), // FIXME: can probably do better than this
+        plistEle = platformTag.find('./plugins-plist'),
+        external_hosts = [];
+
+    // determine if this is a binary or ascii plist and choose the parser
+    // this is temporary until binary support is added to node-plist
+    var pl = (isBinaryPlist(config_path) ? bplist : plist);
+
+    var plistObj = pl.parseFileSync(config_path);
+    
     if (action == 'install') {
         // add hosts to whitelist (ExternalHosts) in plist
         hosts.forEach(function(host) {
@@ -157,8 +189,7 @@ exports.handlePlugin = function (action, project_dir, plugin_dir, plugin_et) {
         for(i=0; i < plistObj.ExternalHosts.length;i++) {
             matched = false;
             hosts.forEach(function(host) {
-                if(host === plistObj.ExternalHosts[i])
-                {
+                if(host === plistObj.ExternalHosts[i]) {
                     matched = true;
                 }
             });
@@ -172,32 +203,47 @@ exports.handlePlugin = function (action, project_dir, plugin_dir, plugin_et) {
 
         delete plistObj.Plugins[plistEle.attrib['key']];
     }
-
+    
     // write out plist
-    fs.writeFileSync(plistPath, plist.build(plistObj));
-
-    // write out xcodeproj file
-    fs.writeFileSync(pbxPath, xcodeproj.writeSync());
+    fs.writeFileSync(config_path, plist.build(plistObj));
 }
 
-function getRelativeDir(file) {
-    var targetDir = file.attrib['target-dir'],
-        preserveDirs = file.attrib['preserve-dirs'];
+function updateConfigXml(action, config_path, plugin_et) {
+    var hosts = plugin_et.findall('./access'),
+        platformTag = plugin_et.find('./platform[@name="ios"]'), // FIXME: can probably do better than this
+        plistEle = platformTag.find('./plugins-plist'), // TODO: use this for older that have plugins-plist
+        configChanges = getConfigChanges(platformTag),
+        pl,
+        external_hosts = [];
+    // edit configuration files
+    var xmlDoc = xml_helpers.parseElementtreeSync(config_path),
+        output;
+
+    configChanges[path.basename(config_path)].forEach(function (configNode) {
+        var selector = configNode.attrib["parent"],
+            children = configNode.findall('*');
+
+        if( action == 'install') {
+            if (!xml_helpers.graftXML(xmlDoc, children, selector)) {
+                throw new Error('failed to add children to ' + filename);
+            }
+        } else {
+            if (!xml_helpers.pruneXML(xmlDoc, children, selector)) {
+                throw new Error('failed to remove children from' + filename);
+            }
+        }
+    });
+
+    output = xmlDoc.write({indent: 4});
+    fs.writeFileSync(config_path, output);
 
-    if (preserveDirs && preserveDirs.toLowerCase() == 'true') {
-        return path.dirname(file.attrib['src']);
-    } else if (targetDir) {
-        return targetDir;
-    } else {
-        return '';
-    }
 }
 
-// determine if a plist file is binary
-function isBinaryPlist(filename) {
-    // I wish there was a synchronous way to read only the first 6 bytes of a
-    // file. This is wasteful :/ 
-    var buf = '' + fs.readFileSync(filename, 'utf8');
-    // binary plists start with a magic header, "bplist"
-    return buf.substring(0, 6) === 'bplist';
+// updates plist file and/or config.xml
+function updateConfig(action, config_path, plugin_et) {
+    if(path.basename(config_path) == "config.xml") {
+        updateConfigXml(action, config_path, plugin_et);
+    } else {
+        updatePlistFile(action, config_path, plugin_et);
+    }
 }