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

import com.google.inject.Binder
import com.google.inject.Module
import com.google.inject.TypeLiteral
import com.google.inject.binder.AnnotatedBindingBuilder
import com.google.inject.name.Names
import org.jetbrains.dokka.*
import org.jetbrains.dokka.Formats.FormatDescriptor
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import java.io.File
import kotlin.reflect.KClass

const val impliedPlatformsName = "impliedPlatforms"

class DokkaRunModule(val configuration: DokkaConfiguration) : Module {
    override fun configure(binder: Binder) {
        binder.bind<DokkaConfiguration>().toInstance(configuration)
        binder.bind(StringListType).annotatedWith(Names.named(impliedPlatformsName)).toInstance(configuration.impliedPlatforms)

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

}

class DokkaAnalysisModule(val environment: AnalysisEnvironment,
                          val configuration: DokkaConfiguration,
                          val defaultPlatformsProvider: DefaultPlatformsProvider,
                          val passConfiguration: DokkaConfiguration.PassConfiguration,
                          val logger: DokkaLogger) : Module {
    override fun configure(binder: Binder) {
        binder.bind<DokkaLogger>().toInstance(logger)

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

        val (dokkaResolutionFacade, libraryResolutionFacade) = environment.createResolutionFacade(coreEnvironment)
        binder.bind<DokkaResolutionFacade>().toInstance(dokkaResolutionFacade)
        binder.bind<DokkaResolutionFacade>().annotatedWith(Names.named("libraryResolutionFacade")).toInstance(libraryResolutionFacade)

        binder.bind<DokkaConfiguration.PassConfiguration>().toInstance(passConfiguration)

        binder.bind<DefaultPlatformsProvider>().toInstance(defaultPlatformsProvider)

        val descriptor = ServiceLocator.lookup<FormatDescriptor>("format", configuration.format)
        descriptor.configureAnalysis(binder)
    }
}

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

class DokkaOutputModule(val configuration: DokkaConfiguration,
                        val logger: DokkaLogger) : Module {
    override fun configure(binder: Binder) {
        binder.bind<DokkaLogger>().toInstance(logger)

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

        descriptor.configureOutput(binder)
    }
}

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(): AnnotatedBindingBuilder<T> = bind(T::class.java)

inline fun <reified T: Any> Binder.lazyBind(): Lazy<AnnotatedBindingBuilder<T>> = lazy { bind(T::class.java) }

inline infix fun <reified T: Any, TKClass: KClass<out T>> Lazy<AnnotatedBindingBuilder<T>>.toOptional(kClass: TKClass?) =
        kClass?.let { value toType it }

inline infix fun <reified T: Any, TKClass: KClass<out T>> AnnotatedBindingBuilder<T>.toType(kClass: TKClass) = to(kClass.java)