https://kotlinlang.org logo
#scripting
Title
# scripting
j

jmfayard

08/23/2022, 3:22 AM
Bonjour, I have a Kotlin script that I need to run on heroku (gradle buildpack) but I have no idea how to do
sdk install kotlin
or
snap install kotlin
on heroku and Google isn't my friend either. Any idea?
v

Vampire

08/23/2022, 8:59 AM
You mean a
.main.kts
script? I'd "simply" run it through Gradle like
Copy code
repositories {
    mavenCentral()
}

val compilerClasspath by configurations.creating {
    isCanBeConsumed = false
}

val scriptClasspath by configurations.creating {
    isCanBeConsumed = false
}

dependencies {
    compilerClasspath(kotlin("compiler", "1.7.0"))
    compilerClasspath(kotlin("scripting-compiler", "1.7.0"))
    add(scriptClasspath.name, kotlin("main-kts", "1.7.0"), closureOf<ExternalModuleDependency> { isTransitive = false } as groovy.lang.Closure<Any>)
}

val foo by tasks.registering(JavaExec::class) {
    inputs.file(file(".github/workflows/test.main.kts"))
    outputs.file(file(".github/workflows/test.yaml"))
    classpath(compilerClasspath)
    mainClass.set(K2JVMCompiler::class.qualifiedName)
    args("-no-stdlib", "-no-reflect")
    args("-classpath", scriptClasspath.asPath)
    args("-script", file(".github/workflows/test.main.kts").absolutePath)
}
K 3
think smart 1
j

jmfayard

08/24/2022, 12:53 PM
it works, I have tested, thanks
👌 1
v

Vampire

08/24/2022, 12:58 PM
Of course it works, I needed a 96 messages thread in this slack to get it working. :-D
j

jmfayard

08/24/2022, 1:31 PM
Amazing
Do you think it will be useful if I publish a small gradle plugin to abstract away that boilerplate?
v

Vampire

08/24/2022, 1:54 PM
Sure, I planned to do it, but didn't find the time and probably will not soon. You're welcome to do it instead. :-)
Actually I expected that to happen when I wrote my first answer here. 😄
j

jmfayard

08/24/2022, 2:02 PM
😄
@Vampire it would be even better if it's implemented in the Kotlin Gradle plugin directly, so I first opened an issue about this, thanks for your help.
👌 1
It could be straightforward to run a Kotlin script via Gradle, so that your build doesn't break if the Kotlin CLI is not installed If you think that's important, you can follow/comment this issue where I share Björn Kautler's solution https://youtrack.jetbrains.com/issue/KT-53707/The-Kotlin-Gradle-plugin-should-allow-to-wrap-a-Kotlin-script-inside-a-Gradle-task
👍 3
b

Big Chungus

08/24/2022, 7:20 PM
I think this would be much more appropriate for the gradle itself as an expansion of kotlin dsl
v

Vampire

08/24/2022, 8:50 PM
Nah, then you would be constraint to the version Gradle embeds. Besides that it currently does not work with the embedded Kotlin compiler, but needs the standalone one.
5 Views