Skip to content

Commit

Permalink
Use new public API to create SourceDirectorySet available since Gradl…
Browse files Browse the repository at this point in the history
…e 5.0 (#292)

Resolves #280
  • Loading branch information
zhangkun83 authored Jan 11, 2019
1 parent 7123080 commit bcf3f1a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,15 @@ public class GenerateProtoTask extends DefaultTask {
* directories.
*/
SourceDirectorySet getOutputSourceDirectorySet() {
Preconditions.checkNotNull(fileResolver)
SourceDirectorySet srcSet = new DefaultSourceDirectorySet(
"generate-proto-" + name, this.fileResolver, new DefaultDirectoryFileTreeFactory())
String srcSetName = "generate-proto-" + name
SourceDirectorySet srcSet
if (Utils.compareGradleVersion(project, "5.0") < 0) {
Preconditions.checkNotNull(fileResolver)
srcSet = new DefaultSourceDirectorySet(
srcSetName, this.fileResolver, new DefaultDirectoryFileTreeFactory())
} else {
srcSet = project.objects.sourceDirectorySet(srcSetName, srcSetName)
}
builtins.each { builtin ->
srcSet.srcDir new File(getOutputDir(builtin))
}
Expand Down
19 changes: 16 additions & 3 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import org.gradle.api.Task
import org.gradle.api.attributes.Attribute
import org.gradle.api.file.FileCollection
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.internal.file.DefaultSourceDirectorySet
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.internal.file.collections.DefaultDirectoryFileTreeFactory
import org.gradle.api.logging.LogLevel
import org.gradle.api.plugins.AppliedPlugin
import org.gradle.api.tasks.SourceSet
Expand Down Expand Up @@ -177,7 +179,18 @@ class ProtobufPlugin implements Plugin<Project> {
*/
private void addSourceSetExtensions() {
getSourceSets().all { sourceSet ->
sourceSet.extensions.create('proto', ProtobufSourceDirectorySet, sourceSet.name, fileResolver)
String name = sourceSet.name
SourceDirectorySet sds
if (Utils.compareGradleVersion(project, "5.0") < 0) {
// TODO(zhangkun83): remove dependency on Gradle internal APIs once we drop support for < 5.0
sds = new DefaultSourceDirectorySet(
name, "${name} Proto source", fileResolver, new DefaultDirectoryFileTreeFactory())
} else {
sds = project.objects.sourceDirectorySet(name, "${name} Proto source")
}
sourceSet.extensions.add('proto', sds)
sds.srcDir("src/${name}/proto")
sds.include("**/*.proto")
}
}

Expand Down Expand Up @@ -300,7 +313,7 @@ class ProtobufPlugin implements Plugin<Project> {
it.fileResolver = this.fileResolver
sourceSets.each { sourceSet ->
addSourceFiles(sourceSet.proto)
ProtobufSourceDirectorySet protoSrcDirSet = sourceSet.proto
SourceDirectorySet protoSrcDirSet = sourceSet.proto
protoSrcDirSet.srcDirs.each { srcDir ->
// 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
Expand Down Expand Up @@ -485,7 +498,7 @@ class ProtobufPlugin implements Plugin<Project> {
} else {
// Make the proto source dirs known to IDEs
getSourceSets().each { sourceSet ->
ProtobufSourceDirectorySet protoSrcDirSet = sourceSet.proto
SourceDirectorySet protoSrcDirSet = sourceSet.proto
protoSrcDirSet.srcDirs.each { File protoDir ->
Utils.addToIdeSources(project, Utils.isTest(sourceSet.name), protoDir)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.google.protobuf.gradle

import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.tasks.SourceSet
import org.gradle.kotlin.dsl.NamedDomainObjectContainerScope
Expand Down Expand Up @@ -45,16 +46,17 @@ fun Project.protobuf(action: ProtobufConfigurator.()->Unit) {
* }
* ```
*
* @receiver [SourceSet] The source set for which the [ProtobufSourceDirectorySet] extension
* @receiver [SourceSet] The source set for which the "proto" [SourceDirectorySet] extension
* will be configured
*
* @param action A configuration lambda to apply on a receiver of type [ProtobufSourceDirectorySet]
* @param action A configuration lambda to apply on a receiver of type [SourceDirectorySet]
* @return [Unit]
*/
fun SourceSet.proto(action: ProtobufSourceDirectorySet.() -> Unit) {
fun SourceSet.proto(action: SourceDirectorySet.() -> Unit) {
(this as? ExtensionAware)
?.extensions
?.getByType(ProtobufSourceDirectorySet::class.java)
?.getByName("proto")
?.let { it as? SourceDirectorySet }
?.apply(action)
}

Expand Down

0 comments on commit bcf3f1a

Please sign in to comment.