From ec6ccf7228e7c0d9417e6f7cd5f508bec5285ca6 Mon Sep 17 00:00:00 2001 From: Krzysztof Romanwoski Date: Fri, 11 Sep 2020 16:29:55 +0200 Subject: [PATCH] Create java-like api for plugins --- build.gradle.kts | 1 + .../kotlin/plugability/DokkaJavaPlugin.kt | 68 +++++++++++++++++++ .../main/kotlin/plugability/DokkaPlugin.kt | 11 ++- 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 core/src/main/kotlin/plugability/DokkaJavaPlugin.kt diff --git a/build.gradle.kts b/build.gradle.kts index f1f66a9c1f..b8879a3153 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,6 +15,7 @@ allprojects { group = "org.jetbrains.dokka" version = dokka_version + val language_version: String by project tasks.withType(KotlinCompile::class).all { kotlinOptions { diff --git a/core/src/main/kotlin/plugability/DokkaJavaPlugin.kt b/core/src/main/kotlin/plugability/DokkaJavaPlugin.kt new file mode 100644 index 0000000000..7c527abcd4 --- /dev/null +++ b/core/src/main/kotlin/plugability/DokkaJavaPlugin.kt @@ -0,0 +1,68 @@ +package org.jetbrains.dokka.plugability + +import org.jetbrains.dokka.DokkaConfiguration + +class ExtensionBuilderStart internal constructor(){ + fun extensionPoint(ext: ExtensionPoint): ProvidedExtension = ProvidedExtension(ext) +} + +class ProvidedExtension internal constructor(val ext: ExtensionPoint){ + fun fromInstance(inst: T): ExtensionBuilder = createBuilder( + LazyEvaluated.fromInstance( + inst + ) + ) + fun fromRecipe(recipe: (DokkaContext) -> T): ExtensionBuilder = createBuilder( + LazyEvaluated.fromRecipe(recipe) + ) + + private val defaultName = "${ext.pointName}/in/${javaClass.simpleName}" + + private fun createBuilder(action: LazyEvaluated) = + ExtensionBuilder(defaultName, ext, action, + OrderingKind.None, + OverrideKind.None, emptyList()) +} + +data class ExtensionBuilder internal constructor( + private val name: String, + private val ext: ExtensionPoint, + private val action: LazyEvaluated, + private val ordering: OrderingKind = OrderingKind.None, + private val override: OverrideKind = OverrideKind.None, + private val conditions: List<(DokkaConfiguration) -> Boolean> +){ + fun build(): Extension = Extension( + ext, + javaClass.name, + name, + action, + ordering, + override, + conditions + ) + + fun overrideExtension(extension: Extension) = copy(override = OverrideKind.Present(extension)) + + fun newOrdering(before: Array>, after: Array>) { + copy(ordering = OrderingKind.ByDsl { + before(*before) + after(*after) + }) + } + + fun addCondition(c: (DokkaConfiguration) -> Boolean) = copy(conditions = conditions + c) + + fun name(name: String) = copy(name = name) +} + +abstract class DokkaJavaPlugin: DokkaPlugin() { + + fun plugin(clazz: Class): T? = + context?.plugin(clazz.kotlin) ?: throwIllegalQuery() + + + fun extend(func: (ExtensionBuilderStart) -> ExtensionBuilder): Lazy> = + lazy { func(ExtensionBuilderStart()).build() }.also { unsafeInstall(it) } + +} \ 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 a62327d2e7..13f59f6e1d 100644 --- a/core/src/main/kotlin/plugability/DokkaPlugin.kt +++ b/core/src/main/kotlin/plugability/DokkaPlugin.kt @@ -10,6 +10,7 @@ import kotlin.reflect.full.createInstance abstract class DokkaPlugin { private val extensionDelegates = mutableListOf>() + private val unsafePlugins = mutableListOf>>() @PublishedApi internal var context: DokkaContext? = null @@ -37,10 +38,14 @@ abstract class DokkaPlugin { } internal fun internalInstall(ctx: DokkaContextConfiguration, configuration: DokkaConfiguration) { - extensionDelegates.asSequence() + val extensionsToInstall = extensionDelegates.asSequence() .filterIsInstance>>() // should be always true - .map { it.get(this) } - .forEach { if (configuration.(it.condition)()) ctx.installExtension(it) } + .map { it.get(this) } + unsafePlugins.map{ it.value } + extensionsToInstall.forEach { if (configuration.(it.condition)()) ctx.installExtension(it) } + } + + protected fun unsafeInstall(ext: Lazy>){ + unsafePlugins.add(ext) } }