Scripting builders (does not work yet)

This commit is contained in:
Alexander Nozik 2019-02-05 16:49:35 +03:00
parent 128f747558
commit c06244465c
3 changed files with 73 additions and 3 deletions

View File

@ -8,12 +8,20 @@ kotlin {
val commonMain by getting {
dependencies {
api(project(":dataforge-workspace"))
implementation("org.jetbrains.kotlin:kotlin-scripting-common")
implementation(kotlin("scripting-common"))
}
}
val jvmMain by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm-host")
implementation(kotlin("scripting-jvm-host-embeddable"))
implementation(kotlin("scripting-jvm"))
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
implementation("ch.qos.logback:logback-classic:1.2.3")
}
}
}

View File

@ -1,7 +1,41 @@
package hep.dataforge.scripting
import hep.dataforge.context.Global
import hep.dataforge.workspace.Workspace
import hep.dataforge.workspace.WorkspaceBuilder
import java.io.File
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
fun buildWorkspace(file: File) {
object Builders {
fun buildWorkspace(source: SourceCode): Workspace {
val builder = WorkspaceBuilder(Global)
val workspaceScriptConfiguration = ScriptCompilationConfiguration {
baseClass(Any::class)
implicitReceivers(WorkspaceBuilder::class)
jvm{
dependenciesFromCurrentContext()
}
}
val evaluationConfiguration = ScriptEvaluationConfiguration {
implicitReceivers(builder)
}
val res = BasicJvmScriptingHost().eval(source, workspaceScriptConfiguration, evaluationConfiguration)
res.reports.forEach{ scriptDiagnostic ->
scriptDiagnostic.exception?.let { throw it }
}
return builder.build()
}
fun buildWorkspace(file: File): Workspace = buildWorkspace(file.toScriptSource())
fun buildWorkspace(string: String): Workspace = buildWorkspace(string.toScriptSource())
}

View File

@ -0,0 +1,28 @@
package hep.dataforge.scripting
import hep.dataforge.meta.get
import hep.dataforge.meta.int
import org.junit.Test
import kotlin.test.assertEquals
class BuildersKtTest {
@Test
fun testWorkspaceBuilder() {
val script = """
println("I am working")
context{
name = "test"
}
target("testTarget"){
"a" to 12
}
""".trimIndent()
val workspace = Builders.buildWorkspace(script)
val target = workspace.targets.getValue("testTarget")
assertEquals(12, target["a"]!!.int)
}
}