org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.dsl
import groovy.lang.Closure
import org.gradle.api.InvalidUserCodeException
import org.gradle.api.NamedDomainObjectCollection
import org.gradle.util.ConfigureUtil
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.utils.isGradleVersionAtLeast
open class KotlinMultiplatformExtension :
KotlinProjectExtension(),
KotlinTargetContainerWithPresetFunctions,
KotlinTargetContainerWithNativeShortcuts
{
override lateinit var presets: NamedDomainObjectCollection>
internal set
override lateinit var targets: NamedDomainObjectCollection
internal set
@Suppress("unused") // DSL
val testableTargets: NamedDomainObjectCollection>
get() = targets.withType(KotlinTargetWithTests::class.java)
internal var isGradleMetadataAvailable: Boolean = false
internal var isGradleMetadataExperimental: Boolean = false
fun metadata(configure: KotlinOnlyTarget.() -> Unit = { }): KotlinOnlyTarget =
@Suppress("UNCHECKED_CAST")
(targets.getByName(KotlinMultiplatformPlugin.METADATA_TARGET_NAME) as KotlinOnlyTarget).also(configure)
fun metadata(configure: Closure<*>) = metadata { ConfigureUtil.configure(configure, this) }
fun targetFromPreset(
preset: KotlinTargetPreset,
name: String = preset.name,
configure: T.() -> Unit = { }
): T = configureOrCreate(name, preset, configure)
fun targetFromPreset(preset: KotlinTargetPreset<*>, name: String, configure: Closure<*>) =
targetFromPreset(preset, name) { ConfigureUtil.configure(configure, this) }
fun targetFromPreset(preset: KotlinTargetPreset<*>) = targetFromPreset(preset, preset.name) { }
fun targetFromPreset(preset: KotlinTargetPreset<*>, name: String) = targetFromPreset(preset, name) { }
fun targetFromPreset(preset: KotlinTargetPreset<*>, configure: Closure<*>) = targetFromPreset(preset, preset.name, configure)
internal val rootSoftwareComponent: KotlinSoftwareComponent by lazy {
KotlinSoftwareComponentWithCoordinatesAndPublication("kotlin", targets)
}
}
internal fun KotlinTarget.isProducedFromPreset(kotlinTargetPreset: KotlinTargetPreset<*>): Boolean =
preset == kotlinTargetPreset
internal fun KotlinTargetsContainerWithPresets.configureOrCreate(
targetName: String,
targetPreset: KotlinTargetPreset,
configure: T.() -> Unit
): T {
val existingTarget = targets.findByName(targetName)
when {
existingTarget?.isProducedFromPreset(targetPreset) ?: false -> {
@Suppress("UNCHECKED_CAST")
configure(existingTarget as T)
return existingTarget
}
existingTarget == null -> {
val newTarget = targetPreset.createTarget(targetName)
targets.add(newTarget)
configure(newTarget)
return newTarget
}
else -> {
throw InvalidUserCodeException(
"The target '$targetName' already exists, but it was not created with the '${targetPreset.name}' preset. " +
"To configure it, access it by name in `kotlin.targets`" +
" or use the preset function '${existingTarget.preset?.name}'."
.takeIf { existingTarget.preset != null } ?: "."
)
}
}
}