Or is there a way to precompile kts file with the ...
# scripting
s
Or is there a way to precompile kts file with the regular Gradle compiler (like kt files) in order to make the compiler optional and use it just for advanced use cases (dynamic or remote kts loading) ?
g
I wouldn't expect there to be a "lightweight" compiler, you're either compiling kotlin code or not. The interactions in your script need to call up to the host environment somehow. If you want to build your own small DSLs maybe you can look to something like Xtext. You can compile .kts files to JVM bytecode ahead of time though: https://github.com/apollo-rsps/apollo/blob/kotlin-experiments/buildSrc/src/main/groovy/org/apollo/build/plugin/ApolloPluginExtension.groovy#L71-L73
s
Interesting thanks
Yeah ahead of time compilation of scripts is probably the way to achieve this
Maybe I can just add the kts file in Gradle sourceset ?
But the question is how I would get the result of script return value from kt since I would like to avoid using global
val
Any idea ?
g
what do you mean script return value?
these scripts dont return things, they get compiled into java classes
the contents of the script gets put in the constructor method of the class
since 1.2.50 you can make the compiler put the script in an implementation of an abstract method, but I don't think it can return things
s
By script return value I mean :
Copy code
import org.foo.bar.TestConfiguration

TestConfiguration(name = "world")
When using JSR 223 eval that return the
TestConfiguration
instance
That's something that
kts
scripts can do and I would like to avoid hardcoding a global
val
to retreive it if I compile the
kts
in order to avoid forcing everybody to bundle the huge compiler
i
Yes, I can confirm that you can compile classes ahead of time, just place it along with regular .kt files in a sourceset.
About return value - after calling script class constructor, you can access all variables you declared in the script as class properties. So you can write something like:
Copy code
val retVal = ...
at the end, and then use
Copy code
scriptInstance.retVal
The proper return value support is planned too, but it is not yet there.
👍 2
since 1.2.50 you can make the compiler put the script in an implementation of an abstract method
This is not yet implemented, unfortunately.
s
Is there an issue pour return value support I could vote for ?
i
Not that I’m aware of.