KScript
Convenient kotlin script wrapper for JVM.
How it works
Base class
open class Test(val test: String) // this class will be super class of script.
KScript("println(test)").apply {
configuration.addClasspath(classpathFromClassOrException(Test::class))
configuration.setBaseClass<Test>(arguments = arrayOf("test"))
}.eval()
External dependencies
By default, library uses KScriptMavenResolver, but we can change it:
val script = """...""".toKScript()
script.configuration.externalResolvers.mavenResolver = KScriptMavenResolver() // one of the default implementations.
script.eval()
Also, we can cache it:
script.configuration.externalResolver = KScriptCacheableMavenResolver(File("path_to_cache_folder"))
Build caching
val script = "...".toKScript()
script.configuration.buildCacheDirectory = File(path)
script.eval()
Caching depends on the code (based on it, md5 is generated with a cache build) and in order to update the cache, you will need to delete the cache file / folder or update something in the script.
External Scripts
Library provides API to add external scripts into your script runtime. You can do it by includeScript
:
val script = text.toKScript()
script.includeScript(other.toKScriptSource())
Or directly in script using @ImportFile
annotation:
@file:ImportFile("other.kts")
// ...
// ...
funFromOtherScript()
Implementation
For now not everything is implemented, but it will be available upon request.
repositories {
maven("https://maven.kotlingang.fun")
}
dependencies {
implementation("fun.kotlingang.kscript:kscript:$version")
}