cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject [3/3] cordova-lib git commit: Add a type named "gradleReference" in framework
Date Thu, 06 Nov 2014 19:35:01 GMT
Add a type named "gradleReference" in framework

It's too complex to dependence AAR in plugin, it need build the plugin
to a android project which include AndroidManifest.xml. This patch
reference the *.gradle in plugin to build.gradle in root project. Below is
a demo.
plugin.xml:
<framework src="libs/xwalk_core_library/xwalk.gradle" custom="true" type="gradleReference"/>

xwalk.gradle:
repositories {
    maven {
        url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'
    }
}
dependencies {
    compile 'org.xwalk:xwalk_core_library_beta:9.38.208.4'
}

The build.gradle will add blow section:
// PLUGIN GRADLE EXTENSIONS START
apply from: 'libs/xwalk_core_library/xwalk.gradle'
// PLUGIN GRADLE EXTENSIONS END


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

Branch: refs/heads/CB-3445-2
Commit: ea7b2bf94f0616fc079abb240f4e95b759e2342a
Parents: 1805d4c
Author: fujunwei <junwei.fu@intel.com>
Authored: Fri Oct 24 16:11:59 2014 +0800
Committer: Ian Clelland <iclelland@chromium.org>
Committed: Thu Nov 6 12:07:03 2014 -0500

----------------------------------------------------------------------
 cordova-lib/src/plugman/platforms/android.js    | 15 +++-
 cordova-lib/src/plugman/util/android-project.js | 73 +++++++++++++++++++-
 2 files changed, 85 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/ea7b2bf9/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 2c2c6d9..08804bd 100644
--- a/cordova-lib/src/plugman/platforms/android.js
+++ b/cordova-lib/src/plugman/platforms/android.js
@@ -107,7 +107,13 @@ module.exports = {
             }
 
             var projectConfig = module.exports.parseProjectFile(project_dir);
-            projectConfig.addSubProject(parentDir, subDir);
+            var type = source_el.attrib.type;
+            if (type == 'gradleReference') {
+                //add reference to build.gradle
+                projectConfig.addGradleReference(parentDir, subDir);
+            } else {
+                projectConfig.addSubProject(parentDir, subDir);
+            }
         },
         uninstall:function(source_el, project_dir, plugin_id) {
             var src = source_el.attrib.src;
@@ -130,7 +136,12 @@ module.exports = {
             }
 
             var projectConfig = module.exports.parseProjectFile(project_dir);
-            projectConfig.removeSubProject(parentDir, subDir);
+            var type = source_el.attrib.type;
+            if (type == 'gradleReference') {
+                projectConfig.removeGradleReference(parentDir, subDir);
+            } else {
+                projectConfig.removeSubProject(parentDir, subDir);
+            }
         }
     },
     parseProjectFile: function(project_dir){

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/ea7b2bf9/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
index 2f11f9a..d78f2a3 100644
--- a/cordova-lib/src/plugman/util/android-project.js
+++ b/cordova-lib/src/plugman/util/android-project.js
@@ -114,7 +114,6 @@ function updateGradleLibrariesFile(librariesFile, updateFn) {
         var libraries = fs.readFileSync(librariesFile, {encoding: 'utf8'});
         var lines = libraries.split('\n');
         var openLine, closeLine;
-        var exists = false;
         for (var i = 0; i < lines.length; ++i) {
             var line = lines[i];
             if (line.indexOf('dependencies {') > -1) {
@@ -168,6 +167,64 @@ function removeGradleLibraryFromLibraries(librariesFile, gradleLibraryPath)
{
     });
 }
 
+function updateBuildGradleFile(librariesFile, updateFn) {
+    try {
+        var libraries = fs.readFileSync(librariesFile, {encoding: 'utf8'});
+        var lines = libraries.split('\n');
+        var openLine, closeLine;
+        for (var i = 0; i < lines.length; ++i) {
+            var line = lines[i];
+            if (line.indexOf('// PLUGIN GRADLE EXTENSIONS START') > -1) {
+                openLine = i;
+            }
+            if ((typeof openLine !== 'undefined') && line.indexOf('// PLUGIN GRADLE
EXTENSIONS END') > -1) {
+                closeLine = i;
+                break;
+            }
+        }
+        if ((typeof closeLine !== 'undefined') && (typeof openLine !== 'undefined'))
{
+            updateFn(lines, openLine, closeLine);
+            fs.writeFileSync(librariesFile, lines.join('\n'), {encoding: 'utf8'});
+        } else {
+            console.log('Cannot update build.gradle');
+        }
+    } catch (e) {
+        if (e.code != 'ENOENT') {
+            throw e;
+        }
+    }
+}
+
+function addReferenceToGradle(librariesFile, gradlePath) {
+    updateBuildGradleFile(librariesFile, function(lines, openLine, closeLine) {
+        var exists = false;
+        for (var i = openLine; i < closeLine; ++i) {
+            if (lines[i].indexOf('apply from: \'' + gradlePath + '\'') > -1) {
+                exists = true;
+            }
+        }
+        if (!exists) {
+            lines.splice(closeLine, 0, 'apply from: \'' + gradlePath + '\'');
+        }
+    });
+}
+
+function removeReferenceFromGradle(librariesFile, gradlePath) {
+    updateBuildGradleFile(librariesFile, function(lines, openLine, closeLine) {
+        var foundLine;
+        var exists = false;
+        for (var i = 0; i < lines.length; ++i) {
+            if (lines[i].indexOf('apply from: \'' + gradlePath + '\'') > -1) {
+                exists = true;
+                foundLine = i;
+            }
+        }
+        if (exists) {
+            lines.splice(foundLine, 1);
+        }
+    });
+}
+
 function addGradleLibraryReference(settingsFile, librariesFile, libraryPathComponents) {
     var gradleLibraryPath = '\':' + libraryPathComponents.join(':') + '\'';
     addGradleLibraryToSettings(settingsFile, gradleLibraryPath);
@@ -221,6 +278,20 @@ AndroidProject.prototype = {
         delete this._subProjectDirs[subDir];
         this._dirty = true;
     },
+    addGradleReference: function(parentDir, subDir) {
+        var gradleExtrasFile = path.resolve(parentDir, 'build.gradle');
+        var gradleReference = module.exports.getRelativeLibraryPath(parentDir, subDir);
+        if (fs.existsSync(gradleExtrasFile)) {
+            addReferenceToGradle(gradleExtrasFile, gradleReference);
+        }
+    },
+    removeGradleReference: function(parentDir, subDir) {
+        var gradleExtrasFile = path.resolve(parentDir, 'build.gradle');
+        var gradleReference = module.exports.getRelativeLibraryPath(parentDir, subDir);
+        if (fs.existsSync(gradleExtrasFile)) {
+            removeReferenceFromGradle(gradleExtrasFile, gradleReference);
+        }
+    },
     write: function(platformVersion) {
         if (!this._dirty) return;
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message