diff options
Diffstat (limited to 'src/Generation/FileGenerator.kt')
-rw-r--r-- | src/Generation/FileGenerator.kt | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/Generation/FileGenerator.kt b/src/Generation/FileGenerator.kt index abe4257f..08a885ab 100644 --- a/src/Generation/FileGenerator.kt +++ b/src/Generation/FileGenerator.kt @@ -1,36 +1,41 @@ package org.jetbrains.dokka +import com.google.inject.Inject +import java.io.File import java.io.FileOutputStream +import java.io.IOException import java.io.OutputStreamWriter -public class FileGenerator(val signatureGenerator: LanguageService, - val locationService: FileLocationService, +public class FileGenerator @Inject constructor(val locationService: FileLocationService, val formatService: FormatService, - val outlineService: OutlineFormatService?) { + @Inject(optional = true) val outlineService: OutlineFormatService?) : Generator { - public fun buildPage(node: DocumentationNode): Unit = buildPages(listOf(node)) - public fun buildOutline(node: DocumentationNode): Unit = buildOutlines(listOf(node)) + override fun buildPages(nodes: Iterable<DocumentationNode>) { + val specificLocationService = locationService.withExtension(formatService.extension) - public fun buildPages(nodes: Iterable<DocumentationNode>) { - for ((location, items) in nodes.groupBy { locationService.location(it) }) { + for ((location, items) in nodes.groupBy { specificLocationService.location(it) }) { val file = location.file - file.getParentFile()?.mkdirs() - FileOutputStream(file).use { - OutputStreamWriter(it, Charsets.UTF_8).use { - it.write(formatService.format(location, items)) + file.parentFile?.mkdirsOrFail() + try { + FileOutputStream(file).use { + OutputStreamWriter(it, Charsets.UTF_8).use { + it.write(formatService.format(location, items)) + } } + } catch (e: Throwable) { + println(e) } buildPages(items.flatMap { it.members }) } } - public fun buildOutlines(nodes: Iterable<DocumentationNode>) { + override fun buildOutlines(nodes: Iterable<DocumentationNode>) { if (outlineService == null) { return } for ((location, items) in nodes.groupBy { locationService.location(it) }) { val file = outlineService.getOutlineFileName(location) - file.getParentFile()?.mkdirs() + file.parentFile?.mkdirsOrFail() FileOutputStream(file).use { OutputStreamWriter(it, Charsets.UTF_8).use { it.write(outlineService.formatOutline(location, items)) @@ -38,4 +43,10 @@ public class FileGenerator(val signatureGenerator: LanguageService, } } } +} + +private fun File.mkdirsOrFail() { + if (!mkdirs() && !exists()) { + throw IOException("Failed to create directory $this") + } }
\ No newline at end of file |