Skip to content

Commit

Permalink
Improve caching of proto in GenerateProtoTask
Browse files Browse the repository at this point in the history
This change will remove the OS from the input properties of the
`GenerateProtoTask`. The expectation is that same versions of `protoc`
will produce same outputs regardless of the OS on which it was run.

If a protoc `artifact` is defined with GAV parameters, the classifier
and extension will not be considered part of the input. An exception is
made for snapshots, which will always be considered uncacheable and be
re-run.

If a protoc `path` is defined, then the path will be exposed as a File
Provider with absolute path normalization since there is no good way to
tell the version of `protoc` without running `protoc --version`.

See google#457.
  • Loading branch information
clayburn committed Apr 27, 2022
1 parent 5eade53 commit aa107a8
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
package com.google.protobuf.gradle

import org.gradle.api.tasks.InputFile

import static java.nio.charset.StandardCharsets.US_ASCII

import com.google.common.base.Preconditions
Expand Down Expand Up @@ -330,8 +332,38 @@ public abstract class GenerateProtoTask extends DefaultTask {
@Internal("Input captured by getAlternativePaths(), this is used to query alternative path by locator name.")
abstract MapProperty<String, FileCollection> getLocatorToAlternativePathsMapping()

@InputFiles
@PathSensitive(PathSensitivity.NONE)
@Input
@Optional
Provider<String> getProtocArtifact() {
// Ideally, this value could be mapped from the protocLocator property, but Provider#map cannot return null in
// Gradle < 6.2. See https://github.com/gradle/gradle/issues/11979
return providerFactory.provider {
def protocArtifact = protocLocator.get().artifact
if (protocArtifact) {
def (groupId, artifact, version) = ToolsLocator.artifactParts(protocArtifact)
outputs.doNotCacheIf("protoc is a SNAPSHOT version", {
version.endsWith("-SNAPSHOT")
})
"$groupId:$artifact:$version".toString()
} else {
null
}
}
}

@InputFile
@PathSensitive(PathSensitivity.ABSOLUTE)
@Optional
Provider<File> getProtocPath() {
// Ideally, this value could be mapped from the protocLocator property, but Provider#map cannot return null in
// Gradle < 6.2. See https://github.com/gradle/gradle/issues/11979
return providerFactory.provider {
def path = getProtocLocator().get().path
path ? new File(path) : null
}
}

@Internal
ConfigurableFileCollection getAlternativePaths() {
return objectFactory.fileCollection().from(getLocatorToAlternativePathsMapping().get().values())
}
Expand Down

0 comments on commit aa107a8

Please sign in to comment.