aboutsummaryrefslogtreecommitdiff
path: root/plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt
blob: eafc366967a52faffbe21773a8f368ed5a13e937 (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
package org.jetbrains.dokka.allModulesPage.templates

import kotlinx.coroutines.*
import org.jetbrains.dokka.base.templating.Command
import org.jetbrains.dokka.plugability.DokkaContext
import org.jsoup.nodes.Element
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import kotlin.coroutines.coroutineContext

interface TemplateProcessor {
    fun process()
}

interface TemplateProcessingStrategy {
    suspend fun process(input: File, output: File)
    suspend fun finish(output: File) {}
}

class DefaultTemplateProcessor(
    private val context: DokkaContext,
    private val strategy: TemplateProcessingStrategy
): TemplateProcessor {
    override fun process() = runBlocking(Dispatchers.Default) {
        coroutineScope {
            context.configuration.modules.forEach {
                launch {
                    it.sourceOutputDirectory.visit(context.configuration.outputDir.resolve(it.relativePathToOutputDirectory))
                }
            }
        }
        strategy.finish(context.configuration.outputDir)
    }

    private suspend fun File.visit(target: File): Unit = coroutineScope {
        val source = this@visit
        if (source.isDirectory) {
           target.mkdir()
           source.list()?.forEach {
               launch { source.resolve(it).visit(target.resolve(it)) }
           }
        } else {
            strategy.process(source, target)
        }
    }
}

data class TemplatingContext<out T: Command>(
    val input: File,
    val output: File,
    val element: Element,
    val command: T,
)