Skip to content

Commit

Permalink
implement compatibility support for gradle 4.10
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoferrer committed Dec 11, 2018
1 parent 9d1da25 commit 6fe9209
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions examples/exampleKotlinDslProject/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.google.protobuf.gradle;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.kotlin.dsl.NamedDomainObjectContainerExtensionsKt;
import org.gradle.kotlin.dsl.NamedDomainObjectContainerScope;

public class KtDslCompatibilityUtils {

/**
* 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 <T> NamedDomainObjectContainer<T> configureNamedDomainObjectContainer(
NamedDomainObjectContainer<T> container,
Function1<? super NamedDomainObjectContainerScope<T>, Unit> block){

return NamedDomainObjectContainerExtensionsKt.invoke(container, block);
}
}
1 change: 1 addition & 0 deletions src/main/groovy/com/google/protobuf/gradle/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@ class Utils {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fun ProtobufConfigurator.protoc(action: ExecutableLocator.() -> Unit) {

fun ProtobufConfigurator.plugins(action: NamedDomainObjectContainerScope<ExecutableLocator>.() -> Unit) {
plugins(closureOf<NamedDomainObjectContainer<ExecutableLocator>> {
NamedDomainObjectContainerScope.of(this).action()
KtDslCompatibilityUtils.configureNamedDomainObjectContainer<ExecutableLocator>(this, action)
})
}

Expand All @@ -95,13 +95,13 @@ fun ProtobufConfigurator.generateProtoTasks(action: ProtobufConfigurator.Generat

fun GenerateProtoTask.builtins(action: NamedDomainObjectContainerScope<GenerateProtoTask.PluginOptions>.()->Unit) {
builtins(closureOf<NamedDomainObjectContainer<GenerateProtoTask.PluginOptions>> {
NamedDomainObjectContainerScope.of(this).action()
KtDslCompatibilityUtils.configureNamedDomainObjectContainer<GenerateProtoTask.PluginOptions>(this, action)
})
}

fun GenerateProtoTask.plugins(action: NamedDomainObjectContainerScope<GenerateProtoTask.PluginOptions>.()-> Unit) {
plugins(closureOf<NamedDomainObjectContainer<GenerateProtoTask.PluginOptions>> {
NamedDomainObjectContainerScope.of(this).action()
KtDslCompatibilityUtils.configureNamedDomainObjectContainer<GenerateProtoTask.PluginOptions>(this, action)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import spock.lang.Specification
* Unit tests for kotlin dsl extensions.
*/
class ProtobufKotlinDslPluginTest extends Specification {
private static final List<String> GRADLE_VERSIONS = ["5.0"]
private static final List<String> GRADLE_VERSIONS = ["4.10", "5.0"]

void "testProjectKotlinDsl should be successfully executed (java-only project)"() {
given: "project from testProjectKotlinDslBase"
Expand Down

0 comments on commit 6fe9209

Please sign in to comment.