From b67fb19a5e81bd310b2f549ecbce6a8ead352db6 Mon Sep 17 00:00:00 2001 From: Marco Ferrer <35935108+marcoferrer@users.noreply.github.com> Date: Wed, 9 Jan 2019 23:16:52 -0500 Subject: [PATCH] add support for protoc --xxx_opt flag --- .../protobuf/gradle/GenerateProtoTask.groovy | 8 +++++- .../com/google/protobuf/gradle/Utils.groovy | 28 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy index 682a8b8f..07da2971 100644 --- a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy @@ -451,7 +451,13 @@ public class GenerateProtoTask extends DefaultTask { logger.warn "protoc plugin '${name}' not defined. Trying to use 'protoc-gen-${name}' from system path" } String pluginOutPrefix = makeOptionsPrefix(plugin.options) - baseCmd += "--${name}_out=${pluginOutPrefix}${getOutputDir(plugin)}" + // Check if protoc supports opt flag + if(Utils.compareProtocVersion(project, "3.2") >= 0 && pluginOutPrefix.length() > 1){ + baseCmd += "--${name}_opt=${pluginOutPrefix.substring(0, pluginOutPrefix.length() - 1)}" + baseCmd += "--${name}_out=${getOutputDir(plugin)}" + } else { + baseCmd += "--${name}_out=${pluginOutPrefix}${getOutputDir(plugin)}" + } } if (generateDescriptorSet) { diff --git a/src/main/groovy/com/google/protobuf/gradle/Utils.groovy b/src/main/groovy/com/google/protobuf/gradle/Utils.groovy index d2d5d3d6..f93472a9 100644 --- a/src/main/groovy/com/google/protobuf/gradle/Utils.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/Utils.groovy @@ -32,6 +32,7 @@ import com.google.common.base.Preconditions import org.apache.commons.lang.StringUtils import org.gradle.api.GradleException import org.gradle.api.Project +import org.gradle.api.artifacts.Dependency import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.TaskInputs import org.gradle.plugins.ide.idea.GenerateIdeaModule @@ -104,7 +105,32 @@ class Utils { * given target version. Only major and minor versions are checked. Patch version is ignored. */ static int compareGradleVersion(Project project, String target) { - Matcher gv = parseVersionString(project.gradle.gradleVersion) + return compareVersions(project.gradle.gradleVersion, target) + } + + /** + * Returns positive/0/negative if current Protoc version is higher than/equal to/lower than the + * given target version. Only major and minor versions are checked. Patch version is ignored. + */ + static int compareProtocVersion(Project project, String target) { + String protocVersion = null + Dependency protocDep = project.configurations + .findByName("protobufToolsLocator_protoc").getAllDependencies()[0] + if(protocDep != null){ + protocVersion = protocDep.version + } + if(protocVersion == null || protocVersion == ""){ + return -1 + } + return compareVersions(protocVersion, target) + } + + /** + * Returns positive/0/negative if current version is higher than/equal to/lower than the + * given target version. Only major and minor versions are checked. Patch version is ignored. + */ + private static int compareVersions(String current, String target) { + Matcher gv = parseVersionString(current) Matcher tv = parseVersionString(target) int majorVersionDiff = gv.group(1).toInteger() - tv.group(1).toInteger() if (majorVersionDiff != 0) {