aboutsummaryrefslogtreecommitdiff
path: root/src/Utilities/GuiceModule.kt
blob: 4ce4863d0d2e15b91a3bef96eaa25111fa81495a (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
package org.jetbrains.dokka.Utilities

import com.google.inject.Binder
import com.google.inject.Module
import com.google.inject.Provider
import com.google.inject.name.Names
import org.jetbrains.dokka.*
import org.jetbrains.dokka.Formats.FormatDescriptor
import java.io.File

class GuiceModule(val config: DokkaGenerator) : Module {
    override fun configure(binder: Binder) {
        binder.bind(javaClass<DokkaGenerator>()).toInstance(config)
        binder.bind(javaClass<File>()).annotatedWith(Names.named("outputDir")).toInstance(File(config.outputDir))

        binder.bindNameAnnotated<LocationService, SingleFolderLocationService>("singleFolder")
        binder.bindNameAnnotated<FileLocationService, SingleFolderLocationService>("singleFolder")
        binder.bindNameAnnotated<LocationService, FoldersLocationService>("folders")
        binder.bindNameAnnotated<FileLocationService, FoldersLocationService>("folders")

        // defaults
        binder.bind(javaClass<LocationService>()).to(javaClass<FoldersLocationService>())
        binder.bind(javaClass<FileLocationService>()).to(javaClass<FoldersLocationService>())
        binder.bind(javaClass<LanguageService>()).to(javaClass<KotlinLanguageService>())

        binder.bind(javaClass<HtmlTemplateService>()).toProvider(object : Provider<HtmlTemplateService> {
            override fun get(): HtmlTemplateService = HtmlTemplateService.default("/dokka/styles/style.css")
        })

        binder.registerCategory<LanguageService>("language")
        binder.registerCategory<OutlineFormatService>("outline")
        binder.registerCategory<FormatService>("format")
        binder.registerCategory<Generator>("generator")

        val descriptor = ServiceLocator.lookup<FormatDescriptor>("format", config.outputFormat, config)

        descriptor.outlineServiceClass?.let { clazz ->
            binder.bind(javaClass<OutlineFormatService>()).to(clazz)
        }
        descriptor.formatServiceClass?.let { clazz ->
            binder.bind(javaClass<FormatService>()).to(clazz)
        }
        binder.bind(javaClass<Generator>()).to(descriptor.generatorServiceClass)
    }

}

private inline fun <reified T> Binder.registerCategory(category: String) {
    ServiceLocator.allServices(category).forEach {
        @suppress("UNCHECKED_CAST")
        bind(javaClass<T>()).annotatedWith(Names.named(it.name)).to(javaClass<T>().classLoader.loadClass(it.className) as Class<T>)
    }
}

private inline fun <reified Base, reified T : Base> Binder.bindNameAnnotated(name: String) {
    bind(javaClass<Base>()).annotatedWith(Names.named(name)).to(javaClass<T>())
}