updated cordoova-common to 1.4.0

This commit is contained in:
Steve Gill
2016-07-22 12:24:27 -07:00
parent 39165a8694
commit 081637134f
72 changed files with 1222 additions and 1982 deletions

View File

@@ -28,7 +28,7 @@ Expoeses shared functionality used by [cordova-lib](https://github.com/apache/co
Represents special instance of NodeJS EventEmitter which is intended to be used to post events to cordova-lib and cordova-cli
Usage:
```
```js
var events = require('cordova-common').events;
events.emit('warn', 'Some warning message')
```
@@ -41,7 +41,7 @@ An error class used by Cordova to throw cordova-specific errors. The CordovaErro
Usage:
```
```js
var CordovaError = require('cordova-common').CordovaError;
throw new CordovaError('Some error message', SOME_ERR_CODE);
```
@@ -53,7 +53,7 @@ See [CordovaError](src/CordovaError/CordovaError.js) for supported error codes.
Exposes functionality to deal with cordova project `config.xml` files. For ConfigParser API reference check [ConfigParser Readme](src/ConfigParser/README.md).
Usage:
```
```js
var ConfigParser = require('cordova-common').ConfigParser;
var appConfig = new ConfigParser('path/to/cordova-app/config.xml');
console.log(appconfig.name() + ':' + appConfig.version());
@@ -64,7 +64,7 @@ console.log(appconfig.name() + ':' + appConfig.version());
`PluginInfo` is a wrapper for cordova plugins' `plugin.xml` files. This class may be instantiated directly or via `PluginInfoProvider`. The difference is that `PluginInfoProvider` caches `PluginInfo` instances based on plugin source directory.
Usage:
```
```js
var PluginInfo: require('cordova-common').PluginInfo;
var PluginInfoProvider: require('cordova-common').PluginInfoProvider;
@@ -80,7 +80,7 @@ console.log('The plugin ' + plugin1.id + ' has version ' + plugin1.version)
Utility module for dealing with sequential tasks. Provides a set of tasks that are needed to be done and reverts all tasks that are already completed if one of those tasks fail to complete. Used internally by cordova-lib and platform's plugin installation routines.
Usage:
```
```js
var ActionStack = require('cordova-common').ActionStack;
var stack = new ActionStack()
@@ -104,7 +104,7 @@ stack.process()
Module for spawning child processes with some advanced logic.
Usage:
```
```js
var superspawn = require('cordova-common').superspawn;
superspawn.spawn('adb', ['devices'])
.progress(function(data){
@@ -121,7 +121,7 @@ superspawn.spawn('adb', ['devices'])
A set of utility methods for dealing with xml files.
Usage:
```
```js
var xml = require('cordova-common').xmlHelpers;
var xmlDoc1 = xml.parseElementtreeSync('some/xml/file');

View File

@@ -20,6 +20,9 @@
-->
# Cordova-common Release Notes
### 1.4.0 (Jul 12, 2016)
* [CB-11023](https://issues.apache.org/jira/browse/CB-11023) Add edit-config functionality
### 1.3.0 (May 12, 2016)
* [CB-11259](https://issues.apache.org/jira/browse/CB-11259): Improving prepare and build logging
* [CB-11194](https://issues.apache.org/jira/browse/CB-11194) Improve cordova load time

View File

@@ -1,48 +1,57 @@
{
"_args": [
[
"cordova-common@^1.3.0",
"D:\\Cordova\\cordova-android"
{
"raw": "cordova-common@^1.4.0",
"scope": null,
"escapedName": "cordova-common",
"name": "cordova-common",
"rawSpec": "^1.4.0",
"spec": ">=1.4.0 <2.0.0",
"type": "range"
},
"/Users/steveng/repo/cordova/cordova-android"
]
],
"_from": "cordova-common@>=1.3.0 <2.0.0",
"_id": "cordova-common@1.3.0",
"_from": "cordova-common@>=1.4.0 <2.0.0",
"_id": "cordova-common@1.4.0",
"_inCache": true,
"_installable": true,
"_location": "/cordova-common",
"_nodeVersion": "5.4.1",
"_nodeVersion": "6.3.0",
"_npmOperationalInternal": {
"host": "packages-16-east.internal.npmjs.com",
"tmp": "tmp/cordova-common-1.3.0.tgz_1464130094288_0.48495062021538615"
"tmp": "tmp/cordova-common-1.4.0.tgz_1469092638680_0.9961137105710804"
},
"_npmUser": {
"email": "stevengill97@gmail.com",
"name": "stevegill"
"name": "kotikov.vladimir",
"email": "kotikov.vladimir@gmail.com"
},
"_npmVersion": "3.9.0",
"_npmVersion": "3.10.5",
"_phantomChildren": {},
"_requested": {
"name": "cordova-common",
"raw": "cordova-common@^1.3.0",
"rawSpec": "^1.3.0",
"raw": "cordova-common@^1.4.0",
"scope": null,
"spec": ">=1.3.0 <2.0.0",
"escapedName": "cordova-common",
"name": "cordova-common",
"rawSpec": "^1.4.0",
"spec": ">=1.4.0 <2.0.0",
"type": "range"
},
"_requiredBy": [
"/"
],
"_resolved": "https://registry.npmjs.org/cordova-common/-/cordova-common-1.3.0.tgz",
"_shasum": "f75161f6aa7cef5486fd5d69a3b0a1f628334491",
"_resolved": "http://registry.npmjs.org/cordova-common/-/cordova-common-1.4.0.tgz",
"_shasum": "b3ba73595caa34fe8250ac11f20a4ed44e7c84e4",
"_shrinkwrap": null,
"_spec": "cordova-common@^1.3.0",
"_where": "D:\\Cordova\\cordova-android",
"_spec": "cordova-common@^1.4.0",
"_where": "/Users/steveng/repo/cordova/cordova-android",
"author": {
"name": "Apache Software Foundation"
},
"bugs": {
"email": "dev@cordova.apache.org",
"url": "https://issues.apache.org/jira/browse/CB"
"url": "https://issues.apache.org/jira/browse/CB",
"email": "dev@cordova.apache.org"
},
"contributors": [],
"dependencies": {
@@ -70,8 +79,8 @@
},
"directories": {},
"dist": {
"shasum": "f75161f6aa7cef5486fd5d69a3b0a1f628334491",
"tarball": "https://registry.npmjs.org/cordova-common/-/cordova-common-1.3.0.tgz"
"shasum": "b3ba73595caa34fe8250ac11f20a4ed44e7c84e4",
"tarball": "https://registry.npmjs.org/cordova-common/-/cordova-common-1.4.0.tgz"
},
"engineStrict": true,
"engines": {
@@ -81,28 +90,28 @@
"main": "cordova-common.js",
"maintainers": [
{
"email": "bowserj@apache.org",
"name": "bowserj"
"name": "bowserj",
"email": "bowserj@apache.org"
},
{
"email": "kotikov.vladimir@gmail.com",
"name": "kotikov.vladimir"
"name": "kotikov.vladimir",
"email": "kotikov.vladimir@gmail.com"
},
{
"email": "purplecabbage@gmail.com",
"name": "purplecabbage"
"name": "purplecabbage",
"email": "purplecabbage@gmail.com"
},
{
"email": "shazron@gmail.com",
"name": "shazron"
"name": "shazron",
"email": "shazron@gmail.com"
},
{
"email": "stevengill97@gmail.com",
"name": "stevegill"
"name": "stevegill",
"email": "stevengill97@gmail.com"
},
{
"email": "npmjs@barhams.info",
"name": "timbarham"
"name": "timbarham",
"email": "npmjs@barhams.info"
}
],
"name": "cordova-common",
@@ -118,5 +127,5 @@
"jshint": "node node_modules/jshint/bin/jshint src && node node_modules/jshint/bin/jshint spec",
"test": "npm run jshint && npm run jasmine"
},
"version": "1.3.0"
"version": "1.4.0"
}

View File

@@ -36,9 +36,11 @@ var fs = require('fs'),
et = require('elementtree'),
semver = require('semver'),
events = require('../events'),
ConfigKeeper = require('./ConfigKeeper');
ConfigKeeper = require('./ConfigKeeper'),
CordovaLogger = require('../CordovaLogger');
var mungeutil = require('./munge-util');
var xml_helpers = require('../util/xml-helpers');
exports.PlatformMunger = PlatformMunger;
@@ -95,9 +97,10 @@ function remove_plugin_changes(pluginInfo, is_top_level) {
var plugin_vars = is_top_level ?
platform_config.installed_plugins[pluginInfo.id] :
platform_config.dependent_plugins[pluginInfo.id];
var edit_config_changes = pluginInfo.getEditConfigs(self.platform);
// get config munge, aka how did this plugin change various config files
var config_munge = self.generate_plugin_config_munge(pluginInfo, plugin_vars);
var config_munge = self.generate_plugin_config_munge(pluginInfo, plugin_vars, edit_config_changes);
// global munge looks at all plugins' changes to config files
var global_munge = platform_config.config_munge;
var munge = mungeutil.decrement_munge(global_munge, config_munge);
@@ -125,12 +128,40 @@ function remove_plugin_changes(pluginInfo, is_top_level) {
PlatformMunger.prototype.add_plugin_changes = add_plugin_changes;
function add_plugin_changes(pluginInfo, plugin_vars, is_top_level, should_increment) {
function add_plugin_changes(pluginInfo, plugin_vars, is_top_level, should_increment, plugin_force) {
var self = this;
var platform_config = self.platformJson.root;
var edit_config_changes = pluginInfo.getEditConfigs(self.platform);
var config_munge;
// get config munge, aka how should this plugin change various config files
var config_munge = self.generate_plugin_config_munge(pluginInfo, plugin_vars);
if (!edit_config_changes || edit_config_changes.length === 0) {
// get config munge, aka how should this plugin change various config files
config_munge = self.generate_plugin_config_munge(pluginInfo, plugin_vars);
}
else {
var isConflictingInfo = is_conflicting(edit_config_changes, platform_config.config_munge, self, plugin_force);
if (plugin_force) {
CordovaLogger.get().log(CordovaLogger.WARN, '--force is used. edit-config will overwrite conflicts if any. Conflicting plugins may not work as expected.');
// remove conflicting munges
var conflict_munge = mungeutil.decrement_munge(platform_config.config_munge, isConflictingInfo.conflictingMunge);
for (var conflict_file in conflict_munge.files) {
self.apply_file_munge(conflict_file, conflict_munge.files[conflict_file], /* remove = */ true);
}
// force add new munges
config_munge = self.generate_plugin_config_munge(pluginInfo, plugin_vars, edit_config_changes);
}
else if(isConflictingInfo.conflictFound) {
throw new Error('There was a conflict trying to modify attributes with <edit-config> in plugin ' + pluginInfo.id +
'. The conflicting plugin, ' + isConflictingInfo.conflictingPlugin + ', already modified the same attributes. The conflict must be resolved before ' +
pluginInfo.id + ' can be added. You may use --force to add the plugin and overwrite the conflicting attributes.');
}
else {
// no conflicts, will handle edit-config
config_munge = self.generate_plugin_config_munge(pluginInfo, plugin_vars, edit_config_changes);
}
}
// global munge looks at all plugins' changes to config files
// TODO: The should_increment param is only used by cordova-cli and is going away soon.
@@ -186,13 +217,17 @@ function reapply_global_munge () {
// generate_plugin_config_munge
// Generate the munge object from plugin.xml + vars
PlatformMunger.prototype.generate_plugin_config_munge = generate_plugin_config_munge;
function generate_plugin_config_munge(pluginInfo, vars) {
function generate_plugin_config_munge(pluginInfo, vars, edit_config_changes) {
var self = this;
vars = vars || {};
var munge = { files: {} };
var changes = pluginInfo.getConfigFiles(self.platform);
if(edit_config_changes) {
Array.prototype.push.apply(changes, edit_config_changes);
}
// Demux 'package.appxmanifest' into relevant platform-specific appx manifests.
// Only spend the cycles if there are version-specific plugin settings
if (self.platform === 'windows' &&
@@ -291,12 +326,69 @@ function generate_plugin_config_munge(pluginInfo, vars) {
});
}
// 2. add into munge
mungeutil.deep_add(munge, change.target, change.parent, { xml: stringified, count: 1, after: change.after });
if (change.mode) {
mungeutil.deep_add(munge, change.file, change.target, { xml: stringified, count: 1, mode: change.mode, plugin: pluginInfo.id });
}
else {
mungeutil.deep_add(munge, change.target, change.parent, { xml: stringified, count: 1, after: change.after });
}
});
});
return munge;
}
function is_conflicting(editchanges, config_munge, self, force) {
var files = config_munge.files;
var conflictFound = false;
var conflictingMunge = { files: {} };
var conflictingParent;
var conflictingPlugin;
editchanges.forEach(function(editchange) {
if (files[editchange.file]) {
var parents = files[editchange.file].parents;
var target = parents[editchange.target];
// Check if the edit target will resolve to an existing target
if (!target || target.length === 0) {
var file_xml = self.config_keeper.get(self.project_dir, self.platform, editchange.file).data;
var resolveEditTarget = xml_helpers.resolveParent(file_xml, editchange.target);
var resolveTarget;
if (resolveEditTarget) {
for (var parent in parents) {
resolveTarget = xml_helpers.resolveParent(file_xml, parent);
if (resolveEditTarget === resolveTarget) {
conflictingParent = parent;
target = parents[parent];
break;
}
}
}
}
else {
conflictingParent = editchange.target;
}
if (target.length !== 0) {
// conflict has been found, exit and throw an error
conflictFound = true;
if (!force) {
// since there has been modifications to the attributes at this target,
// the current plugin should not modify the attributes
conflictingPlugin = target[0].plugin;
return;
}
// need to find all conflicts when --force is used, track conflicting munges
mungeutil.deep_add(conflictingMunge, editchange.file, conflictingParent, target[0]);
}
}
});
return {conflictFound: conflictFound, conflictingPlugin: conflictingPlugin, conflictingMunge: conflictingMunge};
}
// Go over the prepare queue and apply the config munges for each plugin
// that has been (un)installed.
PlatformMunger.prototype.process = PlatformMunger_process;
@@ -313,7 +405,7 @@ function PlatformMunger_process(plugins_dir) {
// Now handle installation
platform_config.prepare_queue.installed.forEach(function(u) {
var pluginInfo = self.pluginInfoProvider.get(path.join(plugins_dir, u.plugin));
self.add_plugin_changes(pluginInfo, u.vars, u.topLevel, true);
self.add_plugin_changes(pluginInfo, u.vars, u.topLevel, true, u.force);
});
// Empty out installed/ uninstalled queues.

View File

@@ -103,7 +103,16 @@ ConfigFile.prototype.graft_child = function ConfigFile_graft_child(selector, xml
var result;
if (self.type === 'xml') {
var xml_to_graft = [modules.et.XML(xml_child.xml)];
result = modules.xml_helpers.graftXML(self.data, xml_to_graft, selector, xml_child.after);
switch (xml_child.mode) {
case 'merge':
result = modules.xml_helpers.graftXMLMerge(self.data, xml_to_graft, selector, xml_child);
break;
case 'overwrite':
result = modules.xml_helpers.graftXMLOverwrite(self.data, xml_to_graft, selector, xml_child);
break;
default:
result = modules.xml_helpers.graftXML(self.data, xml_to_graft, selector, xml_child.after);
}
if ( !result) {
throw new Error('Unable to graft xml at selector "' + selector + '" from "' + filepath + '" during config install');
}
@@ -123,7 +132,14 @@ ConfigFile.prototype.prune_child = function ConfigFile_prune_child(selector, xml
var result;
if (self.type === 'xml') {
var xml_to_graft = [modules.et.XML(xml_child.xml)];
result = modules.xml_helpers.pruneXML(self.data, xml_to_graft, selector);
switch (xml_child.mode) {
case 'merge':
case 'overwrite':
result = modules.xml_helpers.pruneXMLRestore(self.data, selector, xml_child);
break;
default:
result = modules.xml_helpers.pruneXML(self.data, xml_to_graft, selector);
}
} else {
// plist file
result = modules.plist_helpers.prunePLIST(self.data, xml_child.xml, selector);

View File

@@ -52,6 +52,9 @@ exports.deep_remove = function deep_remove(obj, keys /* or key1, key2 .... */ )
return element.xml == k.xml;
});
if (found) {
if (parentArray[index].oldAttrib) {
k.oldAttrib = _.extend({}, parentArray[index].oldAttrib);
}
found.count -= k.count;
if (found.count > 0) {
return false;

View File

@@ -162,8 +162,8 @@ PlatformJson.prototype.removePluginMetadata = function (pluginInfo) {
return this;
};
PlatformJson.prototype.addInstalledPluginToPrepareQueue = function(pluginDirName, vars, is_top_level) {
this.root.prepare_queue.installed.push({'plugin':pluginDirName, 'vars':vars, 'topLevel':is_top_level});
PlatformJson.prototype.addInstalledPluginToPrepareQueue = function(pluginDirName, vars, is_top_level, force) {
this.root.prepare_queue.installed.push({'plugin':pluginDirName, 'vars':vars, 'topLevel':is_top_level, 'force':force});
};
PlatformJson.prototype.addUninstalledPluginToPrepareQueue = function(pluginId, is_top_level) {
@@ -276,4 +276,3 @@ function ModuleMetadata (pluginId, jsModule) {
}
module.exports = PlatformJson;

View File

@@ -146,6 +146,22 @@ function PluginInfo(dirname) {
return configFile;
}
self.getEditConfigs = getEditConfigs;
function getEditConfigs(platform) {
var editConfigs = _getTags(self._et, 'edit-config', platform, _parseEditConfigs);
return editConfigs;
}
function _parseEditConfigs(tag) {
var editConfig =
{ file : tag.attrib['file']
, target : tag.attrib['target']
, mode : tag.attrib['mode']
, xmls : tag.getchildren()
};
return editConfig;
}
// <info> tags, both global and within a <platform>
// TODO (kamrik): Do we ever use <info> under <platform>? Example wanted.
self.getInfo = getInfo;
@@ -382,7 +398,7 @@ function _getTags(pelem, tag, platform, transform) {
return tags;
}
// Same as _getTags() but only looks inside a platfrom section.
// Same as _getTags() but only looks inside a platform section.
function _getTagsInPlatform(pelem, tag, platform, transform) {
var platformTag = pelem.find('./platform[@name="' + platform + '"]');
var tags = platformTag ? platformTag.findall(tag) : [];

View File

@@ -123,7 +123,7 @@ PluginManager.prototype.doOperation = function (operation, plugin, options) {
if (operation === PluginManager.INSTALL) {
// Ignore passed `is_top_level` option since platform itself doesn't know
// anything about managing dependencies - it's responsibility of caller.
self.munger.add_plugin_changes(plugin, options.variables, /*is_top_level=*/true, /*should_increment=*/true);
self.munger.add_plugin_changes(plugin, options.variables, /*is_top_level=*/true, /*should_increment=*/true, options.force);
self.munger.platformJson.addPluginMetadata(plugin);
} else {
self.munger.remove_plugin_changes(plugin, /*is_top_level=*/true);

View File

@@ -29,6 +29,9 @@ var fs = require('fs')
, et = require('elementtree')
;
var ROOT = /^\/([^\/]*)/,
ABSOLUTE = /^\/([^\/]*)\/(.*)/;
module.exports = {
// compare two et.XML nodes, see if they match
// compares tagName, text, attributes and children (recursively)
@@ -68,7 +71,7 @@ module.exports = {
// adds node to doc at selector, creating parent if it doesn't exist
graftXML: function(doc, nodes, selector, after) {
var parent = resolveParent(doc, selector);
var parent = module.exports.resolveParent(doc, selector);
if (!parent) {
//Try to create the parent recursively if necessary
try {
@@ -79,7 +82,7 @@ module.exports = {
} catch (e) {
return false;
}
parent = resolveParent(doc, selector);
parent = module.exports.resolveParent(doc, selector);
if (!parent) return false;
}
@@ -97,9 +100,54 @@ module.exports = {
return true;
},
// adds new attributes to doc at selector
// Will only merge if attribute has not been modified already or --force is used
graftXMLMerge: function(doc, nodes, selector, xml) {
var target = module.exports.resolveParent(doc, selector);
if (!target) return false;
// saves the attributes of the original xml before making changes
xml.oldAttrib = _.extend({}, target.attrib);
nodes.forEach(function (node) {
var attributes = node.attrib;
for (var attribute in attributes) {
target.attrib[attribute] = node.attrib[attribute];
}
});
return true;
},
// overwrite all attributes to doc at selector with new attributes
// Will only overwrite if attribute has not been modified already or --force is used
graftXMLOverwrite: function(doc, nodes, selector, xml) {
var target = module.exports.resolveParent(doc, selector);
if (!target) return false;
// saves the attributes of the original xml before making changes
xml.oldAttrib = _.extend({}, target.attrib);
// remove old attributes from target
var targetAttributes = target.attrib;
for (var targetAttribute in targetAttributes) {
delete targetAttributes[targetAttribute];
}
// add new attributes to target
nodes.forEach(function (node) {
var attributes = node.attrib;
for (var attribute in attributes) {
target.attrib[attribute] = node.attrib[attribute];
}
});
return true;
},
// removes node from doc at selector
pruneXML: function(doc, nodes, selector) {
var parent = resolveParent(doc, selector);
var parent = module.exports.resolveParent(doc, selector);
if (!parent) return false;
nodes.forEach(function (node) {
@@ -114,6 +162,19 @@ module.exports = {
return true;
},
// restores attributes from doc at selector
pruneXMLRestore: function(doc, selector, xml) {
var target = module.exports.resolveParent(doc, selector);
if (!target) return false;
if (xml.oldAttrib) {
target.attrib = _.extend({}, xml.oldAttrib);
}
return true;
},
parseElementtreeSync: function (filename) {
var contents = fs.readFileSync(filename, 'utf-8');
if(contents) {
@@ -121,6 +182,30 @@ module.exports = {
contents = contents.substring(contents.indexOf('<'));
}
return new et.ElementTree(et.XML(contents));
},
resolveParent: function (doc, selector) {
var parent, tagName, subSelector;
// handle absolute selector (which elementtree doesn't like)
if (ROOT.test(selector)) {
tagName = selector.match(ROOT)[1];
// test for wildcard "any-tag" root selector
if (tagName == '*' || tagName === doc._root.tag) {
parent = doc._root;
// could be an absolute path, but not selecting the root
if (ABSOLUTE.test(selector)) {
subSelector = selector.match(ABSOLUTE)[2];
parent = parent.find(subSelector);
}
} else {
return false;
}
} else {
parent = doc.find(selector);
}
return parent;
}
};
@@ -152,33 +237,6 @@ function uniqueChild(node, parent) {
}
}
var ROOT = /^\/([^\/]*)/,
ABSOLUTE = /^\/([^\/]*)\/(.*)/;
function resolveParent(doc, selector) {
var parent, tagName, subSelector;
// handle absolute selector (which elementtree doesn't like)
if (ROOT.test(selector)) {
tagName = selector.match(ROOT)[1];
// test for wildcard "any-tag" root selector
if (tagName == '*' || tagName === doc._root.tag) {
parent = doc._root;
// could be an absolute path, but not selecting the root
if (ABSOLUTE.test(selector)) {
subSelector = selector.match(ABSOLUTE)[2];
parent = parent.find(subSelector);
}
} else {
return false;
}
} else {
parent = doc.find(selector);
}
return parent;
}
// Find the index at which to insert an entry. After is a ;-separated priority list
// of tags after which the insertion should be made. E.g. If we need to
// insert an element C, and the rule is that the order of children has to be
@@ -257,19 +315,19 @@ function mergeXml(src, dest, platform, clobber) {
dest.append(destChild);
}
}
function removeDuplicatePreferences(xml) {
// reduce preference tags to a hashtable to remove dupes
var prefHash = xml.findall('preference[@name][@value]').reduce(function(previousValue, currentValue) {
previousValue[ currentValue.attrib.name ] = currentValue.attrib.value;
return previousValue;
}, {});
// remove all preferences
xml.findall('preference[@name][@value]').forEach(function(pref) {
xml.remove(pref);
});
// write new preferences
Object.keys(prefHash).forEach(function(key, index) {
var element = et.SubElement(xml, 'preference');