ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - s1monw1/KtsRunner: Library for executing .kts files from regular Kotlin code
Library for executing .kts files from regular Kotlin code - s1monw1/KtsRunner
Visit Site

GitHub - s1monw1/KtsRunner: Library for executing .kts files from regular Kotlin code

GitHub - s1monw1/KtsRunner: Library for executing .kts files from regular Kotlin code

KtsRunner

Build Status


KtsRunner is a light-weight tool that allows the execution of .kts (Kotlin Script) files from ordinary Kotlin programs. It's enabled by JSR 223 (Java Scripting Engines API).

Usage

Running a script from file system

A simple usage example for KtsRunner can be described as follows: The declaration of a class is placed in a .kts file, which is supposed to be loaded into a normal Kotlin program so that it can be processed further.

  1. Example class
data class ClassFromScript(val x: String)
  1. .kts file
import de.swirtz.ktsrunner.objectloader.ClassFromScript

ClassFromScript("I was created in kts")
  1. Code to load the object
val scriptReader = Files.newBufferedReader(Paths.get("path/classDeclaration.kts"))
val loadedObj: ClassFromScript = KtsObjectLoader().load<ClassFromScript>(scriptReader)
println(loadedObj.x)
// >> I was created in kts

As shown, the KtsObjectLoader can be used for executing a .kts script and getting its result. The example shows a script that creates an instance of the ClassFromScript type that is loaded via KtsObjectLoader and then processed in the regular program.

Executing scripts directly

The KtsObjectLoader also allows the evaluation of simple String based scripts:

val scriptContent = "5 + 10"
val fromScript: Int = KtsObjectLoader().load<Int>(scriptContent))
println(fromScript)
// >> 15

Application Area

You might want to use KtsRunner when some part of your application's source has to be outsourced from the regular code. As an example, you can think of an application that provides a test suite runtime. The actual test cases are provided by a QA team which writes their test scripts using a domain specific language that is provided by the main application. Since you don't want QA to add source files (defining new test cases) to your application all the time, the test case creation is made via independent .kts (Kotlin Scripting) files in which the DSL is being utilized. The test suite main application can use the presented KtsRunner library for loading the test cases provided in .kts files and process them further afterward.

Controlling the ClassLoader

When instantiating an KtsObjectLoader, you can provide an explicit classloader as shown in this test case:

 @Test
    fun `when passing a custom classloader, it should be used when loading the script`() {
        val myCl = object : ClassLoader() {
            override fun loadClass(name: String?): Class<*> {
                throw IllegalStateException()
            }
        }
       assertExceptionThrownBy<IllegalStateException> {
           KtsObjectLoader(myCl).load("anything")
       }
    }

Getting Started

In your Gradle build, simply include the following repository and dependency:

maven { 
    setUrl("https://dl.bintray.com/s1m0nw1/KtsRunner")
}
dependencies {
    //...
    implementation("de.swirtz:ktsRunner:0.0.x")
}

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory