Few more updates for context builder

This commit is contained in:
Alexander Nozik 2019-06-30 14:13:45 +03:00
parent 1722a7dd82
commit 167dc752ea
3 changed files with 15 additions and 6 deletions

View File

@ -148,14 +148,18 @@ object Global : Context("GLOBAL", null) {
private val contextRegistry = HashMap<String, Context>()
/**
* Get previously builder context o builder a new one
* Get previously built context
*
* @param name
* @return
*/
fun getContext(name: String): Context {
return contextRegistry.getOrPut(name) { Context(name) }
fun getContext(name: String): Context? {
return contextRegistry[name]
}
fun context(name: String, parent: Context = this, block: ContextBuilder.() -> Unit): Context =
ContextBuilder(name, parent).apply(block).build()
}

View File

@ -18,11 +18,15 @@ class ContextBuilder(var name: String = "@anonimous", val parent: Context = Glob
plugins.add(plugin)
}
fun plugin(tag: PluginTag, action: MetaBuilder.() -> Unit) {
fun plugin(tag: PluginTag, action: MetaBuilder.() -> Unit = {}) {
plugins.add(PluginRepository.fetch(tag, buildMeta(action)))
}
fun plugin(name: String, group: String = "", version: String = "", action: MetaBuilder.() -> Unit) {
fun plugin(builder: PluginFactory<*>, action: MetaBuilder.() -> Unit = {}) {
plugins.add(builder.invoke(buildMeta(action)))
}
fun plugin(name: String, group: String = "", version: String = "", action: MetaBuilder.() -> Unit = {}) {
plugin(PluginTag(name, group, version), action)
}

View File

@ -142,7 +142,8 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
val loaded = get(type, recursive = false)
return when {
loaded == null -> {
val plugin = PluginRepository.list().first { it.type == type }.invoke(meta)
val plugin = PluginRepository.list().find { it.type == type }?.invoke(meta)
?: error("Plugin factory for class $type not found")
if (type.isInstance(plugin)) {
@Suppress("UNCHECKED_CAST")
load(plugin as T)