aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/DokkaBootstrapImpl.kt
blob: e0f014c82c096fbc07fb93be9a0d578b591009ba (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.jetbrains.dokka

import com.google.gson.Gson
import org.jetbrains.dokka.DokkaConfiguration.PackageOptions
import org.jetbrains.dokka.utilities.DokkaConsoleLogger
import org.jetbrains.dokka.utilities.DokkaLogger

import java.util.function.BiConsumer


fun parsePerPackageOptions(args: List<String>): List<PackageOptions> = args.map { it.split(",") }.map {
    val prefix = it.first()
    if (prefix == "")
        throw IllegalArgumentException("Please do not register packageOptions with all match pattern, use global settings instead")
    val args = it.subList(1, it.size)
    val deprecated = args.find { it.endsWith("deprecated") }?.startsWith("+") ?: true
    val reportUndocumented = args.find { it.endsWith("reportUndocumented") }?.startsWith("+") ?: true
    val privateApi = args.find { it.endsWith("privateApi") }?.startsWith("+") ?: false
    val suppress = args.find { it.endsWith("suppress") }?.startsWith("+") ?: false
    PackageOptionsImpl(
        prefix,
        includeNonPublic = privateApi,
        reportUndocumented = reportUndocumented,
        skipDeprecated = !deprecated,
        suppress = suppress
    )
}


class DokkaBootstrapImpl : DokkaBootstrap {

    class DokkaProxyLogger(val consumer: BiConsumer<String, String>) : DokkaLogger {
        override var warningsCount: Int = 0
        override var errorsCount: Int = 0

        override fun debug(message: String) {
            consumer.accept("debug", message)
        }

        override fun info(message: String) {
            consumer.accept("info", message)
        }

        override fun progress(message: String) {
            consumer.accept("progress", message)
        }

        override fun warn(message: String) {
            consumer.accept("warn", message).also { warningsCount++ }
        }

        override fun error(message: String) {
            consumer.accept("error", message).also { errorsCount++ }
        }

        override fun report() {
            if (warningsCount > 0 || errorsCount > 0) {
                println(
                    "Generation completed with $warningsCount warning" +
                            (if (warningsCount == 1) "" else "s") +
                            " and $errorsCount error" +
                            if (errorsCount == 1) "" else "s"
                )
            } else {
                println("generation completed successfully")
            }
        }
    }

    private lateinit var generator: DokkaGenerator
    val gson = Gson()

    fun configure(logger: DokkaLogger, configuration: DokkaConfigurationImpl) = with(configuration) {

        fun defaultLinks(config: PassConfigurationImpl): List<ExternalDocumentationLinkImpl> {
            val links = mutableListOf<ExternalDocumentationLinkImpl>()
            if (!config.noJdkLink)
                links += DokkaConfiguration.ExternalDocumentationLink
                    .Builder("https://docs.oracle.com/javase/${config.jdkVersion}/docs/api/")
                    .build() as ExternalDocumentationLinkImpl

            if (!config.noStdlibLink)
                links += DokkaConfiguration.ExternalDocumentationLink
                    .Builder("https://kotlinlang.org/api/latest/jvm/stdlib/")
                    .build() as ExternalDocumentationLinkImpl
            return links
        }

        val configurationWithLinks =
            configuration.copy(
                passesConfigurations =
                passesConfigurations.map {
                    val links: List<ExternalDocumentationLinkImpl> =
                        it.externalDocumentationLinks + defaultLinks(it)
                    it.copy(externalDocumentationLinks = links)
                }
            )

        generator = DokkaGenerator(configurationWithLinks, logger)
    }

    override fun configure(logger: BiConsumer<String, String>, serializedConfigurationJSON: String) = configure(
        DokkaProxyLogger(logger),
        gson.fromJson(serializedConfigurationJSON, DokkaConfigurationImpl::class.java)
    )

    override fun generate() = generator.generate()
}