Skip to content

Commit

Permalink
Pass only *.proto files to protoc command (#621)
Browse files Browse the repository at this point in the history
**Background**
Non *.proto files passed to protoc exec command.

**Changes**
TLTR manual revert of 407ee13 

Pass includes and proto sources separately to generate proto task. 

Add README.md files to test projects to proto source dirs.

**Test plan**
Green pipelines.
  • Loading branch information
rougsig authored Oct 8, 2022
1 parent 7498c51 commit 8dbac63
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> dirs = (sourceDirs + includeDirs).filter { File it -> it.exists() }*.path
List<String> dirs = includeDirs.filter { File it -> it.exists() }*.path
.collect { "-I${it}".toString() }
logger.debug "ProtobufCompile using directories ${dirs}"
logger.debug "ProtobufCompile using files ${protoFiles}"
Expand Down
14 changes: 8 additions & 6 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class ProtobufPlugin implements Plugin<Project> {
Provider<ProtobufExtract> extractIncludeProtosTask = setupExtractIncludeProtosTask(
sourceSet.name, compileProtoPath, sourceSet.compileClasspath)
Provider<GenerateProtoTask> generateProtoTask = addGenerateProtoTask(
sourceSet.name, protoSrcDirSet.sourceDirectories, project.files(extractProtosTask),
sourceSet.name, protoSrcDirSet, project.files(extractProtosTask),
extractIncludeProtosTask) {
it.sourceSet = sourceSet
it.doneInitializing()
Expand Down Expand Up @@ -320,9 +320,8 @@ class ProtobufPlugin implements Plugin<Project> {
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)))
Expand Down Expand Up @@ -367,9 +366,10 @@ class ProtobufPlugin implements Plugin<Project> {
* 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<GenerateProtoTask> addGenerateProtoTask(
String sourceSetOrVariantName,
FileCollection sourceDirs,
SourceDirectorySet protoSourceSet,
FileCollection extractProtosDirs,
Provider<ProtobufExtract> extractIncludeProtosTask,
Action<GenerateProtoTask> configureAction) {
Expand All @@ -381,8 +381,10 @@ class ProtobufPlugin implements Plugin<Project> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<File>
)
}

void "testCustom sourceSet should get its own GenerateProtoTask"() {
given: "a basic project with java and com.google.protobuf"
Project project = setupBasicProject()
Expand Down
1 change: 1 addition & 0 deletions testProjectAndroidBase/src/main/proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
1 change: 1 addition & 0 deletions testProjectAndroidBase/src/test/proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
1 change: 1 addition & 0 deletions testProjectBase/src/main/proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
1 change: 1 addition & 0 deletions testProjectBase/src/test/proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World

0 comments on commit 8dbac63

Please sign in to comment.