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()).toInstance(config) binder.bind(javaClass()).annotatedWith(Names.named("outputDir")).toInstance(File(config.outputDir)) binder.bindNameAnnotated("singleFolder") binder.bindNameAnnotated("singleFolder") binder.bindNameAnnotated("folders") binder.bindNameAnnotated("folders") // defaults binder.bind(javaClass()).to(javaClass()) binder.bind(javaClass()).to(javaClass()) binder.bind(javaClass()).to(javaClass()) binder.bind(javaClass()).toProvider(object : Provider { override fun get(): HtmlTemplateService = HtmlTemplateService.default("/dokka/styles/style.css") }) binder.registerCategory("language") binder.registerCategory("outline") binder.registerCategory("format") binder.registerCategory("generator") val descriptor = ServiceLocator.lookup("format", config.outputFormat, config) descriptor.outlineServiceClass?.let { clazz -> binder.bind(javaClass()).to(clazz) } descriptor.formatServiceClass?.let { clazz -> binder.bind(javaClass()).to(clazz) } binder.bind(javaClass()).to(descriptor.generatorServiceClass) } } private inline fun Binder.registerCategory(category: String) { ServiceLocator.allServices(category).forEach { @Suppress("UNCHECKED_CAST") bind(javaClass()).annotatedWith(Names.named(it.name)).to(javaClass().classLoader.loadClass(it.className) as Class) } } private inline fun Binder.bindNameAnnotated(name: String) { bind(javaClass()).annotatedWith(Names.named(name)).to(javaClass()) }