Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass only *.proto files to protoc command #621

Merged
merged 5 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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