Repository: cordova-lib Updated Branches: refs/heads/master 71e7ba31e -> 85974b0f9
Extract AndroidProject class into a separate .js file Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/85974b0f Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/85974b0f Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/85974b0f Branch: refs/heads/master Commit: 85974b0f9bac6769e3f0fe4e4a61a84f554b6e5d Parents: 513967d Author: Martin Bektchiev <[email protected]> Authored: Tue Jun 3 14:19:59 2014 +0300 Committer: Mark Koudritsky <[email protected]> Committed: Tue Jun 3 14:44:03 2014 -0400 ---------------------------------------------------------------------- .../spec-plugman/platforms/android.spec.js | 10 ++- cordova-lib/src/plugman/platforms/android.js | 75 +--------------- cordova-lib/src/plugman/util/android-project.js | 93 ++++++++++++++++++++ 3 files changed, 102 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/85974b0f/cordova-lib/spec-plugman/platforms/android.spec.js ---------------------------------------------------------------------- diff --git a/cordova-lib/spec-plugman/platforms/android.spec.js b/cordova-lib/spec-plugman/platforms/android.spec.js index df11ec7..2713a7b 100644 --- a/cordova-lib/spec-plugman/platforms/android.spec.js +++ b/cordova-lib/spec-plugman/platforms/android.spec.js @@ -1,4 +1,5 @@ var android = require('../../src/plugman/platforms/android'), + android_project = require('../../src/plugman/util/android-project'), common = require('../../src/plugman/platforms/common'), install = require('../../src/plugman/install'), path = require('path'), @@ -173,7 +174,7 @@ describe('android project handler', function() { android['framework'].install(frameworkElement, dummyplugin, temp); android.parseProjectFile(temp).write(); - var relativePath = android.getRelativeLibraryPath(temp, subDir); + var relativePath = android_project.getRelativeLibraryPath(temp, subDir); expect(_.any(writeFileSync.argsForCall, function (callArgs) { return callArgs[0] === mainProjectPropsFile && callArgs[1].indexOf('\nandroid.library.reference.3=' + relativePath + '\n') > -1; })).toBe(true, 'Reference to library not added'); @@ -223,11 +224,14 @@ describe('android project handler', function() { }); }); describe('of <framework> elements', function() { - it('should remove library reference from the main project', function() { + afterEach(function () { + android.purgeProjectFileCache(temp); + }); + it('should remove library reference from the main project', function () { var frameworkElement = { attrib: { src: "LibraryPath" } }; var sub_dir = path.resolve(temp, frameworkElement.attrib.src); var mainProjectProps = path.resolve(temp, "project.properties"); - var existsSync = spyOn( fs, 'existsSync').andReturn(true); + var existsSync = spyOn(fs, 'existsSync').andReturn(true); var writeFileSync = spyOn(fs, 'writeFileSync'); var readFileSync = spyOn(fs, 'readFileSync').andCallFake(function (file) { if (path.normalize(file) === mainProjectProps) http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/85974b0f/cordova-lib/src/plugman/platforms/android.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/plugman/platforms/android.js b/cordova-lib/src/plugman/platforms/android.js index e105c5b..06aee30 100644 --- a/cordova-lib/src/plugman/platforms/android.js +++ b/cordova-lib/src/plugman/platforms/android.js @@ -23,7 +23,7 @@ var fs = require('fs') // use existsSync in 0.6.x , events = require('../events') , xml_helpers = require(path.join(__dirname, '..', '..', 'util', 'xml-helpers')) , properties_parser = require('properties-parser') - , shell = require('shelljs'); + , android_project = require('../util/android-project'); var projectFileCache = {}; @@ -115,84 +115,13 @@ module.exports = { }, parseProjectFile: function(project_dir){ if (!projectFileCache[project_dir]) { - projectFileCache[project_dir] = { - propertiesEditors: {}, - subProjectDirs : {}, - addSubProject: function(parentDir, subDir) { - var subProjectFile = path.resolve(subDir, "project.properties"); - if (!fs.existsSync(subProjectFile)) throw new Error('cannot find "' + subProjectFile + '" referenced in <framework>'); - - var parentProjectFile = path.resolve(parentDir, "project.properties"); - var parentProperties = this._getPropertiesFile(parentProjectFile); - addLibraryReference(parentProperties, module.exports.getRelativeLibraryPath(parentDir, subDir)); - - var subProperties = this._getPropertiesFile(subProjectFile); - subProperties.set("target", parentProperties.get("target")); - - this.subProjectDirs[subDir] = true; - this._dirty = true; - }, - removeSubProject: function(parentDir, subDir) { - var parentProjectFile = path.resolve(parentDir, "project.properties"); - var parentProperties = this._getPropertiesFile(parentProjectFile); - removeLibraryReference(parentProperties, module.exports.getRelativeLibraryPath(parentDir, subDir)); - delete this.subProjectDirs[subDir]; - this._dirty = true; - }, - write: function () { - if (!this._dirty) return; - - for (var filename in this.propertiesEditors) { - fs.writeFileSync(filename, this.propertiesEditors[filename].toString()); - } - - for (var sub_dir in this.subProjectDirs) - { - shell.exec("android update lib-project --path " + sub_dir); - } - this._dirty = false; - }, - _dirty : false, - _getPropertiesFile: function (filename) { - if (!this.propertiesEditors[filename]) - this.propertiesEditors[filename] = properties_parser.createEditor(filename); - - return this.propertiesEditors[filename]; - } - }; + projectFileCache[project_dir] = new android_project.AndroidProject(); } return projectFileCache[project_dir]; }, purgeProjectFileCache:function(project_dir) { delete projectFileCache[project_dir]; - }, - getRelativeLibraryPath: function (parentDir, subDir) { - var libraryPath = path.relative(parentDir, subDir); - return (path.sep == '\\') ? libraryPath.replace(/\\/g, '/') : libraryPath; } }; -function addLibraryReference(projectProperties, libraryPath) { - var i = 1; - while (projectProperties.get("android.library.reference." + i)) - i++; - - projectProperties.set("android.library.reference." + i, libraryPath); -} - -function removeLibraryReference(projectProperties, libraryPath) { - var i = 1; - var currentLib; - while (currentLib = projectProperties.get("android.library.reference." + i)) { - if (currentLib === libraryPath) { - while (currentLib = projectProperties.get("android.library.reference." + (i + 1))) { - projectProperties.set("android.library.reference." + i, currentLib); - i++; - } - projectProperties.set("android.library.reference." + i); - break; - } - i++; - } -} http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/85974b0f/cordova-lib/src/plugman/util/android-project.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/plugman/util/android-project.js b/cordova-lib/src/plugman/util/android-project.js new file mode 100644 index 0000000..b30748b --- /dev/null +++ b/cordova-lib/src/plugman/util/android-project.js @@ -0,0 +1,93 @@ +/* + Helper for Android projects configuration +*/ + +var fs = require('fs'), + path = require('path'), + properties_parser = require('properties-parser'), + shell = require('shelljs'); + + +function addLibraryReference(projectProperties, libraryPath) { + var i = 1; + while (projectProperties.get("android.library.reference." + i)) + i++; + + projectProperties.set("android.library.reference." + i, libraryPath); +} + +function removeLibraryReference(projectProperties, libraryPath) { + var i = 1; + var currentLib; + while (currentLib = projectProperties.get("android.library.reference." + i)) { + if (currentLib === libraryPath) { + while (currentLib = projectProperties.get("android.library.reference." + (i + 1))) { + projectProperties.set("android.library.reference." + i, currentLib); + i++; + } + projectProperties.set("android.library.reference." + i); + break; + } + i++; + } +} + +function AndroidProject() { + this._propertiesEditors = {}; + this._subProjectDirs = {}; + this._dirty = false; + + return this; +} + +AndroidProject.prototype = { + addSubProject: function(parentDir, subDir) { + var subProjectFile = path.resolve(subDir, "project.properties"); + if (!fs.existsSync(subProjectFile)) throw new Error('cannot find "' + subProjectFile + '" referenced in <framework>'); + + var parentProjectFile = path.resolve(parentDir, "project.properties"); + var parentProperties = this._getPropertiesFile(parentProjectFile); + addLibraryReference(parentProperties, module.exports.getRelativeLibraryPath(parentDir, subDir)); + + var subProperties = this._getPropertiesFile(subProjectFile); + subProperties.set("target", parentProperties.get("target")); + + this._subProjectDirs[subDir] = true; + this._dirty = true; + }, + removeSubProject: function(parentDir, subDir) { + var parentProjectFile = path.resolve(parentDir, "project.properties"); + var parentProperties = this._getPropertiesFile(parentProjectFile); + removeLibraryReference(parentProperties, module.exports.getRelativeLibraryPath(parentDir, subDir)); + delete this._subProjectDirs[subDir]; + this._dirty = true; + }, + write: function () { + if (!this._dirty) return; + + for (var filename in this._propertiesEditors) { + fs.writeFileSync(filename, this._propertiesEditors[filename].toString()); + } + + for (var sub_dir in this._subProjectDirs) + { + shell.exec("android update lib-project --path " + sub_dir); + } + this._dirty = false; + }, + _getPropertiesFile: function (filename) { + if (!this._propertiesEditors[filename]) + this._propertiesEditors[filename] = properties_parser.createEditor(filename); + + return this._propertiesEditors[filename]; + } +}; + + +module.exports = { + AndroidProject: AndroidProject, + getRelativeLibraryPath: function (parentDir, subDir) { + var libraryPath = path.relative(parentDir, subDir); + return (path.sep == '\\') ? libraryPath.replace(/\\/g, '/') : libraryPath; + } +};
