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/05/18 01:47:22 UTC

[05/12] git commit: added a csproj file parses module, incorporated into action stack, and first pass at wp7 platform handler

added a csproj file parses module, incorporated into action stack, and first pass at wp7 platform handler


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

Branch: refs/heads/master
Commit: b356f593b01745870af37c056723eb06fc3b9d75
Parents: d4d9c24
Author: Fil Maj <ma...@gmail.com>
Authored: Fri May 17 15:10:05 2013 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Fri May 17 16:46:01 2013 -0700

----------------------------------------------------------------------
 src/platforms/wp7.js     |  120 +++++++----------------------------------
 src/util/action-stack.js |   16 ++++--
 src/util/csproj.js       |   41 ++++++++++++++
 3 files changed, 73 insertions(+), 104 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/b356f593/src/platforms/wp7.js
----------------------------------------------------------------------
diff --git a/src/platforms/wp7.js b/src/platforms/wp7.js
index ce38882..01645f0 100644
--- a/src/platforms/wp7.js
+++ b/src/platforms/wp7.js
@@ -17,15 +17,13 @@
  *
 */
 
-var fs = require('fs'),
+var common = require('./common'),
     path = require('path'),
     glob = require('glob'),
-    shell = require('shelljs'),
-    et = require('elementtree'),
-    xml_helpers = require('../util/xml-helpers'),
-    getConfigChanges = require('../util/config-changes'),
-    assetsDir = 'www'; // relative path to project's web assets
+    csproj = require('../util/csproj');
+    xml_helpers = require('../util/xml-helpers');
 
+/*
 var unix_projPath,  //  for use with glob
     projectFilename,//  first csproj returned by glob unix_projPath
     projPath,       //  full path to the project file, including file name
@@ -48,27 +46,6 @@ function initPaths(project_dir, plugin_dir, plugin_et, variables) {
     projectChanges = platformTag.findall('./config-file[@target=".csproj"]');
     hosts = plugin_et.findall('./access');
 }
-
-function install(project_dir, plugin_dir, plugin_et, variables) {
-
-  // move asset files
-  assets && assets.forEach(function (asset) {
-      var srcPath = path.resolve(plugin_dir, asset.attrib['src']);
-      var targetPath = path.resolve(project_dir, assetsDir,  asset.attrib['target']);
-      copyFileSync(srcPath, targetPath);
-  });
-
-  // move source files
-  sourceFiles && sourceFiles.forEach(function (sourceFile) {
-      var srcFilePath = path.resolve(plugin_dir,  sourceFile.attrib['src']);
-      var destDir = path.resolve(project_dir, sourceFile.attrib['target-dir']);
-      var destFilePath = path.resolve(destDir, path.basename(sourceFile.attrib['src']));
-      copyFileSync(srcFilePath, destFilePath);
-  });
-
-  updateConfigXml("install", configFilePath, plugin_et);
-
-  et.register_namespace("csproj", "http://schemas.microsoft.com/developer/msbuild/2003");
   projectChanges && projectChanges.forEach(function (configNode) {
 
     var docStr = fs.readFileSync(projPath,"utf8");
@@ -90,71 +67,6 @@ function install(project_dir, plugin_dir, plugin_et, variables) {
     // save it, and get out
     fs.writeFileSync(projPath, newDocStr);
   });
-}
-
-function updateConfigXml(action, config_path, plugin_et) {
-
-    var hosts = plugin_et.findall('./access');
-    var platformTag = plugin_et.find('./platform[@name="wp7"]');
-    var configChanges = getConfigChanges(platformTag);   
-    var base_config_path = path.basename(config_path);
-        // add whitelist hosts
-    var root = et.Element("config-file");
-        root.attrib['parent'] = '.';
-      
-    hosts && hosts.forEach( function (tag) {
-      root.append(tag);
-    });
-
-    if (root.len()) {
-      var changeList = configChanges[path.basename(config_path)];
-      // if changeList then add to it, otherwise create it.
-      if(changeList) {
-        changeList.push(root);
-      }
-      else {
-        configChanges[path.basename(config_path)] = [root]
-      }
-    }  
-
-    if (configChanges[path.basename(config_path)]) {
-
-          // edit configuration files
-      var xmlDoc = xml_helpers.parseElementtreeSync(config_path)
-      configChanges[base_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 ' + selector + ' in ' + config_path);
-                }
-            } else {
-                if (!xml_helpers.pruneXML(xmlDoc, children, selector)) {
-                    throw new Error('failed to remove children from ' + selector + ' in ' + config_path);
-                }
-            }
-      });
-    }
-
-    fs.writeFileSync(config_path, xmlDoc.write({indent: 4}));
-}
-
-function uninstall(project_dir, plugin_dir, plugin_et, variables) {
-
-   assets && assets.forEach(function (asset) {
-      var targetPath = path.resolve(project_dir, assetsDir,  asset.attrib['target']);
-      shell.rm('-rf', targetPath);
-   });
-
-   sourceFiles && sourceFiles.forEach(function (sourceFile) {
-      var destDir = path.resolve(project_dir, sourceFile.attrib['target-dir']);
-      var destFilePath = path.resolve(destDir, path.basename(sourceFile.attrib['src']));
-      shell.rm('-rf', destFilePath);
-   });
-
-   updateConfigXml("uninstall", configFilePath, plugin_et);
-
-   et.register_namespace("csproj", "http://schemas.microsoft.com/developer/msbuild/2003");
 
    projectChanges && projectChanges.forEach(function (configNode) {
 
@@ -180,8 +92,7 @@ function uninstall(project_dir, plugin_dir, plugin_et, variables) {
       fs.writeFileSync(projPath, docStr);
     }
   });
-}
-
+*/
 module.exports = {
     www_dir:function(project_dir) {
         return path.join(project_dir, 'www');
@@ -189,16 +100,25 @@ module.exports = {
     package_name:function(project_dir) {
         return xml_helpers.parseElementtreeSync(path.join(project_dir, 'Properties', 'WMAppManifest.xml')).find('App').attrib.ProductID;
     },
+    parseWP7ProjectFile:function(project_dir) {
+        var project_files = glob.sync(path.join(project_dir, '*.csproj'));
+        if (project_files.length === 0) {
+            throw new Error('does not appear to be a Windows Phone project (no .csproj file)');
+        }
+        return new csproj(project_files[0]);
+    },
     "source-file":{
-        install:function(source_el, plugin_dir, project_dir, plugin_id) {
-            var dest = path.join(source_el.attrib['target-dir'], path.basename(source_el.attrib['src']));
+        install:function(source_el, plugin_dir, project_dir, plugin_id, project_file) {
+            var dest = path.join('Plugins', plugin_id, source_el.attrib['target-dir'], path.basename(source_el.attrib['src']));
             common.copyFile(plugin_dir, source_el.attrib['src'], project_dir, dest);
+            // add reference to this file to csproj.
+            project_file.addSourceFile(dest);
         },
-        uninstall:function(source_el, project_dir, plugin_id) {
-            var dest = path.join(source_el.attrib['target-dir'], path.basename(source_el.attrib['src']));
+        uninstall:function(source_el, project_dir, plugin_id, project_file) {
+            var dest = path.join('Plugins', plugin_id, source_el.attrib['target-dir'], path.basename(source_el.attrib['src']));
             common.removeFile(project_dir, dest);
+            // remove reference to this file from csproj.
+            project_file.removeSourceFile(dest);
         }
-    },
-    "lib-file":{
     }
 };

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/b356f593/src/util/action-stack.js
----------------------------------------------------------------------
diff --git a/src/util/action-stack.js b/src/util/action-stack.js
index 886ff21..e3b63d8 100644
--- a/src/util/action-stack.js
+++ b/src/util/action-stack.js
@@ -1,4 +1,5 @@
 var ios = require('../platforms/ios'),
+    wp7 = require('../platforms/wp7'),
     fs = require('fs');
 
 function ActionStack() {
@@ -23,15 +24,19 @@ ActionStack.prototype = {
         this.stack.push(tx);
     },
     process:function(platform, project_dir, callback) {
+        var project_files;
+        // parse platform-specific project files once
         if (platform == 'ios') {
-            // parse xcode project files once
-            var project_files = ios.parseIOSProjectFiles(project_dir);
+            project_files = ios.parseIOSProjectFiles(project_dir);
         }
+        if (platform == 'wp7') {
+            project_files = wp7.parseWP7ProjectFile(project_dir);
+        } 
         while(this.stack.length) {
             var action = this.stack.shift();
             var handler = action.handler.run;
             var action_params = action.handler.params;
-            if (platform == 'ios') action_params.push(project_files);
+            if (platform == 'ios' || platform == 'wp7') action_params.push(project_files);
             try {
                 handler.apply(null, action_params);
             } catch(e) {
@@ -42,7 +47,7 @@ ActionStack.prototype = {
                     var undo = this.completed.shift();
                     var revert = undo.reverter.run;
                     var revert_params = undo.reverter.params;
-                    if (platform == 'ios') revert_params.push(project_files);
+                    if (platform == 'ios' || platform == 'wp7') revert_params.push(project_files);
                     try {
                         revert.apply(null, revert_params);
                     } catch(err) {
@@ -60,6 +65,9 @@ ActionStack.prototype = {
             // write out xcodeproj file
             fs.writeFileSync(project_files.pbx, project_files.xcode.writeSync());
         }
+        if (platform == 'wp7') {
+            project_files.write();
+        }
         if (callback) callback();
     }
 };

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/b356f593/src/util/csproj.js
----------------------------------------------------------------------
diff --git a/src/util/csproj.js b/src/util/csproj.js
new file mode 100644
index 0000000..1fbe1b3
--- /dev/null
+++ b/src/util/csproj.js
@@ -0,0 +1,41 @@
+var xml_helpers = require('./xml_helpers'),
+    et = require('elementtree'),
+    fs = require('fs');
+
+function csproj(location) {
+    this.location = location;
+    this.xml = xml_helpers.parseElementtreeSync(location);
+    return this;
+}
+
+csproj.prototype = {
+    write:function() {
+        fs.writeFileSync(this.location, this.xml.write({indent:4}), 'utf-8');
+    },
+    addSourceFile:function(relative_path) {
+        relative_path = relative_path.join('/').join('\\');
+        var item = new et.Element('ItemGroup');
+        var compile = new et.Element('Compile');
+        compile.attrib.Include = relative_path;
+        item.append(compile);
+        this.xml.getroot().append(item);
+    },
+    removeSourceFile:function(relative_path) {
+        relative_path = relative_path.join('/').join('\\');
+        var groups = this.xml.findall('ItemGroup');
+        for (var i = 0, l = groups.length; i < l; i++) {
+            var group = groups[i];
+            var compiles = group.findall('Compile');
+            for (var j = 0, k = compiles.length; j < k; j++) {
+                var compile = compiles[j];
+                if (compile.attrib.Include == relative_path) {
+                    group.remove(0, compile);
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+};
+
+module.exports = csproj;