Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for protoc --xxx_opt flag #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better readability, I would change makeOptionsPrefix() to assembleOptions(), which would leave out the trailing colon, then add the colon in the else branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to do that. That was my initial approach but I back tracked a bit. I was reluctant to introduce too many changes with out initial feedback.

baseCmd += "--${name}_opt=${pluginOutPrefix.substring(0, pluginOutPrefix.length() - 1)}"
baseCmd += "--${name}_out=${getOutputDir(plugin)}"
} else {
baseCmd += "--${name}_out=${pluginOutPrefix}${getOutputDir(plugin)}"
}
}

if (generateDescriptorSet) {
Expand Down
28 changes: 27 additions & 1 deletion src/main/groovy/com/google/protobuf/gradle/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not happy about it but it seems that this was the only dependable way to get the current protoc compiler version configured. The artifact property of the protoc executable locator is overridden once the path is resolved.

Copy link
Collaborator

@zhangkun83 zhangkun83 Jan 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The protoc may be from local path and you won't get the version from configurations at all.
A more reliable way is to run the protoc with --version. It should be run only once after protoc is resolved and saved somewhere (probably in ToolsLocator).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea a lot better

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) {
Expand Down