org.jetbrains.kotlin.gradle.plugin.mpp.pm20.GradleKpmConfigurationAttributesSetup.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2022 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.plugin.mpp.pm20
import org.gradle.api.Named
import org.gradle.api.Project
import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.AttributeContainer
/* Internal abbreviation */
interface GradleKpmConfigurationAttributesSetup {
fun setupAttributes(attributes: AttributeContainer, fragment: T)
object None : GradleKpmConfigurationAttributesSetup {
override fun setupAttributes(attributes: AttributeContainer, fragment: GradleKpmFragment) = Unit
}
}
class GradleKpmConfigurationAttributesSetupContext internal constructor(
internal val attributes: AttributeContainer,
val fragment: T,
) : AttributeContainer by attributes {
val project: Project get() = fragment.project
inline fun named(name: String): T = project.objects.named(T::class.java, name)
inline fun namedAttribute(key: Attribute, name: String) = apply { attribute(key, named(name)) }
override fun attribute(key: Attribute, value: K): GradleKpmConfigurationAttributesSetupContext = apply {
attributes.attribute(key, value)
}
}
@Suppress("FunctionName")
fun GradleKpmConfigurationAttributesSetup(
setAttributes: GradleKpmConfigurationAttributesSetupContext.() -> Unit
): GradleKpmConfigurationAttributesSetup {
return object : GradleKpmConfigurationAttributesSetup {
override fun setupAttributes(attributes: AttributeContainer, fragment: T) {
val context = GradleKpmConfigurationAttributesSetupContext(attributes, fragment)
context.setAttributes()
}
}
}
fun AttributeContainer.apply(
attributes: GradleKpmConfigurationAttributesSetup, fragment: T
) {
attributes.setupAttributes(this, fragment)
}
operator fun GradleKpmConfigurationAttributesSetup.plus(other: GradleKpmConfigurationAttributesSetup): GradleKpmConfigurationAttributesSetup {
if (this === GradleKpmConfigurationAttributesSetup.None) return other
if (other === GradleKpmConfigurationAttributesSetup.None) return this
if (this is GradleKpmCompositeConfigurationAttributesSetup && other is GradleKpmCompositeConfigurationAttributesSetup) {
return GradleKpmCompositeConfigurationAttributesSetup(this.children + other.children)
}
if (this is GradleKpmCompositeConfigurationAttributesSetup) {
return GradleKpmCompositeConfigurationAttributesSetup(this.children + other)
}
if (other is GradleKpmCompositeConfigurationAttributesSetup) {
return GradleKpmCompositeConfigurationAttributesSetup(listOf(this) + other.children)
}
return GradleKpmCompositeConfigurationAttributesSetup(listOf(this, other))
}
internal class GradleKpmCompositeConfigurationAttributesSetup(val children: List>) :
GradleKpmConfigurationAttributesSetup {
override fun setupAttributes(attributes: AttributeContainer, fragment: T) {
children.forEach { child -> child.setupAttributes(attributes, fragment) }
}
}