Skip to content

Convenient kotlin script wrapper

License

Notifications You must be signed in to change notification settings

y9vad9/script.kt

Repository files navigation

Maven metadata URL

script.kt

Convenient Kotlin Script wrapper. The main goal of this library is to simplify work with kotlin scripting.

Examples

Type-safe DSL

Simple DSL for scripts that do not need any customization, but just type-safe code.

moduleKts(mainScript = scriptOf(code = "..")) {
    setBaseClass<Foo>(arguments = listOf(Bar()))
    addImplicitReceiver<FooBar>(instance = FooBar())
}

External features

If you need to customize somehow your compilation you can use features system:

moduleKt(mainScript) {
    compilation.install(MavenDependenciesResolver)
    evaluation.install(/* some evaluation feature */)
}

Step-by-step configuration

If you need to divide your scripting processes you can use modular system:

val module = moduleOf(mainScript = scriptOf(code = "..."))
module.compilationConfiguration.apply { // for current state it is mutable
    baseClass = Foo::class
}
// now let's compile it
val compileResult = module.compile(compiler = JvmHostModuleCompiler())
if (compileResult.isError())
    throw RuntimeException("stub!")

val compiledModule = compileResult.valueOrThrow() // for this state compilation configuration is immutable
// let's add arguments of class `Foo`
compiledModule.evaluationConfiguration.apply { // it is mutable for current state
    baseClassArguments = listOf("...")
}

val evaluationResult = compiledModule.evaluate(evaluator = JvmHostModuleEvaluator())
if (evaluationResult.isError())
    throw RuntimeException("stub!")

// let's print our results
println((compileResult.reports + evaluationResult.reports).joinToString("\n") { it.render() })

Implementation

For first, we need to add repository:

repositories {
    maven("https://maven.y9vad9.com")
}

And then we need to add dependency:

dependencies {
    implementation("com.y9vad9.scriptkt:scriptkt:$version")
    // builtins (optional)
    implementation("com.y9vad9.scriptkt:maven-resolver:$version")
    implementation("com.y9vad9.scriptkt:import-script:$version")
}