aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/renderers/FileWriter.kt
blob: e195f23c724a008b2c5f92b5223ea501dfd4f421 (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
package org.jetbrains.dokka.base.renderers

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.jetbrains.dokka.plugability.DokkaContext
import java.io.File
import java.io.IOException
import java.net.URI
import java.nio.file.*

class FileWriter(val context: DokkaContext): OutputWriter {
    private val createdFiles: MutableSet<String> = mutableSetOf()
    private val createdFilesMutex = Mutex()
    private val jarUriPrefix = "jar:file:"
    private val root = context.configuration.outputDir

    override suspend fun write(path: String, text: String, ext: String) {
        if (checkFileCreated(path)) return

        try {
            val dir = Paths.get(root.absolutePath, path.dropLastWhile { it != '/' }).toFile()
            withContext(Dispatchers.IO) {
                dir.mkdirsOrFail()
                Files.write(Paths.get(root.absolutePath, "$path$ext"), text.lines())
            }
        } catch (e: Throwable) {
            context.logger.error("Failed to write $this. ${e.message}")
            e.printStackTrace()
        }
    }

    private suspend fun checkFileCreated(path: String): Boolean = createdFilesMutex.withLock {
        if (createdFiles.contains(path)) {
            context.logger.error("An attempt to write ${root}/$path several times!")
            return true
        }
        createdFiles.add(path)
        return false
    }

    override suspend fun writeResources(pathFrom: String, pathTo: String) =
        if (javaClass.getResource(pathFrom)?.toURI()?.toString()?.startsWith(jarUriPrefix) == true) {
            copyFromJar(pathFrom, pathTo)
        } else {
            copyFromDirectory(pathFrom, pathTo)
        }


    private suspend fun copyFromDirectory(pathFrom: String, pathTo: String) {
        val dest = Paths.get(root.path, pathTo).toFile()
        val uri = javaClass.getResource(pathFrom)?.toURI()
        val file = uri?.let { File(it) } ?: File(pathFrom)
        withContext(Dispatchers.IO) {
            file.copyRecursively(dest, true)
        }
    }

    private suspend fun copyFromJar(pathFrom: String, pathTo: String) {
        val rebase = fun(path: String) =
            "$pathTo/${path.removePrefix(pathFrom)}"
        val dest = Paths.get(root.path, pathTo).toFile()
        if(dest.isDirectory){
            dest.mkdirsOrFail()
        } else {
            dest.parentFile.mkdirsOrFail()
        }
        val uri = javaClass.getResource(pathFrom).toURI()
        val fs = getFileSystemForURI(uri)
        val path = fs.getPath(pathFrom)
        for (file in Files.walk(path).iterator()) {
            if (Files.isDirectory(file)) {
                val dirPath = file.toAbsolutePath().toString()
                withContext(Dispatchers.IO) {
                    Paths.get(root.path, rebase(dirPath)).toFile().mkdirsOrFail()
                }
            } else {
                val filePath = file.toAbsolutePath().toString()
                withContext(Dispatchers.IO) {
                    Paths.get(root.path, rebase(filePath)).toFile().writeBytes(
                        this@FileWriter.javaClass.getResourceAsStream(filePath).use { it?.readBytes() }
                            ?: throw IllegalStateException("Can not get a resource from $filePath")
                    )
                }
            }
        }
    }

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

    private fun getFileSystemForURI(uri: URI): FileSystem =
        try {
            FileSystems.newFileSystem(uri, emptyMap<String, Any>())
        } catch (e: FileSystemAlreadyExistsException) {
            FileSystems.getFileSystem(uri)
        }
}