Hey all, I have colleagues who don't use IntelliJ...
# gradle
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
files or `repl`'s. Is there a way I can make a custom Gradle task in our
that launches a
script that has our project code and library modules on the classpath for them? Thank you!
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
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
You could check an example here how to run kts
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
  -Dkotlin.repl.ideMode=true -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8
  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
were empty, I built a whole application to monitor
for new files and log the contents of the
for nothing!
through the tooling API
but there's no argument or anything there, it seems to just run this: `
Copy code
  -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 🤔
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
4 actionable tasks: 1 executed, 3 up-to-date
ki-shell 0.5.2/1.8.0
type :h for help
and have access to code in
from there
of course you might want to customize it more (or make changes for Windows compatibility) but the basic idea is pretty simple
Wow holy smokes, thank you!