From ea6477f4d3e63ebb1f1717cc02e3117aa356ce84 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Mon, 19 Mar 2018 13:20:09 -0700 Subject: [PATCH] CB-13830: Add handlers for plugins that use non-Java source files, such as Camera --- bin/templates/cordova/Api.js | 2 +- .../cordova/lib/builders/StudioBuilder.js | 110 +++++++++--------- bin/templates/cordova/lib/pluginHandlers.js | 33 ++++-- 3 files changed, 78 insertions(+), 67 deletions(-) diff --git a/bin/templates/cordova/Api.js b/bin/templates/cordova/Api.js index e97f5380..ae4d17bd 100644 --- a/bin/templates/cordova/Api.js +++ b/bin/templates/cordova/Api.js @@ -248,7 +248,7 @@ Api.prototype.addPlugin = function (plugin, installOptions) { }).then(function () { if (plugin.getFrameworks(this.platform).length === 0) return; selfEvents.emit('verbose', 'Updating build files since android plugin contained '); - // This should pick the correct builder, not just get gradle + // This should pick the correct builder, not just get gradle require('./lib/builders/builders').getBuilder(this.builder).prepBuildFiles(); }.bind(this)) // CB-11022 Return truthy value to prevent running prepare after diff --git a/bin/templates/cordova/lib/builders/StudioBuilder.js b/bin/templates/cordova/lib/builders/StudioBuilder.js index 262c1e3a..1689e16c 100644 --- a/bin/templates/cordova/lib/builders/StudioBuilder.js +++ b/bin/templates/cordova/lib/builders/StudioBuilder.js @@ -215,28 +215,28 @@ StudioBuilder.prototype.prepBuildFiles = function () { StudioBuilder.prototype.prepEnv = function (opts) { var self = this; return check_reqs.check_gradle() - .then(function (gradlePath) { - return self.runGradleWrapper(gradlePath); - }).then(function () { - return self.prepBuildFiles(); - }).then(function () { - // If the gradle distribution URL is set, make sure it points to version we want. - // If it's not set, do nothing, assuming that we're using a future version of gradle that we don't want to mess with. - // For some reason, using ^ and $ don't work. This does the job, though. - var distributionUrlRegex = /distributionUrl.*zip/; - var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'https\\://services.gradle.org/distributions/gradle-4.1-all.zip'; - var gradleWrapperPropertiesPath = path.join(self.root, 'gradle', 'wrapper', 'gradle-wrapper.properties'); - shell.chmod('u+w', gradleWrapperPropertiesPath); - shell.sed('-i', distributionUrlRegex, 'distributionUrl=' + distributionUrl, gradleWrapperPropertiesPath); + .then(function (gradlePath) { + return self.runGradleWrapper(gradlePath); + }).then(function () { + return self.prepBuildFiles(); + }).then(function () { + // If the gradle distribution URL is set, make sure it points to version we want. + // If it's not set, do nothing, assuming that we're using a future version of gradle that we don't want to mess with. + // For some reason, using ^ and $ don't work. This does the job, though. + var distributionUrlRegex = /distributionUrl.*zip/; + var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'https\\://services.gradle.org/distributions/gradle-4.1-all.zip'; + var gradleWrapperPropertiesPath = path.join(self.root, 'gradle', 'wrapper', 'gradle-wrapper.properties'); + shell.chmod('u+w', gradleWrapperPropertiesPath); + shell.sed('-i', distributionUrlRegex, 'distributionUrl=' + distributionUrl, gradleWrapperPropertiesPath); - var propertiesFile = opts.buildType + SIGNING_PROPERTIES; - var propertiesFilePath = path.join(self.root, propertiesFile); - if (opts.packageInfo) { - fs.writeFileSync(propertiesFilePath, TEMPLATE + opts.packageInfo.toProperties()); - } else if (isAutoGenerated(propertiesFilePath)) { - shell.rm('-f', propertiesFilePath); - } - }); + var propertiesFile = opts.buildType + SIGNING_PROPERTIES; + var propertiesFilePath = path.join(self.root, propertiesFile); + if (opts.packageInfo) { + fs.writeFileSync(propertiesFilePath, TEMPLATE + opts.packageInfo.toProperties()); + } else if (isAutoGenerated(propertiesFilePath)) { + shell.rm('-f', propertiesFilePath); + } + }); }; /* @@ -248,33 +248,33 @@ StudioBuilder.prototype.build = function (opts) { var args = this.getArgs(opts.buildType === 'debug' ? 'debug' : 'release', opts); return spawn(wrapper, args, {stdio: 'pipe'}) - .progress(function (stdio) { - if (stdio.stderr) { - /* - * Workaround for the issue with Java printing some unwanted information to - * stderr instead of stdout. - * This function suppresses 'Picked up _JAVA_OPTIONS' message from being - * printed to stderr. See https://issues.apache.org/jira/browse/CB-9971 for - * explanation. - */ - var suppressThisLine = /^Picked up _JAVA_OPTIONS: /i.test(stdio.stderr.toString()); - if (suppressThisLine) { - return; + .progress(function (stdio) { + if (stdio.stderr) { + /* + * Workaround for the issue with Java printing some unwanted information to + * stderr instead of stdout. + * This function suppresses 'Picked up _JAVA_OPTIONS' message from being + * printed to stderr. See https://issues.apache.org/jira/browse/CB-9971 for + * explanation. + */ + var suppressThisLine = /^Picked up _JAVA_OPTIONS: /i.test(stdio.stderr.toString()); + if (suppressThisLine) { + return; + } + process.stderr.write(stdio.stderr); + } else { + process.stdout.write(stdio.stdout); } - process.stderr.write(stdio.stderr); - } else { - process.stdout.write(stdio.stdout); - } - }).catch(function (error) { - if (error.toString().indexOf('failed to find target with hash string') >= 0) { - return check_reqs.check_android_target(error).then(function () { - // If due to some odd reason - check_android_target succeeds - // we should still fail here. - return Q.reject(error); - }); - } - return Q.reject(error); - }); + }).catch(function (error) { + if (error.toString().indexOf('failed to find target with hash string') >= 0) { + return check_reqs.check_android_target(error).then(function () { + // If due to some odd reason - check_android_target succeeds + // we should still fail here. + return Q.reject(error); + }); + } + return Q.reject(error); + }); }; StudioBuilder.prototype.clean = function (opts) { @@ -284,16 +284,16 @@ StudioBuilder.prototype.clean = function (opts) { return Q().then(function () { return spawn(wrapper, args, {stdio: 'inherit'}); }) - .then(function () { - shell.rm('-rf', path.join(builder.root, 'out')); + .then(function () { + shell.rm('-rf', path.join(builder.root, 'out')); - ['debug', 'release'].forEach(function (config) { - var propertiesFilePath = path.join(builder.root, config + SIGNING_PROPERTIES); - if (isAutoGenerated(propertiesFilePath)) { - shell.rm('-f', propertiesFilePath); - } + ['debug', 'release'].forEach(function (config) { + var propertiesFilePath = path.join(builder.root, config + SIGNING_PROPERTIES); + if (isAutoGenerated(propertiesFilePath)) { + shell.rm('-f', propertiesFilePath); + } + }); }); - }); }; module.exports = StudioBuilder; diff --git a/bin/templates/cordova/lib/pluginHandlers.js b/bin/templates/cordova/lib/pluginHandlers.js index 842489ae..b5a46cfe 100644 --- a/bin/templates/cordova/lib/pluginHandlers.js +++ b/bin/templates/cordova/lib/pluginHandlers.js @@ -34,15 +34,7 @@ var handlers = { // a later plugins release. This is for legacy plugins to work with Cordova. if (options && options.android_studio === true) { - // If a Java file is using the new directory structure, don't penalize it - if (!obj.targetDir.includes('app/src/main')) { - if (obj.src.endsWith('.java')) { - dest = path.join('app/src/main/java', obj.targetDir.substring(4), path.basename(obj.src)); - } else if (obj.src.endsWith('.xml')) { - // We are making a huge assumption here that XML files will be going to res/xml or values/xml - dest = path.join('app/src/main', obj.targetDir, path.basename(obj.src)); - } - } + dest = studioPathRemap(obj); } if (options && options.force) { @@ -55,10 +47,16 @@ var handlers = { var dest = path.join(obj.targetDir, path.basename(obj.src)); if (options && options.android_studio === true) { - dest = path.join('app/src/main/java', obj.targetDir.substring(4), path.basename(obj.src)); + dest = studioPathRemap(obj); } - deleteJava(project.projectDir, dest); + // TODO: Add Koltin extension to uninstall, since they are handled like Java files + if (obj.src.endsWith('java')) { + deleteJava(project.projectDir, dest); + } else { + // Just remove the file, not the whole parent directory + removeFile(project.projectDir, dest); + } } }, 'lib-file': { @@ -318,3 +316,16 @@ function removeFileAndParents (baseDir, destFile, stopper) { function generateAttributeError (attribute, element, id) { return 'Required attribute "' + attribute + '" not specified in <' + element + '> element from plugin: ' + id; } + +function studioPathRemap (obj) { + // If a Java file is using the new directory structure, don't penalize it + if (!obj.targetDir.includes('app/src/main')) { + if (obj.src.endsWith('.java')) { + return path.join('app/src/main/java', obj.targetDir.substring(4), path.basename(obj.src)); + } else if (obj.src.endsWith('.xml')) { + // We are making a huge assumption here that XML files will be going to res/xml or values/xml + return path.join('app/src/main', obj.targetDir, path.basename(obj.src)); + } + } + +}