diff options
author | Kamil Doległo <kamilok1965@interia.pl> | 2019-09-23 10:25:45 +0200 |
---|---|---|
committer | Kamil Doległo <kamilok1965@interia.pl> | 2019-11-28 11:30:42 +0100 |
commit | e0be6a3d4536ad6aa2d764d96169cd0ca17613b8 (patch) | |
tree | d9d6d0233f2944193d6d1b177bf692d8a675d56d /runners/gradle-plugin/src/main/kotlin | |
parent | de2f32d91fb6f564826ddd7644940452356e2080 (diff) | |
download | dokka-e0be6a3d4536ad6aa2d764d96169cd0ca17613b8.tar.gz dokka-e0be6a3d4536ad6aa2d764d96169cd0ca17613b8.tar.bz2 dokka-e0be6a3d4536ad6aa2d764d96169cd0ca17613b8.zip |
Refactor configuration extraction
Diffstat (limited to 'runners/gradle-plugin/src/main/kotlin')
3 files changed, 68 insertions, 97 deletions
diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/ConfigurationExtractor.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/ConfigurationExtractor.kt index c66998d9..7ae9bca5 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/ConfigurationExtractor.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/ConfigurationExtractor.kt @@ -26,6 +26,12 @@ import java.io.Serializable class ConfigurationExtractor(private val project: Project) { + fun extractConfiguration(targetName: String, variantName: String?) = if (project.isMultiplatformProject()) { + extractFromMultiPlatform(targetName, variantName) + } else { + extractFromSinglePlatform(variantName) + } + fun extractFromSinglePlatform(variantName: String? = null): PlatformData? { val target: KotlinTarget try { @@ -45,28 +51,22 @@ class ConfigurationExtractor(private val project: Project) { } } - fun extractFromMultiPlatform(): List<PlatformData>? { - val targets: NamedDomainObjectCollection<KotlinTarget> + private fun extractFromMultiPlatform(targetName: String, variantName: String?): PlatformData? = try { - targets = project.extensions.getByType(KotlinMultiplatformExtension::class.java).targets + project.extensions.getByType(KotlinMultiplatformExtension::class.java).targets } catch (e: Throwable) { when (e){ is UnknownDomainObjectException, is NoClassDefFoundError, is ClassNotFoundException -> - return null + null else -> throw e } + }?.let { + val fixedName = if(targetName.toLowerCase() == "common") "metadata" else targetName.toLowerCase() + it.find { target -> target.name.toLowerCase() == fixedName }?.let { target -> + PlatformData(fixedName, getClasspath(target, variantName), getSourceSet(target, variantName), target.platformType.toString()) + } } - val commonTargetPlatformData = targets.find { it.platformType == KotlinPlatformType.common }?.let { - PlatformData("common", getClasspath(it), getSourceSet(it), "common") - } - val config = targets.filter { it.platformType != KotlinPlatformType.common }.map { - PlatformData(it.name, getClasspath(it), getSourceSet(it), it.platformType.toString()) - } - - return (config + commonTargetPlatformData).filterNotNull() - } - fun extractFromJavaPlugin(): PlatformData? = project.convention.findPlugin(JavaPluginConvention::class.java) ?.run { sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME)?.allSource?.srcDirs } diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt index 5153ae1c..35ef118a 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt @@ -15,6 +15,7 @@ import org.jetbrains.dokka.Platform import org.jetbrains.dokka.ReflectDsl import org.jetbrains.dokka.ReflectDsl.isNotInstance import org.jetbrains.dokka.gradle.ConfigurationExtractor.PlatformData +import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType import java.io.File import java.net.URLClassLoader import java.util.concurrent.Callable @@ -53,6 +54,9 @@ open class DokkaTask : DefaultTask() { var dokkaRuntime: Configuration? = null @Input + var subProjects: List<String> = emptyList() + + @Input var impliedPlatforms: MutableList<String> = arrayListOf() @Optional @@ -72,16 +76,12 @@ open class DokkaTask : DefaultTask() { // Configure Dokka with closure in Gradle Kotlin DSL fun configuration(action: Action<in GradlePassConfigurationImpl>) = action.execute(configuration) - private var externalDocumentationLinks: MutableList<DokkaConfiguration.ExternalDocumentationLink> = mutableListOf() private val kotlinTasks: List<Task> by lazy { extractKotlinCompileTasks(configuration.collectKotlinTasks ?: { defaultKotlinTasks() }) } private val configExtractor = ConfigurationExtractor(project) @Input - var subProjects: List<String> = emptyList() - - @Input var disableAutoconfiguration: Boolean = false private var outputDiagnosticInfo: Boolean = false // Workaround for Gradle, which fires some methods (like collectConfigurations()) multiple times in its lifecycle @@ -153,7 +153,7 @@ open class DokkaTask : DefaultTask() { val globalConfig = multiplatform.toList().find { it.name.toLowerCase() == GLOBAL_PLATFORM_NAME } val passConfigurationList = collectConfigurations() - .map { defaultPassConfiguration(globalConfig, it) } + .map { defaultPassConfiguration(it, globalConfig) } val configuration = GradleDokkaConfigurationImpl() configuration.outputDir = outputDirectory @@ -181,38 +181,15 @@ open class DokkaTask : DefaultTask() { } } - private fun collectConfigurations(): List<GradlePassConfigurationImpl> = - if (this.isMultiplatformProject()) collectFromMultiPlatform() else collectFromSinglePlatform() + private fun collectConfigurations() = + if (this.isMultiplatformProject()) collectMultiplatform() else listOf(collectSinglePlatform(configuration)) - private fun collectFromMultiPlatform(): List<GradlePassConfigurationImpl> { - val userConfig = multiplatform - .filterNot { it.name.toLowerCase() == GLOBAL_PLATFORM_NAME } - .map { - if (it.collectKotlinTasks != null) { - configExtractor.extractFromKotlinTasks(extractKotlinCompileTasks(it.collectKotlinTasks!!)) - ?.let { platformData -> mergeUserConfigurationAndPlatformData(it, platformData) } ?: it - } else { - it - } - } - - if (disableAutoconfiguration) return userConfig - - val baseConfig = mergeUserAndAutoConfigurations( - userConfig, - configExtractor.extractFromMultiPlatform().orEmpty() - ) - - return if (subProjects.isNotEmpty()) - subProjects.toProjects().fold(baseConfig) { list, subProject -> - mergeUserAndAutoConfigurations(list, ConfigurationExtractor(subProject).extractFromMultiPlatform().orEmpty()) - } - else - baseConfig - } + private fun collectMultiplatform() = multiplatform + .filterNot { it.name.toLowerCase() == GLOBAL_PLATFORM_NAME } + .map { collectSinglePlatform(it) } - private fun collectFromSinglePlatform(): List<GradlePassConfigurationImpl> { - val userConfig = configuration.let { + private fun collectSinglePlatform(config: GradlePassConfigurationImpl): GradlePassConfigurationImpl { + val userConfig = config.let { if (it.collectKotlinTasks != null) { configExtractor.extractFromKotlinTasks(extractKotlinCompileTasks(it.collectKotlinTasks!!)) ?.let { platformData -> mergeUserConfigurationAndPlatformData(it, platformData) } ?: it @@ -221,21 +198,27 @@ open class DokkaTask : DefaultTask() { } } - if (disableAutoconfiguration) return listOf(userConfig) + if (disableAutoconfiguration) return userConfig - val extractedConfig = configExtractor.extractFromSinglePlatform(userConfig.androidVariant) - val baseConfig = if (extractedConfig != null) - listOf(mergeUserConfigurationAndPlatformData(userConfig, extractedConfig)) - else - collectFromSinglePlatformOldPlugin() + val baseConfig = configExtractor.extractConfiguration(userConfig.name, userConfig.androidVariant) + ?.let { mergeUserConfigurationAndPlatformData(userConfig, it) } + ?: if (this.isMultiplatformProject()) { + if (outputDiagnosticInfo) + logger.warn("Could not find target with name: ${userConfig.name} in Kotlin Gradle Plugin, " + + "using only user provided configuration for this target") + userConfig + } else { + logger.warn("Could not find target with name: ${userConfig.name} in Kotlin Gradle Plugin") + collectFromSinglePlatformOldPlugin() + } return if (subProjects.isNotEmpty()) { try { - subProjects.toProjects().fold(baseConfig) { list, subProject -> - listOf(mergeUserConfigurationAndPlatformData( - list.first(), - ConfigurationExtractor(subProject).extractFromSinglePlatform()!! - )) + subProjects.toProjects().fold(baseConfig) { config, subProject -> + mergeUserConfigurationAndPlatformData( + config, + ConfigurationExtractor(subProject).extractConfiguration(config.name, config.androidVariant)!! + ) } } catch(e: NullPointerException) { logger.warn("Cannot extract sources from subProjects. Do you have the Kotlin plugin in version 1.3.30+ " + @@ -247,41 +230,14 @@ open class DokkaTask : DefaultTask() { } } - private fun collectFromSinglePlatformOldPlugin(): List<GradlePassConfigurationImpl> { - val kotlinTasks = configExtractor.extractFromKotlinTasks(kotlinTasks) - return if (kotlinTasks != null) { - listOf(mergeUserConfigurationAndPlatformData(configuration, kotlinTasks)) - } else { - val javaPlugin = configExtractor.extractFromJavaPlugin() - if (javaPlugin != null) - listOf(mergeUserConfigurationAndPlatformData(configuration, javaPlugin)) else listOf(configuration) - } - } - - private fun mergeUserAndAutoConfigurations(userConfigurations: List<GradlePassConfigurationImpl>, - autoConfigurations: List<PlatformData>): List<GradlePassConfigurationImpl> { - val merged: MutableList<GradlePassConfigurationImpl> = mutableListOf() - merged.addAll( - userConfigurations.map { userConfig -> - val autoConfig = autoConfigurations.find { autoConfig -> autoConfig.name == userConfig.name } - if (autoConfig != null) { - mergeUserConfigurationAndPlatformData(userConfig, autoConfig) - } else { - if(outputDiagnosticInfo) { - logger.warn( - "Could not find platform with name: ${userConfig.name} in Kotlin Gradle Plugin, " + - "using only user provided configuration for this platform" - ) - } - userConfig - } - } - ) - return merged.toList() - } + private fun collectFromSinglePlatformOldPlugin() = + configExtractor.extractFromKotlinTasks(kotlinTasks) + ?.let { mergeUserConfigurationAndPlatformData(configuration, it) } + ?: configExtractor.extractFromJavaPlugin() + ?.let { mergeUserConfigurationAndPlatformData(configuration, it) } + ?: configuration - private fun mergeUserConfigurationAndPlatformData(userConfig: GradlePassConfigurationImpl, - autoConfig: PlatformData): GradlePassConfigurationImpl = + private fun mergeUserConfigurationAndPlatformData(userConfig: GradlePassConfigurationImpl, autoConfig: PlatformData) = userConfig.copy().apply { sourceRoots.addAll(userConfig.sourceRoots.union(autoConfig.sourceRoots.toSourceRoots()).distinct()) classpath = userConfig.classpath.union(autoConfig.classpath.map { it.absolutePath }).distinct() @@ -289,7 +245,10 @@ open class DokkaTask : DefaultTask() { platform = autoConfig.platform } - private fun defaultPassConfiguration(globalConfig: GradlePassConfigurationImpl?, config: GradlePassConfigurationImpl): GradlePassConfigurationImpl { + private fun defaultPassConfiguration( + config: GradlePassConfigurationImpl, + globalConfig: GradlePassConfigurationImpl? + ): GradlePassConfigurationImpl { if (config.moduleName == "") { config.moduleName = project.name } @@ -304,7 +263,6 @@ open class DokkaTask : DefaultTask() { if (project.isAndroidProject() && !config.noAndroidSdkLink) { // TODO: introduce Android as a separate Dokka platform? config.externalDocumentationLinks.add(ANDROID_REFERENCE_URL) } - config.externalDocumentationLinks.addAll(externalDocumentationLinks) if (config.platform != null && config.platform.toString().isNotEmpty()) { config.analysisPlatform = dokkaPlatformFromString(config.platform.toString()) } @@ -319,7 +277,8 @@ open class DokkaTask : DefaultTask() { } private fun dokkaPlatformFromString(platform: String) = when (platform.toLowerCase()) { - "androidjvm", "android" -> Platform.jvm + KotlinPlatformType.androidJvm.toString().toLowerCase(), "androidjvm", "android" -> Platform.jvm + "metadata" -> Platform.common else -> Platform.fromString(platform) } diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/utils.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/utils.kt index d70b0499..31892e8e 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/utils.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/utils.kt @@ -2,6 +2,7 @@ package org.jetbrains.dokka.gradle import org.gradle.api.Project import org.gradle.api.UnknownDomainObjectException +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType import org.jetbrains.kotlin.gradle.plugin.KotlinTarget @@ -15,5 +16,16 @@ fun Project.isAndroidProject() = try { false } +fun Project.isMultiplatformProject() = try { + project.extensions.getByType(KotlinMultiplatformExtension::class.java) + true +} catch(e: UnknownDomainObjectException) { + false +} catch (e: NoClassDefFoundError){ + false +} catch(e: ClassNotFoundException) { + false +} + fun KotlinTarget.isAndroidTarget() = this.platformType == KotlinPlatformType.androidJvm fun DokkaTask.isMultiplatformProject() = this.multiplatform.isNotEmpty()
\ No newline at end of file |