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
@@ -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")
}
*/