Skip to content

Commit

Permalink
Delay buildDir evaluation until task execution
Browse files Browse the repository at this point in the history
This causes the plugin to use the correct configuration when users
use a non-default buildDir.

Fixes google#674
  • Loading branch information
ejona86 committed Feb 9, 2023
1 parent 781fad2 commit 4b36d5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskCollection

/**
Expand All @@ -54,14 +55,16 @@ abstract class ProtobufExtension {
private final NamedDomainObjectContainer<ProtoSourceSet> sourceSets

@PackageScope
final String defaultGeneratedFilesBaseDir
final Provider<String> defaultGeneratedFilesBaseDir

public ProtobufExtension(final Project project) {
this.project = project
this.tasks = new GenerateProtoTaskCollection(project)
this.tools = new ToolsLocator(project)
this.taskConfigActions = []
this.defaultGeneratedFilesBaseDir = "${project.buildDir}/generated/source/proto"
this.defaultGeneratedFilesBaseDir = project.layout.buildDirectory.dir("generated/source/proto").map {
it.asFile.path
}
this.generatedFilesBaseDirProperty.convention(defaultGeneratedFilesBaseDir)
this.sourceSets = project.objects.domainObjectContainer(ProtoSourceSet) { String name ->
new DefaultProtoSourceSet(name, project.objects)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,20 @@ class ProtobufPlugin implements Plugin<Project> {
) {
String sourceSetName = protoSourceSet.name
String taskName = 'generate' + Utils.getSourceSetSubstringForTaskNames(sourceSetName) + 'Proto'
String defaultGeneratedFilesBaseDir = protobufExtension.defaultGeneratedFilesBaseDir
Provider<String> defaultGeneratedFilesBaseDir = protobufExtension.defaultGeneratedFilesBaseDir
Provider<String> generatedFilesBaseDirProvider = protobufExtension.generatedFilesBaseDirProperty
Provider<GenerateProtoTask> task = project.tasks.register(taskName, GenerateProtoTask) {
CopyActionFacade copyActionFacade = CopyActionFacade.Loader.create(it.project, it.objectFactory)
it.description = "Compiles Proto source for '${sourceSetName}'".toString()
it.outputBaseDir = project.providers.provider {
"${defaultGeneratedFilesBaseDir}/${sourceSetName}".toString()
it.outputBaseDir = defaultGeneratedFilesBaseDir.map {
"${it}/${sourceSetName}".toString()
}
it.addSourceDirs(protoSourceSet.proto)
it.addIncludeDir(protoSourceSet.proto.sourceDirectories)
it.addIncludeDir(protoSourceSet.includeProtoDirs)
it.doLast { task ->
String generatedFilesBaseDir = generatedFilesBaseDirProvider.get()
if (generatedFilesBaseDir == defaultGeneratedFilesBaseDir) {
if (generatedFilesBaseDir == defaultGeneratedFilesBaseDir.get()) {
return
}
// Purposefully don't wire this up to outputs, as it can be mixed with other files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ class ProtobufJavaPluginTest extends Specification {
assert project.tasks.extractMain2Proto instanceof ProtobufExtract
}

void "test buildDir is late bound"() {
given: "a basic project with java and com.google.protobuf"
Project project = setupBasicProject()

when: "project evaluated"
project.evaluate()
project.buildDir = "expect-the-unexpected"

then: "generate tasks added"
assert project.tasks.generateProto.outputBaseDir.contains("/expect-the-unexpected/")
}

@Unroll
void "testProject should be successfully executed (java-only project) [gradle #gradleVersion]"() {
given: "project from testProject"
Expand Down

0 comments on commit 4b36d5f

Please sign in to comment.