From eb5bca9002920359c835281cf8139cedb43655be Mon Sep 17 00:00:00 2001 From: ignisvulpis Date: Sat, 14 Jun 2014 21:17:14 +0200 Subject: [PATCH 1/8] CB-3571: support for element in config.xml on Android --- cordova-lib/src/configparser/ConfigParser.js | 10 ++--- .../src/cordova/metadata/android_parser.js | 43 ++++++++++++++++++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/cordova-lib/src/configparser/ConfigParser.js b/cordova-lib/src/configparser/ConfigParser.js index eb070d645..365da2107 100644 --- a/cordova-lib/src/configparser/ConfigParser.js +++ b/cordova-lib/src/configparser/ConfigParser.js @@ -107,18 +107,18 @@ ConfigParser.prototype = { * @param {String} platform The platform. * @return {Array} Icons for the platform specified. */ - getIcons: function(platform) { - var ret = [], + getIcons: function(platform, elementName) { + var ret = []; iconElements = []; - + var name = (elementName) ? elementName : 'icon'; if (platform) { // platform specific icons - this.doc.findall('platform[@name=\'' + platform + '\']/icon').forEach(function(elt){ + this.doc.findall('platform[@name=\'' + platform + '\']/'+name).forEach(function(elt){ elt.platform = platform; // mark as platform specific icon iconElements.push(elt); }); } // root level icons - iconElements = iconElements.concat(this.doc.findall('icon')); + iconElements = iconElements.concat(this.doc.findall(name)); // parse icon elements iconElements.forEach(function (elt) { var icon = {}; diff --git a/cordova-lib/src/cordova/metadata/android_parser.js b/cordova-lib/src/cordova/metadata/android_parser.js index 6c9cf410a..655e5009d 100644 --- a/cordova-lib/src/cordova/metadata/android_parser.js +++ b/cordova-lib/src/cordova/metadata/android_parser.js @@ -91,13 +91,54 @@ module.exports.prototype = { fs.writeFileSync(this.strings, strings.write({indent: 4}), 'utf-8'); events.emit('verbose', 'Wrote out Android application name to "' + name + '"'); + var projectRoot = util.isCordova(this.path); + + var splashIcons = config.getIcons('android', 'splash'); + // if there are icon elements in config.xml + if (splashIcons) { + events.emit('verbose', "splash icons: " + JSON.stringify(splashIcons)); + var res = path.join(this.path, 'res'); + var dirs = fs.readdirSync(res); + for (var i=0; i Date: Thu, 19 Jun 2014 23:38:05 +0200 Subject: [PATCH 2/8] change indentation from 2 to 4 spaces --- .../src/cordova/metadata/android_parser.js | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/cordova-lib/src/cordova/metadata/android_parser.js b/cordova-lib/src/cordova/metadata/android_parser.js index 655e5009d..5a0083471 100644 --- a/cordova-lib/src/cordova/metadata/android_parser.js +++ b/cordova-lib/src/cordova/metadata/android_parser.js @@ -96,41 +96,41 @@ module.exports.prototype = { var splashIcons = config.getIcons('android', 'splash'); // if there are icon elements in config.xml if (splashIcons) { - events.emit('verbose', "splash icons: " + JSON.stringify(splashIcons)); - var res = path.join(this.path, 'res'); - var dirs = fs.readdirSync(res); - for (var i=0; i Date: Mon, 16 Jun 2014 16:26:02 +0400 Subject: [PATCH 3/8] Adds support for splashscreens to wp8, windows8 and ios platforms Refactor and cleanup android implementation: * Add getSplashScreens method to ConfigParser * Move common splashscreens/icons logic to getStaticResources method * Move findByDensity method to ConfigParser * fix indentation issues * fix lint errors (+1 squashed commits) --- cordova-lib/src/configparser/ConfigParser.js | 109 ++++++++++++------ .../src/cordova/metadata/android_parser.js | 44 +++---- .../src/cordova/metadata/ios_parser.js | 26 ++++- .../src/cordova/metadata/windows8_parser.js | 12 +- .../src/cordova/metadata/wp8_parser.js | 12 +- 5 files changed, 138 insertions(+), 65 deletions(-) diff --git a/cordova-lib/src/configparser/ConfigParser.js b/cordova-lib/src/configparser/ConfigParser.js index 365da2107..7b36d15a1 100644 --- a/cordova-lib/src/configparser/ConfigParser.js +++ b/cordova-lib/src/configparser/ConfigParser.js @@ -103,64 +103,101 @@ ConfigParser.prototype = { return ret; }, /** - * Returns all icons for the platform specified. - * @param {String} platform The platform. - * @return {Array} Icons for the platform specified. + * Returns all resources for the platform specified. + * @param {String} platform The platform. + * @param {string} resourceName Type of static resources to return. + * "icon" and "splash" currently supported. + * @return {Array} Resources for the platform specified. */ - getIcons: function(platform, elementName) { + getStaticResources: function(platform, resourceName) { var ret = []; - iconElements = []; - var name = (elementName) ? elementName : 'icon'; + staticResources = []; if (platform) { // platform specific icons - this.doc.findall('platform[@name=\'' + platform + '\']/'+name).forEach(function(elt){ - elt.platform = platform; // mark as platform specific icon - iconElements.push(elt); + this.doc.findall('platform[@name=\'' + platform + '\']/' + resourceName).forEach(function(elt){ + elt.platform = platform; // mark as platform specific resource + staticResources.push(elt); }); } - // root level icons - iconElements = iconElements.concat(this.doc.findall(name)); - // parse icon elements - iconElements.forEach(function (elt) { - var icon = {}; - icon.src = elt.attrib.src; - icon.density = elt.attrib['density'] || elt.attrib['cdv:density'] || elt.attrib['gap:density']; - icon.platform = elt.platform || null; // null means icon represents default icon (shared between platforms) - icon.width = elt.attrib.width; - icon.height = elt.attrib.height; - // If one of width or Height is undefined, assume they are equal. - icon.width = icon.width || icon.height; - icon.height = icon.height || icon.width; + // root level resources + staticResources = staticResources.concat(this.doc.findall(resourceName)); + // parse resource elements + staticResources.forEach(function (elt) { + var res = {}; + res.src = elt.attrib.src; + res.density = elt.attrib['density'] || elt.attrib['cdv:density'] || elt.attrib['gap:density']; + res.platform = elt.platform || null; // null means icon represents default icon (shared between platforms) + res.width = elt.attrib.width; + res.height = elt.attrib.height; // default icon - if (!icon.width && !icon.height && !icon.density) { - ret.defaultIcon = icon; + if (!res.width && !res.height && !res.density) { + ret.defaultResource = res; } - ret.push(icon); + ret.push(res); }); /** - * Returns icon with specified width and height - * @param {number} w Width of icon - * @param {number} h Height of icon - * @return {Icon} Icon object or null if not found + * Returns resource with specified width and/or height. + * @param {number} width Width of resource. + * @param {number} height Height of resource. + * @return {Resource} Resource object or null if not found. */ - ret.getIconBySize = function(w, h){ - // If only one of width and height is given - // then we assume that they are equal. - var width = w || h, height = h || w; + ret.getBySize = function(width, height) { + if (!width && !height){ + throw "One of width or height must be defined"; + } + for (var idx in this){ + var res = this[idx]; + // If only one of width or height is not specified, use another parameter for comparation + // If both specified, compare both. + if ((!width || (width == res.width)) && + (!height || (height == res.height))){ + return res; + } + } + return null; + }; + + /** + * Returns resource with specified density. + * @param {string} density Density of resource. + * @return {Resource} Resource object or null if not found. + */ + ret.getByDensity = function (density) { for (var idx in this) { - var icon = this[idx]; - if (width == icon.width && height == icon.width) return icon; + if (this[idx].density == density) { + return this[idx]; + } } return null; }; + /** Returns default icons */ ret.getDefault = function() { - return ret.defaultIcon; + return ret.defaultResource; }; return ret; }, + + /** + * Returns all icons for specific platform. + * @param {string} platform Platform name + * @return {Resource[]} Array of icon objects. + */ + getIcons: function(platform) { + return this.getStaticResources(platform, "icon"); + }, + + /** + * Returns all splash images for specific platform. + * @param {string} platform Platform name + * @return {Resource[]} Array of Splash objects. + */ + getSplashScreens: function(platform) { + return this.getStaticResources(platform, "splash"); + }, + /** *This does not check for duplicate feature entries */ diff --git a/cordova-lib/src/cordova/metadata/android_parser.js b/cordova-lib/src/cordova/metadata/android_parser.js index 5a0083471..d5774d8c9 100644 --- a/cordova-lib/src/cordova/metadata/android_parser.js +++ b/cordova-lib/src/cordova/metadata/android_parser.js @@ -93,41 +93,33 @@ module.exports.prototype = { var projectRoot = util.isCordova(this.path); - var splashIcons = config.getIcons('android', 'splash'); - // if there are icon elements in config.xml - if (splashIcons) { - events.emit('verbose', "splash icons: " + JSON.stringify(splashIcons)); + var splashes = config.getSplashScreens('android'); + // if there are "splash" elements in config.xml + if (splashes) { + events.emit('verbose', "splash screens: " + JSON.stringify(splashes)); var res = path.join(this.path, 'res'); var dirs = fs.readdirSync(res); + for (var i=0; i Date: Thu, 19 Jun 2014 18:45:19 -0400 Subject: [PATCH 4/8] CB-6140 Don't allow deletion of platform dependencies plugman dependency check will ignore platform/dependency when removing an installed plugin, which will result in platform level dependencies is uninstalled improperly. github: close #38 --- cordova-lib/src/plugman/uninstall.js | 2 +- cordova-lib/src/plugman/util/dependencies.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cordova-lib/src/plugman/uninstall.js b/cordova-lib/src/plugman/uninstall.js index 69c70fc39..ac4c78733 100644 --- a/cordova-lib/src/plugman/uninstall.js +++ b/cordova-lib/src/plugman/uninstall.js @@ -116,7 +116,7 @@ module.exports.uninstallPlugin = function(id, plugins_dir, options) { // Recursively remove plugins which were installed as dependents (that are not top-level) // optional? var recursive = true; - var toDelete = recursive ? plugin_et.findall('dependency') : []; + var toDelete = recursive ? plugin_et.findall('.//dependency') : []; toDelete = toDelete && toDelete.length ? toDelete.map(function(p) { return p.attrib.id; }) : []; toDelete.push(top_plugin_id); diff --git a/cordova-lib/src/plugman/util/dependencies.js b/cordova-lib/src/plugman/util/dependencies.js index ce9128b51..511dfb69a 100644 --- a/cordova-lib/src/plugman/util/dependencies.js +++ b/cordova-lib/src/plugman/util/dependencies.js @@ -52,7 +52,7 @@ module.exports = package = { tlps.push(plugin_id); var xml = xml_helpers.parseElementtreeSync( package.resolveConfig(plugin_id, plugins_dir) ); - var deps = xml.findall('dependency'); + var deps = xml.findall('.//dependency'); deps && deps.forEach(function(dep) { graph.add(plugin_id, dep.attrib.id); @@ -60,7 +60,7 @@ module.exports = package = { }); Object.keys(json.dependent_plugins).forEach(function(plugin_id) { var xml = xml_helpers.parseElementtreeSync( package.resolveConfig(plugin_id, plugins_dir) ); - var deps = xml.findall('dependency'); + var deps = xml.findall('.//dependency'); deps && deps.forEach(function(dep) { graph.add(plugin_id, dep.attrib.id); }); From af5ab44a6e3f54e6939420efea3db841f9115955 Mon Sep 17 00:00:00 2001 From: Michal Mocny Date: Fri, 20 Jun 2014 13:46:50 -0400 Subject: [PATCH 5/8] CB-6709 Do not create merges/ folder when adding a platform --- cordova-lib/spec-cordova/platform.spec.js | 1 - cordova-lib/src/cordova/platform.js | 5 ----- 2 files changed, 6 deletions(-) diff --git a/cordova-lib/spec-cordova/platform.spec.js b/cordova-lib/spec-cordova/platform.spec.js index d92a50c97..76c9501a4 100644 --- a/cordova-lib/spec-cordova/platform.spec.js +++ b/cordova-lib/spec-cordova/platform.spec.js @@ -101,7 +101,6 @@ describe('platform end-to-end', function() { }).then(function() { // Check the platform add was successful. expect(path.join(project, 'platforms', helpers.testPlatform)).toExist(); - expect(path.join(project, 'merges', helpers.testPlatform)).toExist(); expect(path.join(project, 'platforms', helpers.testPlatform, 'cordova')).toExist(); }).then(fullPlatformList) // Check for it in platform ls. .then(function() { diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js index eefba913c..3e32d5ae1 100644 --- a/cordova-lib/src/cordova/platform.js +++ b/cordova-lib/src/cordova/platform.js @@ -386,10 +386,6 @@ function supports(project_root, name) { return platformParser.check_requirements(project_root); } -function createOverrides(projectRoot, target) { - shell.mkdir('-p', path.join(cordova_util.appDir(projectRoot), 'merges', target)); -} - // Returns a promise. function call_into_create(target, projectRoot, cfg, libDir, template_dir, opts) { var output = path.join(projectRoot, 'platforms', target); @@ -434,7 +430,6 @@ function call_into_create(target, projectRoot, cfg, libDir, template_dir, opts) return require('./cordova').raw.prepare(target); }) .then(function() { - createOverrides(projectRoot, target); // Install all currently installed plugins into this new platform. var plugins_dir = path.join(projectRoot, 'plugins'); var plugins = cordova_util.findPlugins(plugins_dir); From f0588b9b674740e9f091a40cbd85ec9392aa6f6f Mon Sep 17 00:00:00 2001 From: ignisvulpis Date: Fri, 20 Jun 2014 22:25:36 +0200 Subject: [PATCH 6/8] jshinted --- cordova-lib/src/configparser/ConfigParser.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cordova-lib/src/configparser/ConfigParser.js b/cordova-lib/src/configparser/ConfigParser.js index 7b36d15a1..e274afc99 100644 --- a/cordova-lib/src/configparser/ConfigParser.js +++ b/cordova-lib/src/configparser/ConfigParser.js @@ -110,7 +110,7 @@ ConfigParser.prototype = { * @return {Array} Resources for the platform specified. */ getStaticResources: function(platform, resourceName) { - var ret = []; + var ret = [], staticResources = []; if (platform) { // platform specific icons this.doc.findall('platform[@name=\'' + platform + '\']/' + resourceName).forEach(function(elt){ @@ -144,7 +144,7 @@ ConfigParser.prototype = { */ ret.getBySize = function(width, height) { if (!width && !height){ - throw "One of width or height must be defined"; + throw 'One of width or height must be defined'; } for (var idx in this){ var res = this[idx]; @@ -186,7 +186,7 @@ ConfigParser.prototype = { * @return {Resource[]} Array of icon objects. */ getIcons: function(platform) { - return this.getStaticResources(platform, "icon"); + return this.getStaticResources(platform, 'icon'); }, /** @@ -195,7 +195,7 @@ ConfigParser.prototype = { * @return {Resource[]} Array of Splash objects. */ getSplashScreens: function(platform) { - return this.getStaticResources(platform, "splash"); + return this.getStaticResources(platform, 'splash'); }, /** From 08a9f05ce8d2de79d2068a16af1939e9750a389a Mon Sep 17 00:00:00 2001 From: ignisvulpis Date: Sat, 21 Jun 2014 00:00:53 +0200 Subject: [PATCH 7/8] CB-3571: refactoring handleIcons and handleSplashes out in android_parser.js --- .../src/cordova/metadata/android_parser.js | 204 +++++++++--------- 1 file changed, 106 insertions(+), 98 deletions(-) diff --git a/cordova-lib/src/cordova/metadata/android_parser.js b/cordova-lib/src/cordova/metadata/android_parser.js index d5774d8c9..c7ebb1bce 100644 --- a/cordova-lib/src/cordova/metadata/android_parser.js +++ b/cordova-lib/src/cordova/metadata/android_parser.js @@ -77,26 +77,11 @@ module.exports.prototype = { return launchMode; }, - update_from_config:function(config) { - // TODO: share code for this func with Android. Or fix it and remove - // the below JSHint hacks line. - // jshint unused:false, indent:false, undef:true, loopfunc:true, shadow:true, quotmark:false - if (config instanceof ConfigParser) { - } else throw new Error('update_from_config requires a ConfigParser object'); - - // Update app name by editing res/values/strings.xml - var name = config.name(); - var strings = xml.parseElementtreeSync(this.strings); - strings.find('string[@name="app_name"]').text = name; - fs.writeFileSync(this.strings, strings.write({indent: 4}), 'utf-8'); - events.emit('verbose', 'Wrote out Android application name to "' + name + '"'); - - var projectRoot = util.isCordova(this.path); - - var splashes = config.getSplashScreens('android'); + handleSplashes:function(config) { + var resources = config.getSplashScreens('android'); // if there are "splash" elements in config.xml - if (splashes) { - events.emit('verbose', "splash screens: " + JSON.stringify(splashes)); + if (resources) { + events.emit('verbose', 'splash screens: ' + JSON.stringify(resources)); var res = path.join(this.path, 'res'); var dirs = fs.readdirSync(res); @@ -104,119 +89,142 @@ module.exports.prototype = { var filename = dirs[i]; if (filename.indexOf('drawable-') === 0) { var density = filename.substr(9); - var splash = splashes.getByDensity(density); - if (splash) { + var resource = resources.getByDensity(density); + if (resource) { // copy splash screens. Maybe overwrite template splash screen var destfilepath = path.join(res, 'drawable-' + density, 'screen.png'); - events.emit('verbose', "copying splash icon from " + splash.src + " to " + destfilepath); - shell.cp('-f', splash.src, destfilepath); + events.emit('verbose', 'copying splash icon from ' + resource.src + ' to ' + destfilepath); + shell.cp('-f', resource.src, destfilepath); } else { // remove template splash icon if it exists - var templateSplash = path.join(res, 'drawable-' + density, 'screen.png'); + var template = path.join(res, 'drawable-' + density, 'screen.png'); try { - fs.unlink(templateSplash, function() { - events.emit('verbose', "deleted template splash icon: " + templateSplash); - }); + fs.unlinkSync(template); + events.emit('verbose', 'deleted template splash icon: ' + template); } catch(e) { - events.emit('verbose', "fs.unlink("+templateSplash+") threw: " + e); + // ignored. template screen does probably not exist } } } } } + }, + + handleIcons: function(config) { var icons = config.getIcons('android'); // if there are icon elements in config.xml if (icons) { - var haveSeenDefaultIcon = false; - var iconCount = 0; - var android_icons = {}; - var default_icon; - var max_size; - var max_density; - // http://developer.android.com/design/style/iconography.html - var densities = { - "ldpi" : 36, - "mdpi" : 48, - "hdpi" : 72, - "xhdpi" : 96 - }; - for (var i=0; i Date: Sun, 22 Jun 2014 11:24:15 +0200 Subject: [PATCH 8/8] CB-2606: do not copy default icon to other folders than res/drawable --- .../src/cordova/metadata/android_parser.js | 215 +++++++++--------- 1 file changed, 106 insertions(+), 109 deletions(-) diff --git a/cordova-lib/src/cordova/metadata/android_parser.js b/cordova-lib/src/cordova/metadata/android_parser.js index c7ebb1bce..85658d1d7 100644 --- a/cordova-lib/src/cordova/metadata/android_parser.js +++ b/cordova-lib/src/cordova/metadata/android_parser.js @@ -77,135 +77,132 @@ module.exports.prototype = { return launchMode; }, + // remove the default resource name from all drawable folders + // return the array of the densities in this project + deleteDefaultResource:function(name) { + var densities = []; + var res = path.join(this.path, 'res'); + var dirs = fs.readdirSync(res); + + for (var i=0; i