From 7deeb2ae87acb81c2b29ff68fdf04aebf89123b1 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Thu, 9 Feb 2023 09:14:54 -0800 Subject: [PATCH] Delay buildDir evaluation until needed This causes the plugin to use the correct configuration when users use a non-default buildDir. Note that for IDEs we create directories in afterEvaluate{}. It is possible for the user's own afterEvaluate{} to change buildDir. In that case, the task will output to the 'correct' directory but the mkdirs for IDEs can be the 'old' buildDir. But users shouldn't be using afterEvaluate{}, so we don't care and there's no real fix anyway. Fixes #674 --- .../google/protobuf/gradle/ProtobufExtension.groovy | 7 +++++-- .../com/google/protobuf/gradle/ProtobufPlugin.groovy | 8 ++++---- .../protobuf/gradle/ProtobufJavaPluginTest.groovy | 12 ++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy index 0de530f8..2e380022 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy @@ -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 /** @@ -54,14 +55,16 @@ abstract class ProtobufExtension { private final NamedDomainObjectContainer sourceSets @PackageScope - final String defaultGeneratedFilesBaseDir + final Provider 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) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index c0c2bae5..fe2a5875 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -374,20 +374,20 @@ class ProtobufPlugin implements Plugin { ) { String sourceSetName = protoSourceSet.name String taskName = 'generate' + Utils.getSourceSetSubstringForTaskNames(sourceSetName) + 'Proto' - String defaultGeneratedFilesBaseDir = protobufExtension.defaultGeneratedFilesBaseDir + Provider defaultGeneratedFilesBaseDir = protobufExtension.defaultGeneratedFilesBaseDir Provider generatedFilesBaseDirProvider = protobufExtension.generatedFilesBaseDirProperty Provider 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. diff --git a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy index d3c3be1d..ce58fabc 100644 --- a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy +++ b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy @@ -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"