Removed configuration from plugins.

This commit is contained in:
Alexander Nozik 2019-05-01 16:46:09 +03:00
parent a56fa74ab6
commit f284d15ee4
8 changed files with 24 additions and 32 deletions

View File

@ -1,4 +1,4 @@
val dataforgeVersion by extra("0.1.2-dev-6") val dataforgeVersion by extra("0.1.2-dev-7")
allprojects { allprojects {
repositories { repositories {

View File

@ -1,16 +1,15 @@
package hep.dataforge.context package hep.dataforge.context
import hep.dataforge.meta.Config import hep.dataforge.meta.EmptyMeta
import hep.dataforge.meta.Meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
abstract class AbstractPlugin : Plugin { abstract class AbstractPlugin(override val meta: Meta = EmptyMeta) : Plugin {
private var _context: Context? = null private var _context: Context? = null
override val context: Context override val context: Context
get() = _context ?: error("Plugin $tag is not attached") get() = _context ?: error("Plugin $tag is not attached")
override val config = Config()
override fun attach(context: Context) { override fun attach(context: Context) {
this._context = context this._context = context
} }
@ -19,8 +18,6 @@ abstract class AbstractPlugin : Plugin {
this._context = null this._context = null
} }
//TODO make configuration activation-safe
override fun provideTop(target: String, name: Name): Any? = null override fun provideTop(target: String, name: Name): Any? = null
override fun listTop(target: String): Sequence<Name> = emptySequence() override fun listTop(target: String): Sequence<Name> = emptySequence()

View File

@ -1,8 +1,7 @@
package hep.dataforge.context package hep.dataforge.context
import hep.dataforge.meta.Config
import hep.dataforge.meta.MetaBuilder import hep.dataforge.meta.MetaBuilder
import hep.dataforge.meta.configure import hep.dataforge.meta.buildMeta
/** /**
* A convenience builder for context * A convenience builder for context
@ -19,11 +18,11 @@ class ContextBuilder(var name: String = "@anonimous", val parent: Context = Glob
plugins.add(plugin) plugins.add(plugin)
} }
fun plugin(tag: PluginTag, action: Config.() -> Unit) { fun plugin(tag: PluginTag, action: MetaBuilder.() -> Unit) {
plugins.add(PluginRepository.fetch(tag).configure(action)) plugins.add(PluginRepository.fetch(tag, buildMeta(action)))
} }
fun plugin(name: String, group: String = "", version: String = "", action: Config.() -> Unit) { fun plugin(name: String, group: String = "", version: String = "", action: MetaBuilder.() -> Unit) {
plugin(PluginTag(name, group, version), action) plugin(PluginTag(name, group, version), action)
} }

View File

@ -1,6 +1,5 @@
package hep.dataforge.context package hep.dataforge.context
import hep.dataforge.meta.Configurable
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaRepr import hep.dataforge.meta.MetaRepr
import hep.dataforge.meta.buildMeta import hep.dataforge.meta.buildMeta
@ -22,7 +21,7 @@ import hep.dataforge.provider.Provider
* *
* @author Alexander Nozik * @author Alexander Nozik
*/ */
interface Plugin : Named, ContextAware, Provider, MetaRepr, Configurable { interface Plugin : Named, ContextAware, Provider, MetaRepr {
/** /**
* Get tag for this plugin * Get tag for this plugin
@ -31,13 +30,14 @@ interface Plugin : Named, ContextAware, Provider, MetaRepr, Configurable {
*/ */
val tag: PluginTag val tag: PluginTag
val meta: Meta
/** /**
* The name of this plugin ignoring version and group * The name of this plugin ignoring version and group
* *
* @return * @return
*/ */
override val name: String override val name: String get() = tag.name
get() = tag.name
/** /**
* Plugin dependencies which are required to attach this plugin. Plugin * Plugin dependencies which are required to attach this plugin. Plugin
@ -67,7 +67,7 @@ interface Plugin : Named, ContextAware, Provider, MetaRepr, Configurable {
"context" to context.name "context" to context.name
"type" to this::class.simpleName "type" to this::class.simpleName
"tag" to tag "tag" to tag
"meta" to config "meta" to meta
} }
companion object { companion object {

View File

@ -116,7 +116,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
val loaded = get(tag, false) val loaded = get(tag, false)
return when { return when {
loaded == null -> load(PluginRepository.fetch(tag,meta)) loaded == null -> load(PluginRepository.fetch(tag,meta))
loaded.config == meta -> loaded // if meta is the same, return existing plugin loaded.meta == meta -> loaded // if meta is the same, return existing plugin
else -> throw RuntimeException("Can't load plugin with tag $tag. Plugin with this tag and different configuration already exists in context.") else -> throw RuntimeException("Can't load plugin with tag $tag. Plugin with this tag and different configuration already exists in context.")
} }
} }
@ -125,7 +125,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
val loaded = get(factory.tag, false) val loaded = get(factory.tag, false)
return when { return when {
loaded == null -> load(factory(meta)) loaded == null -> load(factory(meta))
loaded.config == meta -> loaded // if meta is the same, return existing plugin loaded.meta == meta -> loaded // if meta is the same, return existing plugin
else -> throw RuntimeException("Can't load plugin with tag ${factory.tag}. Plugin with this tag and different configuration already exists in context.") else -> throw RuntimeException("Can't load plugin with tag ${factory.tag}. Plugin with this tag and different configuration already exists in context.")
} }
} }
@ -146,7 +146,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
error("Corrupt type information in plugin repository") error("Corrupt type information in plugin repository")
} }
} }
loaded.config == meta -> loaded // if meta is the same, return existing plugin loaded.meta == meta -> loaded // if meta is the same, return existing plugin
else -> throw RuntimeException("Can't load plugin with type $type. Plugin with this type and different configuration already exists in context.") else -> throw RuntimeException("Can't load plugin with type $type. Plugin with this type and different configuration already exists in context.")
} }
} }

View File

@ -2,18 +2,14 @@ package hep.dataforge.context
import hep.dataforge.meta.EmptyMeta import hep.dataforge.meta.EmptyMeta
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.meta.configure
import kotlin.reflect.KClass import kotlin.reflect.KClass
interface PluginFactory<T : Plugin> { interface PluginFactory<T : Plugin> {
val tag: PluginTag val tag: PluginTag
val type: KClass<out T> val type: KClass<out T>
operator fun invoke(): T operator fun invoke(meta: Meta = EmptyMeta): T
} }
operator fun PluginFactory<*>.invoke(meta: Meta) = invoke().configure(meta)
expect object PluginRepository { expect object PluginRepository {
fun register(factory: PluginFactory<*>) fun register(factory: PluginFactory<*>)
@ -29,26 +25,25 @@ expect object PluginRepository {
* Fetch specific plugin and instantiate it with given meta * Fetch specific plugin and instantiate it with given meta
*/ */
fun PluginRepository.fetch(tag: PluginTag, meta: Meta = EmptyMeta): Plugin = fun PluginRepository.fetch(tag: PluginTag, meta: Meta = EmptyMeta): Plugin =
PluginRepository.list().find { it.tag.matches(tag) }?.invoke(meta) list().find { it.tag.matches(tag) }?.invoke(meta) ?: error("Plugin with tag $tag not found in the repository")
?: error("Plugin with tag $tag not found in the repository")
fun <T : Plugin> PluginRepository.register( fun <T : Plugin> PluginRepository.register(
tag: PluginTag, tag: PluginTag,
type: KClass<out T>, type: KClass<out T>,
constructor: () -> T constructor: (Meta) -> T
): PluginFactory<T> { ): PluginFactory<T> {
val factory = object : PluginFactory<T> { val factory = object : PluginFactory<T> {
override val tag: PluginTag = tag override val tag: PluginTag = tag
override val type: KClass<out T> = type override val type: KClass<out T> = type
override fun invoke(): T = constructor() override fun invoke(meta: Meta): T = constructor(meta)
} }
PluginRepository.register(factory) register(factory)
return factory return factory
} }
inline fun <reified T : Plugin> PluginRepository.register(tag: PluginTag, noinline constructor: () -> T) = inline fun <reified T : Plugin> PluginRepository.register(tag: PluginTag, noinline constructor: (Meta) -> T) =
register(tag, T::class, constructor) register(tag, T::class, constructor)
fun PluginRepository.register(plugin: Plugin) = register(plugin.tag, plugin::class) { plugin } fun PluginRepository.register(plugin: Plugin) = register(plugin.tag, plugin::class) { plugin }

View File

@ -61,6 +61,7 @@ data class NameToken(val body: String, val index: String = "") {
} }
fun String.toName(): Name { fun String.toName(): Name {
if (isBlank()) return EmptyName
val tokens = sequence { val tokens = sequence {
var bodyBuilder = StringBuilder() var bodyBuilder = StringBuilder()
var queryBuilder = StringBuilder() var queryBuilder = StringBuilder()

View File

@ -67,7 +67,7 @@ class ConsoleOutputManager : AbstractPlugin(), OutputManager {
override val type = ConsoleOutputManager::class override val type = ConsoleOutputManager::class
override fun invoke() = ConsoleOutputManager() override fun invoke(meta:Meta) = ConsoleOutputManager()
} }
} }