Hi all, I’m successfully getting the Kotlin REPL t...
# getting-started
n
Hi all, I’m successfully getting the Kotlin REPL to work via the IntelliJ integration for my Gradle project. Work great, and I’d like the ability to start the same repl in my shell via a script but I’m not having much luck (started by copying the command that IntelliJ uses, which is a start but it’s not quite working) Wondering if anyone has done this before and could point me to some documentation. I see the REPL in IntelliJ is marked as experimental so understand if on my own for this adventure :)
g
Curious what is your use case. Your application will embed Kotlin Repl inside?
n
No, just invoking my code via the repl. It’s nice to be able to do it outside of IntelliJ
g
you mean run repl with context of your own code? In general for this I use Scratch files
And it's not perfectly supported, but way better than Repl and have decent IDE support
So you create Kotlin Scratch file, you can choose classpath for it (no classpath except stdlib, or classpath of some of your modules) and you can write it in it and it can work as repl (execute line by line, in repl mode while you typing too)
n
I’ll have to play with it, that seems nice. would still like to know how to start the repl outside the context of the IDE Seems like it’s a Java app I should be able to run anywhere
g
if it an JVM app, I think you don't need repl, isn't it? You can just run it from command line
k
If you're looking for a REPL that allows you to call arbitrary functions from your project, try running
kotlin -cp <your-project's-jarfile>
. Download the kotlin compiler CLI from Github.
n
Ah! This sounds exactly like what I’m looking for, trying that out
this seems like the right path. If anyone has a similar need in the future, here is where I landed so far:
Copy code
// Download the Kotlin compiler to run the REPL
tasks.register("downloadKotlinCompiler", Download::class) {
    src("<https://github.com/JetBrains/kotlin/releases/download/v$kotlinVersion/kotlin-compiler-$kotlinVersion.zip>")
    dest(layout.buildDirectory.file("kotlin-compiler.zip"))
}

// Unzip the Kotlin compiler to run the REPL
tasks.register("unzipKotlinCompiler", Copy::class) {
    dependsOn("downloadKotlinCompiler")
    from(zipTree(layout.buildDirectory.file("kotlin-compiler.zip")))
    into(layout.buildDirectory.dir("kotlin-compiler"))
}

// REPL task, depends on the unzipping & download of the Kotlin compiler
tasks.register("repl", Exec::class.java) {
    dependsOn("unzipKotlinCompiler")

    // Set the working directory to where Kotlin bin is located
    workingDir = layout.buildDirectory.dir("kotlin-compiler").get().asFile

    val applicationClasspath = project.sourceSets["main"].runtimeClasspath.asPath
    commandLine = listOf(
        "kotlinc/bin/kotlin",
        "-cp",
        applicationClasspath
    )

    // Set up the standard I/O streams such that Gradle does not interfere
    standardInput = System.`in`
    standardOutput = System.out
    errorOutput = System.err
}
run with
./gradlew repl --console=plain