From 202845bf4c9e72cfcda578a548f32bc7cc04f64d Mon Sep 17 00:00:00 2001 From: Paweł Marks Date: Mon, 25 Nov 2019 17:45:23 +0100 Subject: Default context for dokka --- core/src/main/kotlin/plugability/DokkaContext.kt | 33 ++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/plugability/DokkaContext.kt b/core/src/main/kotlin/plugability/DokkaContext.kt index a5bcd695..b917ff99 100644 --- a/core/src/main/kotlin/plugability/DokkaContext.kt +++ b/core/src/main/kotlin/plugability/DokkaContext.kt @@ -8,7 +8,8 @@ import kotlin.reflect.KClass import kotlin.reflect.full.createInstance interface DokkaContext { - operator fun > get(point: E): List> + operator fun get(point: E, askDefault: AskDefault = AskDefault.WhenEmpty): List> + where T : Any, E : ExtensionPoint fun plugin(kclass: KClass): T? @@ -16,7 +17,7 @@ interface DokkaContext { companion object { fun create(pluginsClasspath: Iterable, logger: DokkaLogger): DokkaContext = - DokkaContextConfigurationImpl(logger).apply { + DokkaContextConfigurationImpl(logger, DefaultContext(logger)).apply { pluginsClasspath.map { it.relativeTo(File(".").absoluteFile).toURI().toURL() } .toTypedArray() .let { URLClassLoader(it, this.javaClass.classLoader) } @@ -32,7 +33,8 @@ interface DokkaContextConfiguration { } private class DokkaContextConfigurationImpl( - override val logger: DokkaLogger + override val logger: DokkaLogger, + private val defaultContext: DokkaContext? ) : DokkaContext, DokkaContextConfiguration { private val plugins = mutableMapOf, DokkaPlugin>() @@ -41,10 +43,17 @@ private class DokkaContextConfigurationImpl( internal val extensions = mutableMapOf, MutableList>>() @Suppress("UNCHECKED_CAST") - override operator fun > get(point: E) = extensions[point] as List> + override operator fun get(point: E, askDefault: AskDefault) where T : Any, E : ExtensionPoint = + when (askDefault) { + AskDefault.Never -> extensions[point].orEmpty() + AskDefault.Always -> extensions[point].orEmpty() + defaultContext?.get(point, askDefault).orEmpty() + AskDefault.WhenEmpty -> + extensions[point]?.takeIf { it.isNotEmpty() } ?: defaultContext?.get(point, askDefault).orEmpty() + } as List> @Suppress("UNCHECKED_CAST") - override fun plugin(kclass: KClass) = (plugins[kclass] ?: pluginStubFor(kclass)) as T + override fun plugin(kclass: KClass) = + (plugins[kclass] ?: defaultContext?.plugin(kclass) ?: pluginStubFor(kclass)) as T private fun pluginStubFor(kclass: KClass): DokkaPlugin = pluginStubs.getOrPut(kclass) { kclass.createInstance().also { it.context = this } } @@ -78,4 +87,18 @@ private fun checkClasspath(classLoader: URLClassLoader) { "Please fix your plugins dependencies or exclude dokka api artifact from plugin classpath" ) } +} + +class DefaultContext(override val logger: DokkaLogger) : DokkaContext { + override fun > get(point: E, askDefault: AskDefault): List> = + when (point) { + + else -> emptyList() + } + + override fun plugin(kclass: KClass): Nothing? = null +} + +enum class AskDefault { + Always, Never, WhenEmpty } \ No newline at end of file -- cgit