From f999f0b1ca1229e9464243f8e8d04e9e9e6f6ab4 Mon Sep 17 00:00:00 2001 From: Marco Ferrer <35935108+marcoferrer@users.noreply.github.com> Date: Tue, 11 Dec 2018 11:15:06 -0500 Subject: [PATCH] implement compatibility support for gradle 4.10 --- README.md | 2 +- .../exampleKotlinDslProject/build.gradle.kts | 7 ++--- .../com/google/protobuf/gradle/Utils.groovy | 29 +++++++++++++++++++ .../gradle/ProtobufConfiguratorExts.kt | 6 ++-- .../gradle/ProtobufKotlinDslPluginTest.groovy | 2 +- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5de23652..b8cc15dc 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Stand-alone examples are available for each of gradle's supported languages. * [Groovy](https://github.com/google/protobuf-gradle-plugin/tree/master/examples/exampleProject) ___(Default)___ * Run `../../gradlew build` under the example directory to test it out. * [Kotlin](https://github.com/google/protobuf-gradle-plugin/tree/master/examples/exampleKotlinDslProject) ___(Experimental)___ - * Run `./gradlew build` under the Kotlin example directory to test it out. This example is set up with Gradle 5.0, the minimum required version. + * Run `./gradlew build` under the Kotlin example directory to test it out. This example is set up with Gradle 4.10, the minimum required version. Directories that start with `testProject` can also serve as usage diff --git a/examples/exampleKotlinDslProject/build.gradle.kts b/examples/exampleKotlinDslProject/build.gradle.kts index e5fbc89f..d22b9e81 100644 --- a/examples/exampleKotlinDslProject/build.gradle.kts +++ b/examples/exampleKotlinDslProject/build.gradle.kts @@ -3,7 +3,7 @@ import org.gradle.kotlin.dsl.provider.gradleKotlinDslOf // A minimal example Java project that uses the protobuf plugin. // To build it: -// $ ../gradlew clean build +// $ ./gradlew clean build plugins { java idea @@ -61,10 +61,7 @@ protobuf { generateProtoTasks { ofSourceSet("main").forEach { it.plugins { - // Apply the "grpc" plugin whose spec is defined above, without - // options. Note the braces cannot be omitted, otherwise the - // plugin will not be added. This is because of the implicit way - // NamedDomainObjectContainer binds the methods. + // Apply the "grpc" plugin whose spec is defined above, without options. id("grpc") } } diff --git a/src/main/groovy/com/google/protobuf/gradle/Utils.groovy b/src/main/groovy/com/google/protobuf/gradle/Utils.groovy index 6a134fb0..cf51c9ff 100644 --- a/src/main/groovy/com/google/protobuf/gradle/Utils.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/Utils.groovy @@ -29,11 +29,16 @@ package com.google.protobuf.gradle import com.google.common.base.Preconditions +import kotlin.Unit +import kotlin.jvm.functions.Function1 import org.apache.commons.lang.StringUtils import org.gradle.api.GradleException +import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.TaskInputs +import org.gradle.kotlin.dsl.NamedDomainObjectContainerExtensionsKt +import org.gradle.kotlin.dsl.NamedDomainObjectContainerScope import org.gradle.plugins.ide.idea.GenerateIdeaModule import org.gradle.plugins.ide.idea.model.IdeaModel import org.gradle.util.GUtil @@ -159,4 +164,28 @@ class Utils { } } } + + /** + * This method is used to delegate the NamedDomainObjectContainer configuration to + * to which ever kotlin-dsl ext implementation is available on the classpath. + * + * Since NamedDomainObjectContainerExtensionsKt.invoke is an inline extension function, our + * usages in 'ProtobufConfiguratorExts.kt' would use the byte code for which ever kotlin-dsl + * version we compiled against. This caused issues with providing compatibility with + * kotlin-dsl 1.0.0-rc3 and 1.0.4 (Stable). + * + * Since the kotlin compiler creates a static implementations of all extensions, we can create a + * delegating utility function that ensures we are not inline-ing our kotlin-dsl compilation + * target byte code. + * + * @param container Container to apply the scope configuration + * @param block A kotlin lambda to apply to the NamedDomainObjectContainerScope + * @return A NamedDomainObjectContainer with the block lambda applied to is NamedDomainObjectContainerScope + */ + static NamedDomainObjectContainer configureNamedDomainObjectContainer( + NamedDomainObjectContainer container, + Function1, Unit> block){ + + return NamedDomainObjectContainerExtensionsKt.invoke(container, block) + } } diff --git a/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt b/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt index 12e96719..932ce494 100644 --- a/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt +++ b/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt @@ -85,7 +85,7 @@ fun ProtobufConfigurator.protoc(action: ExecutableLocator.() -> Unit) { fun ProtobufConfigurator.plugins(action: NamedDomainObjectContainerScope.() -> Unit) { plugins(closureOf> { - NamedDomainObjectContainerScope.of(this).action() + Utils.configureNamedDomainObjectContainer(this, action) }) } @@ -95,13 +95,13 @@ fun ProtobufConfigurator.generateProtoTasks(action: ProtobufConfigurator.Generat fun GenerateProtoTask.builtins(action: NamedDomainObjectContainerScope.()->Unit) { builtins(closureOf> { - NamedDomainObjectContainerScope.of(this).action() + Utils.configureNamedDomainObjectContainer(this, action) }) } fun GenerateProtoTask.plugins(action: NamedDomainObjectContainerScope.()-> Unit) { plugins(closureOf> { - NamedDomainObjectContainerScope.of(this).action() + Utils.configureNamedDomainObjectContainer(this, action) }) } diff --git a/src/test/groovy/com/google/protobuf/gradle/ProtobufKotlinDslPluginTest.groovy b/src/test/groovy/com/google/protobuf/gradle/ProtobufKotlinDslPluginTest.groovy index 41b1fe95..7c1d05bf 100644 --- a/src/test/groovy/com/google/protobuf/gradle/ProtobufKotlinDslPluginTest.groovy +++ b/src/test/groovy/com/google/protobuf/gradle/ProtobufKotlinDslPluginTest.groovy @@ -9,7 +9,7 @@ import spock.lang.Specification * Unit tests for kotlin dsl extensions. */ class ProtobufKotlinDslPluginTest extends Specification { - private static final List GRADLE_VERSIONS = ["5.0"] + private static final List GRADLE_VERSIONS = ["4.10", "5.0"] void "testProjectKotlinDsl should be successfully executed (java-only project)"() { given: "project from testProjectKotlinDslBase"