Implement simplified property bag dsl:

move it to the separate jar,
use property bag chaining
This commit is contained in:
Ilya Chernikov
2018-03-27 23:49:07 +02:00
parent 495c75c48e
commit 2ddcc280a9
20 changed files with 124 additions and 119 deletions
@@ -6,7 +6,7 @@
package org.jetbrains.kotlin.script.examples.jvm.resolve.maven.host
import org.jetbrains.kotlin.script.examples.jvm.resolve.maven.MyScriptWithMavenDeps
import org.jetbrains.kotlin.script.examples.jvm.resolve.maven.myJvmConfig
import org.jetbrains.kotlin.script.examples.jvm.resolve.maven.myJvmConfigParams
import java.io.File
import kotlin.script.experimental.api.*
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
@@ -28,7 +28,7 @@ fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
scriptDefinition.evaluator
)
return host.eval(scriptFile.toScriptSource(), myJvmConfig(), ScriptEvaluationEnvironment())
return host.eval(scriptFile.toScriptSource(), ChainedPropertyBag(null, myJvmConfigParams), ScriptEvaluationEnvironment())
}
fun main(vararg args: String) {
@@ -18,6 +18,7 @@ import kotlin.script.experimental.annotations.KotlinScriptEvaluator
import kotlin.script.experimental.api.*
import kotlin.script.experimental.jvm.*
import kotlin.script.experimental.jvm.runners.BasicJvmScriptEvaluator
import kotlin.script.experimental.misc.*
@KotlinScript
@KotlinScriptCompilationConfigurator(MyConfigurator::class)
@@ -26,32 +27,30 @@ abstract class MyScriptWithMavenDeps {
// abstract fun body(vararg args: String): Int
}
inline fun myJvmConfig(
from: ChainedPropertyBag = ChainedPropertyBag(),
crossinline body: JvmScriptCompileConfigurationParams.Builder.() -> Unit = {}
) = jvmConfigWithJavaHome(from) {
signature<MyScriptWithMavenDeps>()
ScriptCompileConfigurationParams.importedPackages(listOf(DependsOn::class.qualifiedName!!, Repository::class.qualifiedName!!))
dependencies(
scriptCompilationClasspathFromContext(
"script", // script library jar name
"kotlin-script-util" // DependsOn annotation is taken from script-util
)
val myJvmConfigParams = jvmJavaHomeParams + with(ScriptCompileConfigurationParams) {
listOf(
scriptSignature to ScriptSignature(MyScriptWithMavenDeps::class, ProvidedDeclarations()),
importedPackages(DependsOn::class.qualifiedName!!, Repository::class.qualifiedName!!),
dependencies(
JvmDependency(
scriptCompilationClasspathFromContext(
"script", // script library jar name
"kotlin-script-util" // DependsOn annotation is taken from script-util
)
)
),
updateConfigurationOnAnnotations(DependsOn::class, Repository::class)
)
ScriptCompileConfigurationParams.updateConfigurationOnAnnotations(listOf(DependsOn::class, Repository::class))
body()
}
class MyConfigurator(val environment: ChainedPropertyBag) : ScriptCompilationConfigurator {
private val resolver = FilesAndMavenResolver()
override val defaultConfiguration = myJvmConfig {
add(ScriptCompileConfigurationParams.baseClass to environment[ScriptingEnvironmentProperties.baseClass])
}
override val defaultConfiguration = ChainedPropertyBag(environment, myJvmConfigParams)
override suspend fun baseConfiguration(scriptSource: ScriptSource): ResultWithDiagnostics<ScriptCompileConfiguration> =
myJvmConfig(defaultConfiguration) { add(scriptSource.toConfigEntry()) }.asSuccess()
defaultConfiguration.asSuccess()
override suspend fun refineConfiguration(
configuration: ScriptCompileConfiguration,
@@ -76,7 +75,7 @@ class MyConfigurator(val environment: ChainedPropertyBag) : ScriptCompilationCon
val newDependency = JvmDependency(resolvedClasspath)
val updatedDeps =
configuration.getOrNull(ScriptCompileConfigurationParams.dependencies)?.plus(newDependency) ?: listOf(newDependency)
configuration.cloneWith(ScriptCompileConfigurationParams.dependencies to updatedDeps).asSuccess(diagnostics)
ChainedPropertyBag(configuration, ScriptCompileConfigurationParams.dependencies(updatedDeps)).asSuccess(diagnostics)
} catch (e: Throwable) {
ResultWithDiagnostics.Failure(*diagnostics.toTypedArray(), e.asDiagnostics())
}
@@ -1 +1 @@
import org.jetbrains.kotlin.gradle.dsl.Coroutines
import org.jetbrains.kotlin.gradle.dsl.Coroutines
@@ -13,14 +13,13 @@ import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseC
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.*
import kotlin.script.experimental.jvmhost.impl.KJVMCompilerImpl
import kotlin.script.experimental.misc.*
inline fun myJvmConfig(
from: ChainedPropertyBag = ChainedPropertyBag(),
crossinline body: JvmScriptCompileConfigurationParams.Builder.() -> Unit = {}
) = jvmConfigWithJavaHome(from) {
signature<MyScript>()
dependencies(scriptCompilationClasspathFromContext("script" /* script library jar name */))
body()
val myJvmConfigParams = jvmJavaHomeParams + with(ScriptCompileConfigurationParams) {
listOf(
scriptSignature to ScriptSignature(MyScript::class, ProvidedDeclarations()),
dependencies(JvmDependency(scriptCompilationClasspathFromContext("script" /* script library jar name */)))
)
}
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
@@ -35,7 +34,7 @@ fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
scriptDefinition.evaluator
)
return host.eval(scriptFile.toScriptSource(), myJvmConfig(), ScriptEvaluationEnvironment())
return host.eval(scriptFile.toScriptSource(), ChainedPropertyBag(null, myJvmConfigParams), ScriptEvaluationEnvironment())
}
fun main(vararg args: String) {
@@ -30,11 +30,5 @@ object ScriptCompileConfigurationParams {
val updateConfigurationOnAnnotations by typedKey<Iterable<KClass<out Annotation>>>()
val updateConfigurationOnSections by typedKey<Iterable<String>>()
open class Builder(parentBuilder: PropertyBagBuilder? = null) : PropertyBagBuilder(parentBuilder) {
inline fun <reified T> signature(providedDeclarations: ProvidedDeclarations = ProvidedDeclarations.Empty) {
add(scriptSignature to ScriptSignature(T::class, providedDeclarations))
}
}
}
@@ -7,12 +7,10 @@
package kotlin.script.experimental.api
object ScriptDefinitionProperties : PropertiesBase<ScriptDefinitionProperties>() {
object ScriptDefinitionProperties {
val name by typedKey<String>()
val fileExtension by typedKey<String>()
open class Builder(parentBuilder: PropertyBagBuilder? = null) : PropertyBagBuilder(parentBuilder)
}
@@ -7,21 +7,16 @@
package kotlin.script.experimental.api
open class ScriptEvaluationEnvironmentParams : PropertyBagBuilder() {
companion object {
val implicitReceivers by typedKey<List<Any>>()
object ScriptEvaluationEnvironmentParams {
val implicitReceivers by typedKey<List<Any>>()
val contextVariables by typedKey<Map<String, Any?>>() // external variables
val contextVariables by typedKey<Map<String, Any?>>() // external variables
val constructorArgs by typedKey<List<Any?>>()
val constructorArgs by typedKey<List<Any?>>()
val runArgs by typedKey<List<Any?>>()
}
val runArgs by typedKey<List<Any?>>()
}
inline fun scriptEvaluationEnvironment(from: ChainedPropertyBag = ChainedPropertyBag(), body: ScriptEvaluationEnvironmentParams.() -> Unit) =
ScriptEvaluationEnvironmentParams().build(from, body)
typealias ScriptEvaluationEnvironment = ChainedPropertyBag
data class EvaluationResult(val returnValue: Any?, val environment: ScriptEvaluationEnvironment)
@@ -5,13 +5,8 @@
package kotlin.script.experimental.api
class ProcessedScriptDataParams : PropertyBagBuilder() {
companion object {
val annotations by typedKey<Iterable<Annotation>>()
object ProcessedScriptDataParams {
val annotations by typedKey<Iterable<Annotation>>()
val fragments by typedKey<Iterable<ScriptSourceNamedFragment>>()
}
val fragments by typedKey<Iterable<ScriptSourceNamedFragment>>()
}
inline fun processedScriptData(from: ChainedPropertyBag = ChainedPropertyBag(), body: ProcessedScriptDataParams.() -> Unit) =
ProcessedScriptDataParams().build(from, body)
@@ -6,7 +6,6 @@
package kotlin.script.experimental.api
import kotlin.reflect.KProperty
import kotlin.reflect.full.primaryConstructor
data class TypedKey<T>(val name: String)
@@ -16,41 +15,23 @@ class TypedKeyDelegate<T> {
fun <T> typedKey() = TypedKeyDelegate<T>()
class ChainedPropertyBag(private val parent: ChainedPropertyBag? = null, private val data: Map<TypedKey<*>, Any?> = hashMapOf()) {
constructor(data: Map<TypedKey<*>, Any?>) : this(null, data)
constructor(parent: ChainedPropertyBag?, pairs: Iterable<Pair<TypedKey<*>, Any?>>) : this(
parent,
HashMap<TypedKey<*>, Any?>().also {
it.putAll(pairs)
})
class ChainedPropertyBag(private val parent: ChainedPropertyBag? = null, pairs: Iterable<Pair<TypedKey<*>, Any?>>) {
constructor(parent: ChainedPropertyBag, vararg pairs: Pair<TypedKey<*>, Any?>) : this(parent, pairs.asIterable())
constructor(vararg pairs: Pair<TypedKey<*>, Any?>) : this(null, pairs.asIterable())
constructor(parent: ChainedPropertyBag?, vararg pairs: Pair<TypedKey<*>, Any?>) : this(parent, hashMapOf(*pairs))
private val data = HashMap<TypedKey<*>, Any?>().also { it.putAll(pairs) }
operator fun <T> get(key: TypedKey<T>): T =
inline operator fun <reified T> get(key: TypedKey<T>): T = getUnchecked(key) as T
fun <T> getUnchecked(key: TypedKey<T>): Any? =
when {
data.containsKey(key) -> data[key] as T
parent != null -> parent[key]
data.containsKey(key) -> data[key]
parent != null -> parent.getUnchecked(key)
else -> throw IllegalArgumentException("Unknown key $key")
}
fun <T> getOrNull(key: TypedKey<T>): T? = data[key] as T? ?: parent?.getOrNull(key)
inline fun <reified T> getOrNull(key: TypedKey<T>): T? = getOrNullUnchecked(key)?.let { it as T }
fun <T> getOrNullUnchecked(key: TypedKey<T>): Any? = data[key] ?: parent?.getOrNullUnchecked(key)
}
open class PropertyBagBuilder(private val parentBuilder: PropertyBagBuilder? = null) {
val pairs: MutableList<Pair<TypedKey<*>, Any?>> = arrayListOf()
open operator fun <T> TypedKey<T>.invoke(v: T) {
pairs.add(this to v)
}
fun add(pair: Pair<TypedKey<*>, Any?>) {
pairs.add(pair)
}
fun getAllPairs() = if (parentBuilder == null) pairs else parentBuilder.pairs + pairs
}
inline fun <T : PropertyBagBuilder> T.build(parent: ChainedPropertyBag? = null, body: T.() -> Unit): ChainedPropertyBag {
body()
return ChainedPropertyBag(parent, getAllPairs())
}
@@ -11,7 +11,5 @@ object ScriptingEnvironmentProperties {
// required by definitions that extract data from script base class annotations
val baseClass by typedKey<KClass<*>>()
open class Builder(parentBuilder: PropertyBagBuilder? = null) : PropertyBagBuilder(parentBuilder)
}
@@ -35,7 +35,7 @@ open class ScriptDefinitionFromAnnotatedBaseClass(val environment: ChainedProper
if (properties.getOrNull(ScriptDefinitionProperties.name) == null) {
toAdd += ScriptDefinitionProperties.name to baseClass.simpleName!!
}
properties.cloneWith(toAdd)
ChainedPropertyBag(properties, toAdd)
}
override val compilationConfigurator =
@@ -87,7 +87,8 @@ class KJVMCompilerImpl : KJVMCompilerProxy {
val updatedDeps = updatedScriptCompileConfiguration.getOrNull(ScriptCompileConfigurationParams.dependencies)?.plus(
JvmDependency(classpath)
) ?: listOf(JvmDependency(classpath))
updatedScriptCompileConfiguration = updatedScriptCompileConfiguration.cloneWith(
updatedScriptCompileConfiguration = ChainedPropertyBag(
updatedScriptCompileConfiguration,
ScriptCompileConfigurationParams.dependencies to updatedDeps
)
}
@@ -38,7 +38,8 @@ class BridgeDependenciesResolver(
val processedScriptData =
ProcessedScriptData(ProcessedScriptDataParams.annotations to scriptContents.annotations)
val scriptCompilerConfiguration = baseScriptCompilerConfiguration.cloneWith(
val scriptCompilerConfiguration = ChainedPropertyBag(
baseScriptCompilerConfiguration,
ScriptCompileConfigurationParams.scriptSourceFragments to scriptContents.toScriptSourceFragments()
)
@@ -8,16 +8,10 @@ package kotlin.script.experimental.jvm
import kotlinx.coroutines.experimental.runBlocking
import kotlin.script.experimental.api.*
import java.io.File
import kotlin.script.experimental.jvm.JvmScriptCompileConfigurationParams.javaHomeDir
inline fun jvmConfigWithJavaHome(
from: ChainedPropertyBag = ChainedPropertyBag(),
crossinline body: JvmScriptCompileConfigurationParams.Builder.() -> Unit = {}
) =
jvmScriptConfiguration(from) {
javaHomeDir(File(System.getProperty("java.home")))
body()
}
val jvmJavaHomeParams = with(JvmScriptCompileConfigurationParams) {
listOf(javaHomeDir to File(System.getProperty("java.home")))
}
val ScriptCompilationConfigurator?.defaultConfiguration: ScriptCompileConfiguration
get() = this?.let { runBlocking { defaultConfiguration } } ?: ScriptCompileConfiguration()
@@ -6,19 +6,12 @@
package kotlin.script.experimental.jvm
import java.io.File
import kotlin.script.experimental.api.*
import kotlin.script.experimental.api.ScriptDependency
import kotlin.script.experimental.api.typedKey
object JvmScriptCompileConfigurationParams {
val javaHomeDir by typedKey<File>()
open class Builder : ScriptCompileConfigurationParams.Builder() {
fun dependencies(vararg classpath: Iterable<File>) =
add(ScriptCompileConfigurationParams.dependencies to classpath.map(::JvmDependency))
}
}
inline fun jvmScriptConfiguration(from: ChainedPropertyBag = ChainedPropertyBag(), body: JvmScriptCompileConfigurationParams.Builder.() -> Unit) =
JvmScriptCompileConfigurationParams.Builder().build(from, body)
class JvmDependency(val classpath: Iterable<File>) : ScriptDependency
@@ -7,7 +7,9 @@
package kotlin.script.experimental.jvm
import kotlin.script.experimental.api.*
import kotlin.script.experimental.api.ScriptCompilationConfigurator
import kotlin.script.experimental.api.ScriptEvaluator
import kotlin.script.experimental.api.typedKey
import kotlin.script.experimental.host.BasicScriptingHost
open class JvmBasicScriptingHost<ScriptBase : Any>(
@@ -16,11 +18,6 @@ open class JvmBasicScriptingHost<ScriptBase : Any>(
evaluator: ScriptEvaluator<ScriptBase>
) : BasicScriptingHost<ScriptBase>(configurationExtractor, compiler, evaluator)
class JvmScriptEvaluationEnvironmentParams : ScriptEvaluationEnvironmentParams() {
companion object {
val baseClassLoader by typedKey<ClassLoader?>()
}
object JvmScriptEvaluationEnvironmentParams {
val baseClassLoader by typedKey<ClassLoader?>()
}
inline fun jvmScriptEvaluationEnvironment(from: ChainedPropertyBag = ChainedPropertyBag(), body: JvmScriptEvaluationEnvironmentParams.() -> Unit) =
JvmScriptEvaluationEnvironmentParams().build(from, body)
+28
View File
@@ -0,0 +1,28 @@
import org.jetbrains.kotlin.gradle.dsl.Coroutines
plugins {
kotlin("jvm")
}
jvmTarget = "1.6"
dependencies {
compile(project(":kotlin-script-runtime"))
compile(projectDist(":kotlin-stdlib"))
compile(project(":kotlin-scripting-common"))
}
sourceSets {
"main" { projectDefault() }
"test" {}
}
kotlin.experimental.coroutines = Coroutines.ENABLE
dist()
ideaPlugin()
standardPublicJars()
publish()
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.script.experimental.misc
import kotlin.reflect.KClass
import kotlin.script.experimental.api.TypedKey
inline operator fun <reified T> TypedKey<T>.invoke(v: T): Pair<TypedKey<T>, T> = this to v
inline operator fun <reified K> TypedKey<KClass<*>>.invoke(): Pair<TypedKey<KClass<*>>, KClass<*>> = this to K::class
inline operator fun <reified E> TypedKey<List<E>>.invoke(vararg vs: E): Pair<TypedKey<List<E>>, List<E>> = this to vs.toList()
@JvmName("invokeIterable")
inline operator fun <reified E> TypedKey<Iterable<E>>.invoke(vararg vs: E): Pair<TypedKey<Iterable<E>>, Iterable<E>> = this to vs.toList()
// TODO: make tests from examples below
/*
val x = with(kotlin.script.experimental.api.ScriptingEnvironmentProperties) {
baseClass<String>()
}
val y = with(kotlin.script.experimental.api.ScriptCompileConfigurationParams) {
importedPackages("a1", "a2")
}
*/
+2
View File
@@ -151,6 +151,7 @@ include ":kotlin-build-common",
":kotlin-annotations-jvm",
":kotlin-annotations-android",
":kotlin-scripting-common",
":kotlin-scripting-misc",
":kotlin-scripting-jvm",
":kotlin-scripting-jvm-host",
":kotlin-scripting-compiler",
@@ -255,6 +256,7 @@ project(':examples:kotlin-jsr223-daemon-local-eval-example').projectDir = "$root
project(':kotlin-annotations-jvm').projectDir = "$rootDir/libraries/tools/kotlin-annotations-jvm" as File
project(':kotlin-annotations-android').projectDir = "$rootDir/libraries/tools/kotlin-annotations-android" as File
project(':kotlin-scripting-common').projectDir = "$rootDir/libraries/scripting/common" as File
project(':kotlin-scripting-misc').projectDir = "$rootDir/libraries/scripting/misc" as File
project(':kotlin-scripting-jvm').projectDir = "$rootDir/libraries/scripting/jvm" as File
project(':kotlin-scripting-jvm-host').projectDir = "$rootDir/libraries/scripting/jvm-host" as File
project(':kotlin-scripting-compiler').projectDir = "$rootDir/plugins/scripting/scripting-cli" as File