aboutsummaryrefslogtreecommitdiff
path: root/plugins/templating/src/main/kotlin/templates/SourcesetDependencyProcessingStrategy.kt
blob: ddebe160acc75133fc3d749c766bf29699ef80e3 (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
package templates

import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.base.templating.AddToSourcesetDependencies
import org.jetbrains.dokka.base.templating.parseJson
import org.jetbrains.dokka.base.templating.toJsonString
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.templates.TemplateProcessingStrategy
import java.io.File
import java.util.concurrent.ConcurrentHashMap

private typealias Entry = Map<String, List<String>>

class SourcesetDependencyProcessingStrategy(val context: DokkaContext) : TemplateProcessingStrategy {
    private val fileName = "sourceset_dependencies.js"
    private val fragments = ConcurrentHashMap<String, Entry>()

    override fun finish(output: File) {
        if (fragments.isNotEmpty()) {
            val content = fragments.values.fold(emptyMap<String, List<String>>()) { acc, e -> acc + e }
                .let { "sourceset_dependencies = '${toJsonString(it)}'" }
            output.resolve("scripts").mkdirs()
            output.resolve("scripts/$fileName").writeText(content)
        }
    }

    override fun process(input: File, output: File, moduleContext: DokkaConfiguration.DokkaModuleDescription?): Boolean =
        input.takeIf { it.name == fileName }
            ?.runCatching { parseJson<AddToSourcesetDependencies>(input.readText()) }
            ?.getOrNull()
            ?.also { (moduleName, content) ->
                fragments += (moduleName to content)
            } != null
}