diff --git a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy index 35b656d6..2e6f2510 100644 --- a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy @@ -604,7 +604,7 @@ 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 = (sourceDirs + includeDirs).filter { File it -> it.exists() }*.path + List dirs = 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 60840cb1..82b18d67 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -248,7 +248,7 @@ class ProtobufPlugin implements Plugin { Provider extractIncludeProtosTask = setupExtractIncludeProtosTask( sourceSet.name, compileProtoPath, sourceSet.compileClasspath) Provider generateProtoTask = addGenerateProtoTask( - sourceSet.name, protoSrcDirSet.sourceDirectories, project.files(extractProtosTask), + sourceSet.name, protoSrcDirSet, project.files(extractProtosTask), extractIncludeProtosTask) { it.sourceSet = sourceSet it.doneInitializing() @@ -320,9 +320,8 @@ class ProtobufPlugin implements Plugin { setupExtractIncludeProtosTask(variant.name, classPathConfig, testClassPathConfig) // GenerateProto task, one per variant (compilation unit). - FileCollection sourceDirs = project.files(project.providers.provider { - variant.sourceSets.collect { it.proto.sourceDirectories } - }) + SourceDirectorySet sourceDirs = project.objects.sourceDirectorySet(variant.name, "AllSourceSets") + variant.sourceSets.forEach { sourceDirs.source(it.proto) } FileCollection extractProtosDirs = project.files(project.providers.provider { variant.sourceSets.collect { project.files(project.tasks.named(getExtractProtosTaskName(it.name))) @@ -367,9 +366,10 @@ class ProtobufPlugin implements Plugin { * compiled. For Java it's the sourceSet that sourceSetOrVariantName stands * for; for Android it's the collection of sourceSets that the variant includes. */ + @SuppressWarnings(["UnnecessaryObjectReferences"]) // suppress a lot of it.doLogic in task registration block private Provider addGenerateProtoTask( String sourceSetOrVariantName, - FileCollection sourceDirs, + SourceDirectorySet protoSourceSet, FileCollection extractProtosDirs, Provider extractIncludeProtosTask, Action configureAction) { @@ -381,8 +381,10 @@ class ProtobufPlugin implements Plugin { return project.tasks.register(generateProtoTaskName, GenerateProtoTask) { it.description = "Compiles Proto source for '${sourceSetOrVariantName}'".toString() it.outputBaseDir = outDir - it.addSourceDirs(sourceDirs) + it.addSourceDirs(protoSourceSet) + it.addIncludeDir(protoSourceSet.sourceDirectories) it.addSourceDirs(extractProtosDirs) + it.addIncludeDir(extractProtosDirs) it.addIncludeDir(project.files(extractIncludeProtosTask)) configureAction.execute(it) } diff --git a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy index 58c72b59..451a6610 100644 --- a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy +++ b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy @@ -35,6 +35,23 @@ class ProtobufJavaPluginTest extends Specification { assert project.tasks.extractTestProto instanceof ProtobufExtract } + void "test generate proto task sources should include only *.proto files"() { + given: "a project with readme file in proto source directory" + Project project = setupBasicProject() + project.file("src/main/proto").mkdirs() + project.file("src/main/proto/messages.proto") << "syntax = \"proto3\";" + project.file("src/main/proto/README.md") << "Hello World" + + when: "project evaluated" + project.evaluate() + + then: "it contains only *.proto files" + assert Objects.equals( + project.tasks.generateProto.sourceDirs.asFileTree.files, + [project.file("src/main/proto/messages.proto")] as Set + ) + } + void "testCustom sourceSet should get its own GenerateProtoTask"() { given: "a basic project with java and com.google.protobuf" Project project = setupBasicProject() diff --git a/testProjectAndroidBase/src/main/proto/README.md b/testProjectAndroidBase/src/main/proto/README.md new file mode 100644 index 00000000..557db03d --- /dev/null +++ b/testProjectAndroidBase/src/main/proto/README.md @@ -0,0 +1 @@ +Hello World diff --git a/testProjectAndroidBase/src/test/proto/README.md b/testProjectAndroidBase/src/test/proto/README.md new file mode 100644 index 00000000..557db03d --- /dev/null +++ b/testProjectAndroidBase/src/test/proto/README.md @@ -0,0 +1 @@ +Hello World diff --git a/testProjectBase/src/main/proto/README.md b/testProjectBase/src/main/proto/README.md new file mode 100644 index 00000000..557db03d --- /dev/null +++ b/testProjectBase/src/main/proto/README.md @@ -0,0 +1 @@ +Hello World diff --git a/testProjectBase/src/test/proto/README.md b/testProjectBase/src/test/proto/README.md new file mode 100644 index 00000000..557db03d --- /dev/null +++ b/testProjectBase/src/test/proto/README.md @@ -0,0 +1 @@ +Hello World