aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Utilities/DokkaModules.kt
blob: 3352a846de516a6745dcfd5fc0b076e48f045ba5 (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
package org.jetbrains.dokka.Utilities

import com.google.inject.Binder
import com.google.inject.Module
import com.google.inject.Provider
import com.google.inject.TypeLiteral
import com.google.inject.name.Names
import org.jetbrains.dokka.*
import org.jetbrains.dokka.Formats.FormatDescriptor
import org.jetbrains.dokka.Samples.SampleProcessingService
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import java.io.File

class DokkaAnalysisModule(val environment: AnalysisEnvironment,
                          val options: DocumentationOptions,
                          val implicitPlatforms: List<String>,
                          val logger: DokkaLogger) : Module {
    override fun configure(binder: Binder) {
        val descriptor = ServiceLocator.lookup<FormatDescriptor>("format", options.outputFormat)

        binder.registerCategory<LanguageService>("language")
        binder.bind<PackageDocumentationBuilder>().to(descriptor.packageDocumentationBuilderClass.java)
        binder.bind<JavaDocumentationBuilder>().to(descriptor.javaDocumentationBuilderClass.java)
        binder.bind<SampleProcessingService>().to(descriptor.sampleProcessingService.java)

        val coreEnvironment = environment.createCoreEnvironment()
        binder.bind<KotlinCoreEnvironment>().toInstance(coreEnvironment)

        val dokkaResolutionFacade = environment.createResolutionFacade(coreEnvironment)
        binder.bind<DokkaResolutionFacade>().toInstance(dokkaResolutionFacade)

        binder.bind<DocumentationOptions>().toInstance(options)
        binder.bind<DokkaLogger>().toInstance(logger)

        binder.bind(StringListType).annotatedWith(Names.named(implicitPlatformName)).toInstance(implicitPlatforms)
    }
}

object StringListType : TypeLiteral<@JvmSuppressWildcards List<String>>()

class DokkaOutputModule(val options: DocumentationOptions,
                        val logger: DokkaLogger) : Module {
    override fun configure(binder: Binder) {
        binder.bind(LanguageService::class.java).to(KotlinLanguageService::class.java)

        binder.bind(HtmlTemplateService::class.java).toProvider(object : Provider<HtmlTemplateService> {
            override fun get(): HtmlTemplateService = HtmlTemplateService.default("style.css")
        })

        binder.bind(File::class.java).annotatedWith(Names.named("outputDir")).toInstance(File(options.outputDir))

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

        // defaults
        binder.bind(LocationService::class.java).to(FoldersLocationService::class.java)
        binder.bind(FileLocationService::class.java).to(FoldersLocationService::class.java)

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

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

        descriptor.outlineServiceClass?.let { clazz ->
            binder.bind(OutlineFormatService::class.java).to(clazz.java)
        }
        descriptor.formatServiceClass?.let { clazz ->
            binder.bind(FormatService::class.java).to(clazz.java)
        }

        binder.bind<Generator>().to(descriptor.generatorServiceClass.java)

        binder.bind<DocumentationOptions>().toInstance(options)
        binder.bind<DokkaLogger>().toInstance(logger)
    }
}

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

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


inline fun <reified T: Any> Binder.bind() = bind(T::class.java)