Setup props once, fix initialization problem
This commit is contained in:
+1
-1
@@ -27,7 +27,7 @@ abstract class MyScriptWithMavenDeps {
|
|||||||
// abstract fun body(vararg args: String): Int
|
// abstract fun body(vararg args: String): Int
|
||||||
}
|
}
|
||||||
|
|
||||||
object MyScriptProperties : ScriptingProperties() {
|
class MyScriptProperties : ScriptingProperties() {
|
||||||
override fun setup() {
|
override fun setup() {
|
||||||
scriptDefinition {
|
scriptDefinition {
|
||||||
defaultImports<DependsOn>()
|
defaultImports<DependsOn>()
|
||||||
|
|||||||
@@ -33,7 +33,12 @@ open class ScriptingProperties(body: ScriptingProperties.() -> Unit = {}) {
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
body()
|
body()
|
||||||
setup() // TODO: does it work?
|
setupOnce() // TODO: does it work?
|
||||||
|
}
|
||||||
|
|
||||||
|
private var isSetUp = false
|
||||||
|
fun setupOnce() {
|
||||||
|
if (!isSetUp) setup()
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun setup() {}
|
open fun setup() {}
|
||||||
@@ -78,7 +83,7 @@ open class ScriptingProperties(body: ScriptingProperties.() -> Unit = {}) {
|
|||||||
// inclusion:
|
// inclusion:
|
||||||
|
|
||||||
fun include(props: ScriptingProperties) {
|
fun include(props: ScriptingProperties) {
|
||||||
props.setup()
|
props.setupOnce()
|
||||||
data.putAll(props.data)
|
data.putAll(props.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -61,7 +61,7 @@ fun createScriptDefinitionFromAnnotatedBaseClass(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun scriptingPropsInstance(kclass: KClass<out ScriptingProperties>): ScriptingProperties = try {
|
fun scriptingPropsInstance(kclass: KClass<out ScriptingProperties>): ScriptingProperties = try {
|
||||||
kclass.objectInstance ?: kclass.createInstance().also { it.setup() }
|
kclass.objectInstance ?: kclass.createInstance().also { it.setupOnce() }
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
throw IllegalArgumentException(ILLEGAL_CONFIG_ANN_ARG, e)
|
throw IllegalArgumentException(ILLEGAL_CONFIG_ANN_ARG, e)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user