Skip to content

Commit

Permalink
Allow Kotlin lambda as deferred path/file notation
Browse files Browse the repository at this point in the history
  • Loading branch information
bamboo committed Sep 28, 2018
1 parent 0e456e3 commit c9b8f70
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ libraries.jsch = [coordinates: 'com.jcraft:jsch', version: '0.1.5
libraries.jsr305 = [coordinates: 'com.google.code.findbugs:jsr305', version: '3.0.2']
libraries.junit = [coordinates: 'junit:junit', version: '4.12']
libraries.junit_platform = [coordinates: 'org.junit.platform:junit-platform-launcher', version: '1.3.1']
libraries.kotlin = [coordinates: 'org.jetbrains.kotlin:kotlin-stdlib', version: '1.3.0-rc-116']
libraries.kryo = [coordinates: 'com.esotericsoftware.kryo:kryo', version: '2.24.0']
libraries.maven3 = [coordinates: 'org.apache.maven:maven-core', version: '3.0.4']
libraries.maven3_wagon_file = [coordinates: 'org.apache.maven.wagon:wagon-file', version: '3.0.0', because: '3.1.0 of wagon-http seems to break Digest authentication']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,27 @@ task("dumpKotlinBuildScriptModelClassPath") {
then:
outputContains("gradle-kotlin-dsl!")
}

def 'can use Kotlin lambda as path notation'() {
given:
buildFile << """
task("listFiles") {
doLast {
val collection = layout.files(
// top-level lambda
{ "foo" },
// nested lambda
{ file({ "bar" }) }
)
println(collection.files.map { it.name })
}
}
"""

when:
succeeds 'listFiles'

then:
outputContains '[foo, bar]'
}
}
1 change: 1 addition & 0 deletions subprojects/model-core/model-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
api project(':baseServices')
api project(":coreApi")
api libraries.groovy.coordinates
api libraries.kotlin.coordinates

implementation project(':baseServicesGroovy')
implementation libraries.slf4j_api.coordinates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

public class DeferredUtil {
/**
* Successively unpacks a path that may be deferred by a Callable or Factory
* until it's resolved to null or something other than a Callable or Factory.
* Successively unpacks a path that may be deferred by a Callable, Factory or Kotlin function
* until it's resolved to null or something other than a Callable, Factory or Kotlin function.
*/
@Nullable
public static Object unpack(@Nullable Object path) {
Expand All @@ -39,6 +39,8 @@ public static Object unpack(@Nullable Object path) {
return ((Provider<?>) current).get();
} else if (current instanceof Factory) {
return ((Factory) current).create();
} else if (current instanceof kotlin.jvm.functions.Function0) {
current = ((kotlin.jvm.functions.Function0) current).invoke();
} else {
return current;
}
Expand All @@ -49,6 +51,7 @@ public static Object unpack(@Nullable Object path) {
public static boolean isDeferred(Object value) {
return value instanceof Callable
|| value instanceof Provider
|| value instanceof Factory;
|| value instanceof Factory
|| value instanceof kotlin.jvm.functions.Function0;
}
}

0 comments on commit c9b8f70

Please sign in to comment.