aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/plugability/DokkaJavaPlugin.kt
blob: 1d8ea8aafef992ed1a57d09e5a6b99a06e80debd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package org.jetbrains.dokka.plugability

import org.jetbrains.dokka.DokkaConfiguration

class ExtensionBuilderStart internal constructor(){
    fun <T: Any>  extensionPoint(ext: ExtensionPoint<T>): ProvidedExtension<T> = ProvidedExtension(ext)
}

class ProvidedExtension<T: Any> internal constructor(val ext: ExtensionPoint<T>){
    fun fromInstance(inst: T): ExtensionBuilder<T> = createBuilder(
        LazyEvaluated.fromInstance(
            inst
        )
    )
    fun fromRecipe(recipe: (DokkaContext) -> T): ExtensionBuilder<T> = createBuilder(
        LazyEvaluated.fromRecipe(recipe)
    )

    private val defaultName = "${ext.pointName}/in/${javaClass.simpleName}"

    private fun createBuilder(action: LazyEvaluated<T>) =
        ExtensionBuilder(defaultName, ext, action,
            emptyList(), emptyList(),
            OverrideKind.None, emptyList())
}

data class ExtensionBuilder<T: Any> internal constructor(
    private val name: String,
    private val ext: ExtensionPoint<T>,
    private val action: LazyEvaluated<T>,
    private val before: List<out Extension<*, *, *>>,
    private val after: List<out Extension<*, *, *>>,
    private val override: OverrideKind = OverrideKind.None,
    private val conditions: List<(DokkaConfiguration) -> Boolean>
){
    fun build(): Extension<T, *, *>  = Extension(
        ext,
        javaClass.name,
        name,
        action,
        OrderingKind.ByDsl {
            before(*before.toTypedArray())
            after(*after.toTypedArray())
        },
        override,
        conditions
    )

    fun overrideExtension(extension: Extension<T, *, *>) = copy(override = OverrideKind.Present(listOf(extension)))

    fun newOrdering(before: Array<out Extension<*, *, *>>, after: Array<out Extension<*, *, *>>) =
        copy(before = this.before + before, after = this.after + after)

    fun before(vararg exts: Extension<*, *, *>) = copy(before = this.before + exts)

    fun after(vararg exts: Extension<*, *, *>) = copy(after = this.after + exts)

    fun addCondition(c: (DokkaConfiguration) -> Boolean) = copy(conditions = conditions + c)

    fun name(name: String) = copy(name = name)
}

abstract class DokkaJavaPlugin: DokkaPlugin() {

    fun <T: DokkaPlugin> plugin(clazz: Class<T>): T? =
        context?.plugin(clazz.kotlin) ?: throwIllegalQuery()


    fun <T: Any> extend(func: (ExtensionBuilderStart) -> ExtensionBuilder<T>): Lazy<Extension<T, *, *>> =
        lazy { func(ExtensionBuilderStart()).build() }.also { unsafeInstall(it) }

}