From a945617725e8df084270aacd5af76da2b911111e Mon Sep 17 00:00:00 2001 From: Paweł Marks Date: Mon, 25 Nov 2019 15:34:47 +0100 Subject: Interface segregation in plugins api --- core/src/main/kotlin/DokkaGenerator.kt | 4 +- core/src/main/kotlin/plugability/DokkaContext.kt | 82 +++++++++++++++--------- core/src/main/kotlin/plugability/DokkaPlugin.kt | 14 +--- 3 files changed, 54 insertions(+), 46 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/DokkaGenerator.kt b/core/src/main/kotlin/DokkaGenerator.kt index b29a0da2..05eb3054 100644 --- a/core/src/main/kotlin/DokkaGenerator.kt +++ b/core/src/main/kotlin/DokkaGenerator.kt @@ -25,9 +25,7 @@ class DokkaGenerator( fun generate(): Unit { logger.debug("Initializing plugins") - val context = DokkaContext.from(configuration.pluginsClasspath) - logger.progress("Loaded plugins: ${context.pluginNames}") - logger.progress("Loaded: ${context.loadedListForDebug}") + val context = DokkaContext.create(configuration.pluginsClasspath, logger) configuration.passesConfigurations.map { pass -> AnalysisEnvironment(DokkaMessageCollector(logger), pass.analysisPlatform).run { diff --git a/core/src/main/kotlin/plugability/DokkaContext.kt b/core/src/main/kotlin/plugability/DokkaContext.kt index dcd3398e..7727329c 100644 --- a/core/src/main/kotlin/plugability/DokkaContext.kt +++ b/core/src/main/kotlin/plugability/DokkaContext.kt @@ -1,55 +1,75 @@ package org.jetbrains.dokka.plugability +import org.jetbrains.dokka.DokkaLogger import java.io.File import java.net.URLClassLoader import java.util.* import kotlin.reflect.KClass +interface DokkaContext { + operator fun > get(point: E): List> -class DokkaContext private constructor() { + fun plugin(kclass: KClass): T? + + val logger: DokkaLogger + + companion object { + fun create(pluginsClasspath: Iterable, logger: DokkaLogger): DokkaContext = + DokkaContextConfigurationImpl(logger).apply { + pluginsClasspath.map { it.relativeTo(File(".").absoluteFile).toURI().toURL() } + .toTypedArray() + .let { URLClassLoader(it, this.javaClass.classLoader) } + .also { checkClasspath(it) } + .let { ServiceLoader.load(DokkaPlugin::class.java, it) } + .forEach { install(it) } + }.also { it.logInitialisationInfo() } + } +} + +interface DokkaContextConfiguration { + fun addExtension(extension: Extension<*>) +} + +private class DokkaContextConfigurationImpl( + override val logger: DokkaLogger +) : DokkaContext, DokkaContextConfiguration { private val plugins = mutableMapOf, DokkaPlugin>() internal val extensions = mutableMapOf, MutableList>>() @Suppress("UNCHECKED_CAST") - operator fun > get(point: E) = extensions[point] as List> - - @PublishedApi - internal fun plugin(kclass: KClass<*>) = plugins[kclass] - - val pluginNames: List - get() = plugins.values.map { it::class.qualifiedName.toString() } + override operator fun > get(point: E) = extensions[point] as List> - val loadedListForDebug - get() = extensions.run { keys + values.flatten() }.toList() - .joinToString(prefix = "[\n", separator = ",\n", postfix = "\n]") { "\t$it" } + @Suppress("UNCHECKED_CAST") + override fun plugin(kclass: KClass) = plugins[kclass] as T - private fun install(plugin: DokkaPlugin) { + fun install(plugin: DokkaPlugin) { plugins[plugin::class] = plugin + plugin.context = this plugin.internalInstall(this) } - companion object { - fun from(pluginsClasspath: Iterable) = DokkaContext().apply { - pluginsClasspath.map { it.relativeTo(File(".").absoluteFile).toURI().toURL() } - .toTypedArray() - .let { URLClassLoader(it, this.javaClass.classLoader) } - .also { checkClasspath(it) } - .let { ServiceLoader.load(DokkaPlugin::class.java, it) } - .forEach { install(it) } - } + override fun addExtension(extension: Extension<*>) { + extensions.getOrPut(extension.extensionPoint, ::mutableListOf) += extension } - private fun checkClasspath(classLoader: URLClassLoader) { - classLoader.findResource(javaClass.name.replace('.', '/') + ".class")?.also { - throw AssertionError( - "Dokka API found on plugins classpath. This will lead to subtle bugs. " + - "Please fix your plugins dependencies or exclude dokka api artifact from plugin classpath" - ) - } - } + fun logInitialisationInfo() { + val pluginNames: List = plugins.values.map { it::class.qualifiedName.toString() } + + val loadedListForDebug = extensions.run { keys + values.flatten() }.toList() + .joinToString(prefix = "[\n", separator = ",\n", postfix = "\n]") { "\t$it" } + + logger.progress("Loaded plugins: ${pluginNames}") + logger.progress("Loaded: ${loadedListForDebug}") - internal fun addExtension(it: Extension<*>) { - extensions.getOrPut(it.extensionPoint, ::mutableListOf) += it } } + +private fun checkClasspath(classLoader: URLClassLoader) { + classLoader.findResource(DokkaContext::class.java.name.replace('.', '/') + ".class")?.also { + throw AssertionError( + "Dokka API found on plugins classpath. This will lead to subtle bugs. " + + "Please fix your plugins dependencies or exclude dokka api artifact from plugin classpath" + ) + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/plugability/DokkaPlugin.kt b/core/src/main/kotlin/plugability/DokkaPlugin.kt index 60ca245c..663cf88e 100644 --- a/core/src/main/kotlin/plugability/DokkaPlugin.kt +++ b/core/src/main/kotlin/plugability/DokkaPlugin.kt @@ -3,12 +3,7 @@ package org.jetbrains.dokka.plugability import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty import kotlin.reflect.KProperty1 -import kotlin.reflect.KTypeProjection import kotlin.reflect.full.createInstance -import kotlin.reflect.full.createType -import kotlin.reflect.full.declaredMemberProperties -import kotlin.reflect.jvm.isAccessible -import kotlin.reflect.jvm.jvmErasure private typealias ExtensionDelegate = ReadOnlyProperty> @@ -18,10 +13,8 @@ abstract class DokkaPlugin { @PublishedApi internal var context: DokkaContext? = null - protected open fun install(context: DokkaContext) {} - protected inline fun plugin(): T = - context?.plugin(T::class) as? T ?: T::class.createInstance().also { it.context = this.context } + context?.plugin(T::class) ?: T::class.createInstance().also { it.context = this.context } protected fun extensionPoint() = object : ReadOnlyProperty> { @@ -44,10 +37,7 @@ abstract class DokkaPlugin { }.also { thisRef.extensionDelegates += property } } - internal fun internalInstall(ctx: DokkaContext) { - context = ctx - install(ctx) - + internal fun internalInstall(ctx: DokkaContextConfiguration) { extensionDelegates.asSequence() .filterIsInstance>>() // always true .map { it.get(this) } -- cgit