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

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): Boolean =
        input.takeIf { it.name == fileName }
            ?.runCatching { parseJson<AddToSourcesetDependencies>(input.readText()) }
            ?.getOrNull()
            ?.also { (moduleName, content) ->
                fragments += (moduleName to content)
            } != null
}