aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/renderers/FileWriter.kt
blob: 5439db17afd5241d24a314cbf2274a119579633e (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 org.jetbrains.dokka.renderers

import java.io.File
import java.io.IOException
import java.nio.file.Paths

class FileWriter(val root: String, override val extension: String): OutputWriter {
    private val createdFiles: MutableSet<String> = mutableSetOf()

    override fun write(path: String, text: String, ext: String) {
        if (createdFiles.contains(path)) {
            println("ERROR. An attempt to write $root/$path several times!")
            return
        }
        createdFiles.add(path)

        try {
//            println("Writing $root/$path$ext")
            val dir = Paths.get(root, path.dropLastWhile { it != '/' }).toFile()
            dir.mkdirsOrFail()
            Paths.get(root, "$path$ext").toFile().writeText(text)
        } catch (e : Throwable) {
            println("Failed to write $this. ${e.message}")
            e.printStackTrace()
        }
    }

    private fun File.mkdirsOrFail() {
        if (!mkdirs() && !exists()) {
            throw IOException("Failed to create directory $this")
        }
    }
}