From c06244465ca00864cd30d2c5bbef030b9f31844c Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 5 Feb 2019 16:49:35 +0300 Subject: [PATCH] Scripting builders (does not work yet) --- dataforge-scripting/build.gradle.kts | 12 +++++-- .../hep/dataforge/scripting/Builders.kt | 36 ++++++++++++++++++- .../hep/dataforge/scripting/BuildersKtTest.kt | 28 +++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 dataforge-scripting/src/jvmTest/kotlin/hep/dataforge/scripting/BuildersKtTest.kt diff --git a/dataforge-scripting/build.gradle.kts b/dataforge-scripting/build.gradle.kts index 9c37715d..674ec25f 100644 --- a/dataforge-scripting/build.gradle.kts +++ b/dataforge-scripting/build.gradle.kts @@ -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") } } } diff --git a/dataforge-scripting/src/jvmMain/kotlin/hep/dataforge/scripting/Builders.kt b/dataforge-scripting/src/jvmMain/kotlin/hep/dataforge/scripting/Builders.kt index ffe5569a..452b3fe2 100644 --- a/dataforge-scripting/src/jvmMain/kotlin/hep/dataforge/scripting/Builders.kt +++ b/dataforge-scripting/src/jvmMain/kotlin/hep/dataforge/scripting/Builders.kt @@ -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()) } \ No newline at end of file diff --git a/dataforge-scripting/src/jvmTest/kotlin/hep/dataforge/scripting/BuildersKtTest.kt b/dataforge-scripting/src/jvmTest/kotlin/hep/dataforge/scripting/BuildersKtTest.kt new file mode 100644 index 00000000..ef58de78 --- /dev/null +++ b/dataforge-scripting/src/jvmTest/kotlin/hep/dataforge/scripting/BuildersKtTest.kt @@ -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) + } +} \ No newline at end of file