Hey all, I have colleagues who don't use IntelliJ...
# gradle
g
Hey all, I have colleagues who don't use IntelliJ to write Kotlin. They would like some kind of way to run sandbox code like
.kts
files or `repl`'s. Is there a way I can make a custom Gradle task in our
build.gradle.kts
that launches a
.kts
script that has our project code and library modules on the classpath for them? Thank you!
e
launching anything interactive from Gradle is a bit of a hassle because of the daemon - it's not attached to the console
but you could build a custom kotlin interactive shell, or https://github.com/kscripting/kscript#treat-yourself-a-repl-with-interactive, and run it directly from the command line
a
from what I recall, it’s possible to .kts scripts from Gradle, but discovering the requirements was difficult https://kotlinlang.slack.com/archives/C0BT46EL8/p1661224924225269 https://kotlinlang.slack.com/archives/C0BT46EL8/p1655415795596189
t
You could check an example here how to run kts
g
Oh wow @tapchicoma this is brilliant, it looks like this also injects the project classpath too to provide dependencies?
Thanks all for the help, I was looking into some of these articles and also at the output of starting a Kotlin REPL from IntelliJ:
Copy code
java
  -Dkotlin.repl.ideMode=true -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8
  @/mnt/c/Users/rayga/AppData/Local/Temp/idea_arg_file194049547
  org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -jvm-target 1.8 -kotlin-home /mnt/c/Users/rayga/AppData/Local/JetBrains/Toolbox/apps/IDEA-U/ch-0/223.8617.20/plugins/Kotlin/kotlinc
So it seems easy enough to invoke this but I didn't understand, how does the IntelliJ Kotlin REPL get the project classpath dependencies?
The contents of
@/mnt/c/Users/rayga/AppData/Local/Temp/idea_arg_file194049547
were empty, I built a whole application to monitor
/Temp
for new files and log the contents of the
idea_arg
for nothing!
e
through the tooling API
g
but there's no argument or anything there, it seems to just run this: `
Copy code
java
  -Dkotlin.repl.ideMode=true
  org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
  -jvm-target 1.8
  -kotlin-home plugins/Kotlin/kotlinc
So how does running this command, which doesn't know where my application is, pick up my app's gradle build and classpath 🤔
e
easier to create a Gradle task that consumes the runtime configuration as the classpath, or probably more useful, creates a launcher script that embeds it (so that it can be run interactively outside of Gradle)
example: with this build script, I can run
Copy code
$ ./gradlew repl && build/repl
BUILD SUCCESSFUL in 1s
4 actionable tasks: 1 executed, 3 up-to-date
ki-shell 0.5.2/1.8.0
type :h for help
[0]
and have access to code in
src/main/kotlin/**
from there
of course you might want to customize it more (or make changes for Windows compatibility) but the basic idea is pretty simple
g
Wow holy smokes, thank you!