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 org.jetbrains.dokka.Samples.SampleProcessingService import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import java.io.File class DokkaModule(val environment: AnalysisEnvironment, val options: DocumentationOptions, val logger: DokkaLogger) : Module { override fun configure(binder: Binder) { binder.bind(File::class.java).annotatedWith(Names.named("outputDir")).toInstance(File(options.outputDir)) binder.bindNameAnnotated("singleFolder") binder.bindNameAnnotated("singleFolder") binder.bindNameAnnotated("folders") binder.bindNameAnnotated("folders") // defaults binder.bind(LocationService::class.java).to(FoldersLocationService::class.java) binder.bind(FileLocationService::class.java).to(FoldersLocationService::class.java) binder.bind(LanguageService::class.java).to(KotlinLanguageService::class.java) binder.bind(HtmlTemplateService::class.java).toProvider(object : Provider { override fun get(): HtmlTemplateService = HtmlTemplateService.default("style.css") }) binder.registerCategory("language") binder.registerCategory("outline") binder.registerCategory("format") binder.registerCategory("generator") val descriptor = ServiceLocator.lookup("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().to(descriptor.packageDocumentationBuilderClass.java) binder.bind().to(descriptor.javaDocumentationBuilderClass.java) binder.bind().to(descriptor.sampleProcessingService.java) binder.bind().to(descriptor.generatorServiceClass.java) val coreEnvironment = environment.createCoreEnvironment() binder.bind().toInstance(coreEnvironment) val dokkaResolutionFacade = environment.createResolutionFacade(coreEnvironment) binder.bind().toInstance(dokkaResolutionFacade) binder.bind().toInstance(options) binder.bind().toInstance(logger) } } private inline fun 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) } } private inline fun Binder.bindNameAnnotated(name: String) { bind(Base::class.java).annotatedWith(Names.named(name)).to(T::class.java) } inline fun Binder.bind() = bind(T::class.java)