$ brew install holgerbrandl/tap/kscript
$ brew install maven
$ kscript demo.kts
Hello from Kotlin!
Hello,
World!
demo.kts
:
#!/usr/bin/env kscript
@file:MavenRepository("central", "https://repo.maven.apache.org/maven2/")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:0.22.5")
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
println("Hello from Kotlin!")
launch {
delay(1000L)
println("World!")
}
println("Hello,")
Thread.sleep(2000L)
Links:
- https://github.com/holgerbrandl/kscript
- https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md
Dependency Repositories:
Caveats:
kscript requires maven to resolve dependencies.
$ kscript demo.kts
[kscript] Resolving dependencies...[kscript] [ERROR] mvn is not in PATH
$ brew install maven
when resolved dependencies were removed, the kscript cache needs to be deleted.
$ kscript demo.kts
$ rm -r ~/.m2/repository/org/jetbrains/kotlinx/
$ kscript demo.kts
...
Caused by: java.lang.ClassNotFoundException: kotlinx.coroutines.experimental.BuildersKt
$ rm -r ~/.kscript/
dependencies can also be downloaded manually.
$ mvn dependency:get -Dartifact=org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5