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

Update to stable kotlin dsl #275

Merged
merged 6 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 6 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ buildscript {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath "com.gradle.publish:plugin-publish-plugin:0.9.7"
classpath "com.github.ben-manes:gradle-versions-plugin:0.12.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.10"
}
}

repositories {
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://repo.gradle.org/gradle/libs-releases-local/" }
google()
// Needed for resolving 'kotlinx-metadata-jvm'
// A transitive dependency of gradle Kotlin DSL
maven { url "https://kotlin.bintray.com/kotlinx/" }
}

configurations {
Expand Down Expand Up @@ -68,7 +71,7 @@ task createClasspathManifest {
dependencies {
compileOnly gradleApi()
compileOnly localGroovy()
compileOnly "org.gradle:gradle-kotlin-dsl:1.0-rc-6"
compileOnly "org.gradle:gradle-kotlin-dsl:1.0.3"

compile 'com.google.guava:guava:18.0'
compile 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
Expand All @@ -83,7 +86,7 @@ dependencies {
}
testCompile 'commons-io:commons-io:2.5'

testProjectRuntime "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.0"
testProjectRuntime "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.10"

// Add android plugin to the test classpath, so that we can jump into class definitions,
// read their sources, set break points, etc while debugging android unit tests.
Expand Down
2 changes: 1 addition & 1 deletion examples/exampleKotlinDslProject/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.gradle.kotlin.dsl.provider.gradleKotlinDslOf
plugins {
java
idea
id("com.google.protobuf") version "0.8.7-SNAPSHOT"
id("com.google.protobuf") version "0.8.8-SNAPSHOT"
}

repositories {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-rc-3-bin.zip
marcoferrer marked this conversation as resolved.
Show resolved Hide resolved
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun ProtobufConfigurator.protoc(closure: ExecutableLocator.() -> Unit) {

fun ProtobufConfigurator.plugins(closure: NamedDomainObjectContainerScope<ExecutableLocator>.() -> Unit) {
plugins(closureOf<NamedDomainObjectContainer<ExecutableLocator>> {
this(closure)
closure(NamedDomainObjectContainerScope.of(this))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New to kotlin and trying to decipher this syntax.
I can tell closure is a function parameter and Unit means no return value, but am not sure what NamedDomainObjectContainerScope<ExecutableLocator>.() means. And what does this refer to?

Seems NamedDomainObjectContainerScope<ExecutableLocator>.() -> Unit takes no parameter, but why closure is taking a parameter on this line?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Kotlin NamedDomainObjectContainerScope<ExecutableLocator> on the 2nd line is so-called receiver.

In closure: NamedDomainObjectContainerScope<ExecutableLocator>.() -> Unit closure parameter is a kind of 'extension' method, that would be executed on the appropriate receiver.

Back from bottom to top:

// extension method needs a receiver to be executed upon. The receiver is created
// by factory method NamedDomainObjectContainerScope.of(this)
closure(NamedDomainObjectContainerScope.of(this))
// here is what _this_ in the bottom line is. It is again a 'receiver' (now in groovy sense)
// of type NamedDomainObjectContainer<ExecutableLocator>
(closureOf<NamedDomainObjectContainer<ExecutableLocator>> {

and when we look inside 'plugins' definition:

  public void plugins(Closure configureClosure) {
    ConfigureUtil.configure(configureClosure, tools.plugins)
  }

indeed, tools.plugins parameter is of type NamedDomainObjectContainer.

So, back from top to bottom. The parameter tools.plugins of type NamedDomainObjectContainer would be configured with closureOf, it becomes this in the bottom line, where it is used as a factory method parameter. Factory method instantiates an object that would be configured by closure parameter of the top line.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation!

})
}

Expand All @@ -41,15 +41,15 @@ fun ProtobufConfigurator.generateProtoTasks(closure: ProtobufConfigurator.Genera
generateProtoTasks(closureOf(closure))
}

fun GenerateProtoTask.builtins(closure: NamedDomainObjectContainerScope<GenerateProtoTask.PluginOptions>.()->Unit) {
fun GenerateProtoTask.builtins(configuration: NamedDomainObjectContainerScope<GenerateProtoTask.PluginOptions>.()->Unit) {
builtins(closureOf<NamedDomainObjectContainer<GenerateProtoTask.PluginOptions>> {
this(closure)
configuration(NamedDomainObjectContainerScope.of(this))
})
}

fun GenerateProtoTask.plugins(closure: NamedDomainObjectContainerScope<GenerateProtoTask.PluginOptions>.()-> Unit) {
fun GenerateProtoTask.plugins(configuration: NamedDomainObjectContainerScope<GenerateProtoTask.PluginOptions>.()-> Unit) {
plugins(closureOf<NamedDomainObjectContainer<GenerateProtoTask.PluginOptions>> {
this(closure)
configuration(NamedDomainObjectContainerScope.of(this))
})
}

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 = ["4.10"]
private static final List<String> GRADLE_VERSIONS = ["5.0-rc-3"]
marcoferrer marked this conversation as resolved.
Show resolved Hide resolved

void "testProjectKotlinDsl should be successfully executed (java-only project)"() {
given: "project from testProjectKotlinDslBase"
Expand Down
2 changes: 1 addition & 1 deletion testProjectKotlinDslBase/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ tasks {
from(sourceSet.output)

val compileTaskName = sourceSet.getCompileTaskName("java")
dependsOn(tasks.getByName(compileTaskName))
dependsOn(project.tasks.getByName(compileTaskName))
Copy link
Contributor Author

@marcoferrer marcoferrer Nov 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An additional extension was added to the task container in 1.0.3. So in order to deal with ambiguity in method resolution I had to explicitly qualify the tasks property.

}
}

Expand Down