From 407ee13063b4517dec38ef714fe9a7b21c2c41e5 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Tue, 5 Jul 2022 14:36:48 -0700 Subject: [PATCH] Don't duplicate GenerateProtoTask sources as includes Let the task itself handle the needed duplication. --- .../protobuf/gradle/GenerateProtoTask.groovy | 17 +++++++++-------- .../protobuf/gradle/ProtobufPlugin.groovy | 17 +++++------------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy index bf75c224..3dea5a1f 100644 --- a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy @@ -87,7 +87,7 @@ public abstract class GenerateProtoTask extends DefaultTask { // that may be "imported" from the source protos, but will not be compiled. private final ConfigurableFileCollection includeDirs = objectFactory.fileCollection() // source files are proto files that will be compiled by protoc - private final ConfigurableFileCollection sourceFiles = objectFactory.fileCollection() + private final ConfigurableFileCollection sourceDirs = objectFactory.fileCollection() private final NamedDomainObjectContainer builtins = objectFactory.domainObjectContainer(PluginOptions) private final NamedDomainObjectContainer plugins = objectFactory.domainObjectContainer(PluginOptions) private final ProjectLayout projectLayout = project.layout @@ -289,7 +289,7 @@ public abstract class GenerateProtoTask extends DefaultTask { this.buildType = buildType } - @Internal("Inputs tracked in getSourceFiles()") + @Internal("Inputs tracked in getSourceDirs()") SourceSet getSourceSet() { Preconditions.checkState(!isAndroidProject.get(), 'sourceSet should not be used in an Android project') @@ -301,8 +301,8 @@ public abstract class GenerateProtoTask extends DefaultTask { @PathSensitive(PathSensitivity.RELATIVE) @IgnoreEmptyDirectories @InputFiles - FileCollection getSourceFiles() { - return sourceFiles + FileCollection getSourceDirs() { + return sourceDirs } @InputFiles @@ -471,9 +471,9 @@ public abstract class GenerateProtoTask extends DefaultTask { /** * Add a collection of proto source files to be compiled. */ - public void addSourceFiles(FileCollection files) { + public void addSourceDirs(FileCollection dirs) { checkCanConfig() - sourceFiles.from(files) + sourceDirs.from(dirs) } /** @@ -590,7 +590,7 @@ public abstract class GenerateProtoTask extends DefaultTask { // Sort to ensure generated descriptors have a canonical representation // to avoid triggering unnecessary rebuilds downstream - List protoFiles = sourceFiles.files.sort() + List protoFiles = sourceDirs.asFileTree.files.sort() [builtins, plugins]*.forEach { PluginOptions plugin -> String outputPath = getOutputDir(plugin) @@ -605,7 +605,8 @@ public abstract class GenerateProtoTask extends DefaultTask { // The source directory designated from sourceSet may not actually exist on disk. // "include" it only when it exists, so that Gradle and protoc won't complain. - List dirs = includeDirs.filter { File it -> it.exists() }*.path.collect { "-I${it}".toString() } + List dirs = (sourceDirs + includeDirs).filter { File it -> it.exists() }*.path + .collect { "-I${it}".toString() } logger.debug "ProtobufCompile using directories ${dirs}" logger.debug "ProtobufCompile using files ${protoFiles}" diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index 9f3d9a07..029f999f 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -253,7 +253,7 @@ class ProtobufPlugin implements Plugin { // dependent projects can import them. Task processResourcesTask = project.tasks.getByName(sourceSet.getTaskName('process', 'resources')) - processResourcesTask.from(generateProtoTask.sourceFiles) { + processResourcesTask.from(generateProtoTask.sourceDirs) { include '**/*.proto' } @@ -297,7 +297,7 @@ class ProtobufPlugin implements Plugin { // Include source proto files in the compiled archive, so that proto files from // dependent projects can import them. Task processResourcesTask = variant.getProcessJavaResourcesProvider().get() - processResourcesTask.from(generateProtoTask.sourceFiles) { + processResourcesTask.from(generateProtoTask.sourceDirs) { include '**/*.proto' } } @@ -332,9 +332,7 @@ class ProtobufPlugin implements Plugin { description = "Compiles Proto source for '${sourceSetOrVariantName}'" outputBaseDir = outDir sourceSets.each { sourceSet -> - addSourceFiles(sourceSet.proto) - SourceDirectorySet protoSrcDirSet = sourceSet.proto - addIncludeDir(protoSrcDirSet.sourceDirectories) + addSourceDirs(sourceSet.proto.sourceDirectories) } } } @@ -360,8 +358,7 @@ class ProtobufPlugin implements Plugin { } } - linkExtractTaskToGenerateTask(task, generateProtoTask) - generateProtoTask.addSourceFiles(project.files(task).asFileTree) + generateProtoTask.addSourceDirs(project.files(task)) return task } @@ -413,11 +410,7 @@ class ProtobufPlugin implements Plugin { } } - linkExtractTaskToGenerateTask(task, generateProtoTask) - } - - private void linkExtractTaskToGenerateTask(ProtobufExtract extractTask, GenerateProtoTask generateTask) { - generateTask.addIncludeDir(project.files(extractTask)) + generateProtoTask.addIncludeDir(project.files(task)) } private void linkGenerateProtoTasksToTaskName(String compileTaskName, GenerateProtoTask genProtoTask) {