Hello, I am wondering how to set up a scripting en...
# scripting
r
Hello, I am wondering how to set up a scripting environment so that every file with a certain extension are wrapped inside a class and has default imports with autocomplete in the ide. I have not been able to find adequate resources for something like this online and have only at most been able to load kts files manually.
i
I'm not sure I understand your request, there is no support for wrapping script inside a class. But to some extend a custom script definition may provide you some properties of such a construct, namely you can define implicit receivers (sort of wrapping) and default imports, and such definition could be used in the IDE, so autocompletion will work too. You may have a look at these examples - https://github.com/Kotlin/kotlin-script-examples/tree/master/jvm/simple-main-kts
r
Hello ilya sorry this is 11 days later. I have found 2 jetbrains packages that might help me out but I cannot find any examples of how to use them to get started on achieving what I am asking. Maybe I am not looking in the right spots!
Copy code
org.jetbrains.kotlin:kotlin-scripting-common
org.jetbrains.kotlin:kotlin-script-runtime
More specifically I have been looking at
kotlin.script.experimental.annotations.KotlinScript
as it seems to be what I am looking for. Looking at it and
kotlin.script.experimental.api.ScriptCompilationConfiguration
lead me to believe I can achieve what I asked about, assuming it is set up properly with the build system (which I am also new to, advanced building systems like gradle and maven), which would be to say create a custom configuration
ScriptCompilationConfiguration
that imports certain things by default and only import them by default when inside a file of a certain extension which you can define
KotlinScript.fileExtension
For example I have
abstract class Dog { fun woof() { println("woof") }
which I want to set as one of the
ScriptCompilationConfiguration.implicitReceivers
and be able to access it's insides in a file with
dog.kts
extension and I can use it at runtime by passing a
Dog
to it. I'm not sure if I am explaining this properly, but I know what I am trying to achieve I just don't know how to achieve it
i
Hi @RayeW47. Sorry, I overlooked your response. Please mention me next time, so I can get the Slack notification. Please have a look at the example I linked above. It is a complete library and test project that demonstrates the things you need (so it seems to me) and many on top. In particular, in this line - https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/scriptDef.kt#L41 it adds an implicit receiver of the type
String
(which is not a good example indeed, I have to replace it with something more sensible, but it shows the mechanics anyway for now). And in this line - https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/scriptDef.kt#L85 the evaluation configuration defines actual object passed as this receiver (empty string in this case). You may change the configuration dynamically, e.g. you can configure a callback (as in this line - https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/scriptDef.kt#L86) or you can pass a base evaluation configuration to
eval
.
r
Thank you for the links @ilya.chernikov. Sorry, I have not used slack much at all so I didn't understand that you had to mention for a notification. That is exactly what I am looking for, I'm not sure how I didn't find such a thing that is right there on GitHub.
👍 1