cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [08/16] git commit: config-changes.js: Added ConfigFile class to abstract details config files.
Date Wed, 19 Feb 2014 20:32:54 GMT
config-changes.js: Added ConfigFile class to abstract details config files.

For now it is only intended for files in the project_dir, not
for plugin.xml and platform.json.

cordoova create/plugin add file-transfer/prepare
results in identical cordova project dir tree as master (empty diff).


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

Branch: refs/heads/master
Commit: 14965a840ad39662eda45d2387d5e257b49388fd
Parents: 223fbdf
Author: Mark Koudritsky <kamrik@gmail.com>
Authored: Thu Feb 6 14:55:58 2014 -0500
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Wed Feb 19 15:32:19 2014 -0500

----------------------------------------------------------------------
 src/util/config-changes.js | 146 +++++++++++++++++++++++++++++-----------
 1 file changed, 106 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/14965a84/src/util/config-changes.js
----------------------------------------------------------------------
diff --git a/src/util/config-changes.js b/src/util/config-changes.js
index 7413025..795adff 100644
--- a/src/util/config-changes.js
+++ b/src/util/config-changes.js
@@ -342,10 +342,10 @@ function PlatformMunger(platform, project_dir, plugins_dir) {
     this.platform = platform;
     this.project_dir = project_dir;
     this.plugins_dir = plugins_dir;
+    this.config_keeper = new ConfigKeeper();
 }
 
 PlatformMunger.prototype.apply_plugin_changes = apply_plugin_changes;
-
 function apply_plugin_changes(plugin_id, plugin_vars, is_top_level, should_increment, cache)
{
     var self = this;
     var platform_config = module.exports.get_platform_json(self.plugins_dir, self.platform);
@@ -425,7 +425,10 @@ function apply_plugin_changes(plugin_id, plugin_vars, is_top_level, should_incre
                             }
                         } else {
                             // this xml child is new, graft it (only if config file exists)
-                            self.graft_child(file, xml_child, selector);
+                            var config_file = self.config_keeper.get(self.project_dir, self.platform,
file);
+                            if (config_file.exists) {
+                                config_file.graft_child(selector, xml_child);
+                            }
                         }
                     }
                 });
@@ -446,58 +449,121 @@ function apply_plugin_changes(plugin_id, plugin_vars, is_top_level,
should_incre
     if ( pbxproj && pbxproj.needs_write ){
         pbxproj.write();
     }
+    self.config_keeper.save_all();
+}
+
+function ConfigKeeper() {
+    this._cached = {};
 }
 
-// Graft a child node if the corresponding config file exists.
-PlatformMunger.prototype.graft_child = graft_child;
-function graft_child(file, xml_child, selector) {
+ConfigKeeper.prototype.get = ConfigKeeper_get;
+function ConfigKeeper_get(project_dir, platform, file) {
     var self = this;
-    // config file may be in a place not exactly specified in the target
-    var filepath = resolveConfigFilePath(self.project_dir, self.platform, file);
+    var fake_path = path.join(project_dir, platform, file);
+    if (self._cached[fake_path]) {
+        return self._cached[fake_path];
+    }
+    // File was not cached, need to load.
+    var config_file = new ConfigFile(project_dir, platform, file);
+    self._cached[fake_path] = config_file;
+    return config_file;
+}
 
-    if (fs.existsSync(filepath)) {
+ConfigKeeper.prototype.save_all = ConfigKeeper_save_all;
+function ConfigKeeper_save_all() {
+    var self = this;
+    var keys = Object.keys(this._cached);
+    keys.forEach(function (fake_path) {
+        var config_file = self._cached[fake_path];
+        if (config_file.is_changed) config_file.save();
+    });
+}
 
-        // look at ext and do proper config change based on file type
-        var ext = path.extname(filepath);
-        // Windows8 uses an appxmanifest, and wp8 will likely use
-        // the same in a future release
-        // TODO: consider proper xml file detection, via <?xml version='1.0' encoding='utf-8'?>
-        if (ext == '.xml' || ext == '.appxmanifest') {
-            var xml_to_graft = [et.XML(xml_child)];
-            // TODO: could parse the filepath once per unique target instead of on every
change
-            var doc = xml_helpers.parseElementtreeSync(filepath);
-            if (xml_helpers.graftXML(doc, xml_to_graft, selector)) {
-                // were good, write out the file!
-                fs.writeFileSync(filepath, doc.write({indent: 4}), 'utf-8');
-            } else {
-                // uh oh
-                throw new Error('grafting xml at selector "' + selector + '" from "' + filepath
+ '" during config install went bad :(');
-            }
-        } else {
-            // plist file
-            var pl = (isBinaryPlist(filepath) ? bplist : plist);
-            // TODO: could parse the filepath once per unique target instead of on every
change
-            var plistObj = pl.parseFileSync(filepath);
-            if (plist_helpers.graftPLIST(plistObj, xml_child, selector)) {
-                var regExp = new RegExp("<string>[ \t\r\n]+?</string>", "g");
-                fs.writeFileSync(filepath, plist.build(plistObj).replace(regExp, "<string></string>"));
-            } else {
-                throw new Error('grafting to plist "' + filepath + '" during config install
went bad :(');
-            }
+function ConfigFile(project_dir, platform, file_tag) {
+    this.project_dir = project_dir;
+    this.platform = platform;
+    this.file_tag = file_tag;
+    //this.filepath = resolveConfigFilePath(project_dir, platform, file_tag);
+    this.load();
+    // type (xml, plist, csproj??)
+    // filepath
+    // data
+    // raw?
+    // stat_on_last_read
+    this.is_changed = false;
+    // graft_child()
+    // save()
+    // save_if_changed()
+    // reload() ?
+}
+
+
+// GRAFT
+ConfigFile.prototype.graft_child = ConfigFile_graft_child;
+function ConfigFile_graft_child(selector, xml_child) {
+    var self = this;
+    var filepath = self.filepath;
+    var result;
+    if (self.type === 'xml') {
+        var xml_to_graft = [et.XML(xml_child)];
+        result = xml_helpers.graftXML(self.data, xml_to_graft, selector);
+        if ( !result) {
+            throw new Error('grafting xml at selector "' + selector + '" from "' + filepath
+ '" during config install went bad :(');
+        }
+    } else {
+        // plist file
+        var pl = (isBinaryPlist(filepath) ? bplist : plist);
+        result = plist_helpers.graftPLIST(self.data, xml_child, selector);
+        if ( !result ) {
+            throw new Error('grafting to plist "' + filepath + '" during config install went
bad :(');
         }
     }
+    self.is_changed = true;
 }
 
-function ConfigKeeper() {
-    this._cached = {};
+
+//// SAVE
+ConfigFile.prototype.save = ConfigFile_save;
+function ConfigFile_save() {
+    var self = this;
+    if (self.type === 'xml') {
+        fs.writeFileSync(self.filepath, self.data.write({indent: 4}), 'utf-8');
+    } else {
+        // plist
+        var regExp = new RegExp("<string>[ \t\r\n]+?</string>", "g");
+        fs.writeFileSync(self.filepath, plist.build(self.data).replace(regExp, "<string></string>"));
+    }
 }
 
-ConfigKeeper.prototype.get = ConfigKeeper_get;
-function ConfigKeeper_get(project_dir, platform, file) {
+
+//// LOAD
+ConfigFile.prototype.load = ConfigFile_load;
+function ConfigFile_load() {
     var self = this;
+
+    // config file may be in a place not exactly specified in the target
+    var filepath = self.filepath = resolveConfigFilePath(self.project_dir, self.platform,
self.file_tag);
+
+    if (!fs.existsSync(filepath)) {
+        self.exists = false;
+        return;
+    }
+    self.exists = true;
+    var ext = path.extname(filepath);
+    // Windows8 uses an appxmanifest, and wp8 will likely use
+    // the same in a future release
+    if (ext == '.xml' || ext == '.appxmanifest') {
+        self.type = 'xml';
+        self.data = xml_helpers.parseElementtreeSync(filepath);
+    } else {
+        // plist file
+        self.type = 'plist';
+        var pl = (isBinaryPlist(filepath) ? bplist : plist);
+        self.data = pl.parseFileSync(filepath);
+    }
 }
 
-/////////////////////////// END
+/////////////////////////// END 
 
 package.add_plugin_changes = function (platform, project_dir, plugins_dir, plugin_id, plugin_vars,
is_top_level, should_increment, cache) {
     var munger = new PlatformMunger(platform, project_dir, plugins_dir);


Mime
View raw message