Add properties builders to the dsl, implement a builder for refineConfiguration
This commit is contained in:
@@ -6,12 +6,23 @@
|
||||
package kotlin.script.experimental.api
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.script.experimental.util.ChainedPropertyBag
|
||||
import kotlin.script.experimental.util.TypedKey
|
||||
|
||||
interface PropertiesGroup
|
||||
|
||||
|
||||
open class PropertiesBuilder(val props: ScriptingProperties)
|
||||
|
||||
class PropertiesBuilderDelegate<T: PropertiesBuilder>(val kclass: KClass<T>) {
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): KClass<T> = kclass
|
||||
}
|
||||
|
||||
inline fun <reified T : PropertiesBuilder> propertiesBuilder() = PropertiesBuilderDelegate(T::class)
|
||||
|
||||
|
||||
open class ScriptingProperties(body: ScriptingProperties.() -> Unit = {}) {
|
||||
|
||||
var parentPropertiesBag: ChainedPropertyBag? = null
|
||||
@@ -35,6 +46,12 @@ open class ScriptingProperties(body: ScriptingProperties.() -> Unit = {}) {
|
||||
|
||||
inline operator fun <T : PropertiesGroup> T.invoke(body: T.() -> Unit) = body()
|
||||
|
||||
// generic invoke for properties builder - for extending dsl with complex builders
|
||||
|
||||
inline operator fun <reified T : PropertiesBuilder> KClass<T>.invoke(body: T.() -> Unit) {
|
||||
constructors.first().call(this@ScriptingProperties).body()
|
||||
}
|
||||
|
||||
// chaining:
|
||||
|
||||
private fun chain(propsBag: ChainedPropertyBag?, propsBuilder: ScriptingProperties?, replaceParent: Boolean) {
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ object ProcessedScriptDataProperties : PropertiesGroup {
|
||||
val foundFragments by typedKey<List<ScriptSourceNamedFragment>>()
|
||||
}
|
||||
|
||||
interface RefineScriptCompilationConfiguration {
|
||||
interface RefineScriptCompilationConfigurationHandler {
|
||||
suspend operator fun invoke(
|
||||
scriptSource: ScriptSource,
|
||||
configuration: ScriptCompileConfiguration,
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package kotlin.script.experimental.api
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.script.experimental.util.ChainedPropertyBag
|
||||
import kotlin.script.experimental.util.typedKey
|
||||
|
||||
@@ -40,12 +41,59 @@ object ScriptDefinitionProperties : PropertiesGroup {
|
||||
|
||||
val compilerOptions by typedKey<List<String>>() // Q: CommonCompilerOptions instead?
|
||||
|
||||
val refineConfiguration by typedKey<RefineScriptCompilationConfiguration>() // dynamic configurator
|
||||
val refineConfigurationHandler by typedKey<RefineScriptCompilationConfigurationHandler>() // dynamic configurator
|
||||
|
||||
val refineBeforeParsing by typedKey<Boolean>() // default: false
|
||||
val refineConfigurationBeforeParsing by typedKey<Boolean>() // default: false
|
||||
|
||||
val refineConfigurationOnAnnotations by typedKey<List<KotlinType>>()
|
||||
|
||||
val refineConfigurationOnSections by typedKey<List<String>>()
|
||||
|
||||
// DSL:
|
||||
|
||||
val refineConfiguration by propertiesBuilder<RefineConfigurationBuilder>()
|
||||
}
|
||||
|
||||
// DSL --------------------
|
||||
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
class RefineConfigurationBuilder(props: ScriptingProperties) : PropertiesBuilder(props) {
|
||||
|
||||
inline operator fun invoke(body: RefineConfigurationBuilder.() -> Unit) {
|
||||
body()
|
||||
}
|
||||
|
||||
fun handler(fn: RefineScriptCompilationConfigurationHandler) {
|
||||
props.data[ScriptDefinitionProperties.refineConfigurationHandler] = fn
|
||||
}
|
||||
|
||||
fun beforeParsing(value: Boolean = true) {
|
||||
props.data[ScriptDefinitionProperties.refineConfigurationBeforeParsing] = value
|
||||
}
|
||||
|
||||
fun onAnnotations(annotations: Iterable<KotlinType>) {
|
||||
props.data.addToListProperty(ScriptDefinitionProperties.refineConfigurationOnAnnotations, annotations)
|
||||
}
|
||||
|
||||
fun onAnnotations(vararg annotations: KotlinType) {
|
||||
onAnnotations(annotations.asIterable())
|
||||
}
|
||||
|
||||
inline fun <reified T : Annotation> onAnnotations() {
|
||||
onAnnotations(KotlinType(T::class))
|
||||
}
|
||||
|
||||
fun onAnnotations(vararg annotations: KClass<out Annotation>) {
|
||||
onAnnotations(annotations.map { KotlinType(it) })
|
||||
}
|
||||
|
||||
fun onSections(sections: Iterable<String>) {
|
||||
props.data.addToListProperty(ScriptDefinitionProperties.refineConfigurationOnSections, sections)
|
||||
}
|
||||
|
||||
fun onSections(vararg sections: String) {
|
||||
props.data.addToListProperty(ScriptDefinitionProperties.refineConfigurationOnSections, *sections)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user