diff options
author | Sergey Mashkov <sergey.mashkov@jetbrains.com> | 2015-07-31 15:35:34 +0300 |
---|---|---|
committer | Sergey Mashkov <sergey.mashkov@jetbrains.com> | 2015-07-31 17:52:49 +0300 |
commit | e27fb69817b1417c1bc556a507b14f2700c7a736 (patch) | |
tree | b6d3b5622e65651ae6510f5109d86f834ff2c337 /src/Generation | |
parent | 12f91ee7d491b21359ff8e8822c594f35b904def (diff) | |
download | dokka-e27fb69817b1417c1bc556a507b14f2700c7a736.tar.gz dokka-e27fb69817b1417c1bc556a507b14f2700c7a736.tar.bz2 dokka-e27fb69817b1417c1bc556a507b14f2700c7a736.zip |
Use Guice injector and ServiceLocator to load implementations on the fly
Diffstat (limited to 'src/Generation')
-rw-r--r-- | src/Generation/FileGenerator.kt | 37 | ||||
-rw-r--r-- | src/Generation/Generator.kt | 15 |
2 files changed, 39 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 diff --git a/src/Generation/Generator.kt b/src/Generation/Generator.kt new file mode 100644 index 00000000..7dcabb0b --- /dev/null +++ b/src/Generation/Generator.kt @@ -0,0 +1,15 @@ +package org.jetbrains.dokka + +public interface Generator { + fun buildPages(nodes: Iterable<DocumentationNode>) + fun buildOutlines(nodes: Iterable<DocumentationNode>) + + final fun buildAll(nodes: Iterable<DocumentationNode>) { + buildPages(nodes) + buildOutlines(nodes) + } + + final fun buildPage(node: DocumentationNode): Unit = buildPages(listOf(node)) + final fun buildOutline(node: DocumentationNode): Unit = buildOutlines(listOf(node)) + final fun buildAll(node: DocumentationNode): Unit = buildAll(listOf(node)) +} |