/* * Copyright 2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.gradle.kotlin.dsl import org.gradle.api.Action import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.NamedDomainObjectProvider import org.gradle.api.PolymorphicDomainObjectContainer import org.gradle.kotlin.dsl.support.delegates.NamedDomainObjectContainerDelegate import kotlin.reflect.KClass import kotlin.reflect.KProperty /** * Allows the container to be configured via an augmented DSL. * * @param configuration The expression to configure this container with * @return The container. */ @Suppress("nothing_to_inline") inline operator fun > C.invoke( configuration: Action> ): C = apply { configuration.execute(NamedDomainObjectContainerScope.of(this)) } /** * Property delegate for registering new elements in the container. * * `tasks { val rebuild by registering }` * * @param T the domain object type * @param C the concrete container type */ inline val > C.registering: RegisteringDomainObjectDelegateProvider get() = RegisteringDomainObjectDelegateProvider.of(this) /** * Property delegate for registering new elements in the container. * * ```kotlin * tasks { * val rebuild by registering { * dependsOn("clean", "build") * } * } * ``` * * @param T the domain object type * @param C the concrete container type * @param action the configuration action */ fun > C.registering(action: T.() -> Unit): RegisteringDomainObjectDelegateProviderWithAction = RegisteringDomainObjectDelegateProviderWithAction.of(this, action) /** * Property delegate for registering new elements in the container. * * `tasks { val jar by registering(Jar::class) }` * * @param T the domain object type * @param C the concrete container type * @param type the domain object type */ fun , U : T> C.registering(type: KClass): RegisteringDomainObjectDelegateProviderWithType = RegisteringDomainObjectDelegateProviderWithType.of(this, type) /** * Property delegate for registering new elements in the container. * * `tasks { val jar by registering(Jar::class) { } }` * * @param T the container element type * @param C the container type * @param U the desired domain object type * @param type the domain object type * @param action the configuration action */ fun , U : T> C.registering( type: KClass, action: U.() -> Unit ): RegisteringDomainObjectDelegateProviderWithTypeAndAction = RegisteringDomainObjectDelegateProviderWithTypeAndAction.of(this, type, action) /** * Registers an element and provides a delegate with the resulting [NamedDomainObjectProvider]. */ operator fun > RegisteringDomainObjectDelegateProvider.provideDelegate( receiver: Any?, property: KProperty<*> ) = ExistingDomainObjectDelegate.of( delegateProvider.register(property.name) ) /** * Registers an element and provides a delegate with the resulting [NamedDomainObjectProvider]. */ operator fun > RegisteringDomainObjectDelegateProviderWithAction.provideDelegate( receiver: Any?, property: KProperty<*> ) = ExistingDomainObjectDelegate.of( delegateProvider.register(property.name, action) ) /** * Registers an element and provides a delegate with the resulting [NamedDomainObjectProvider]. */ operator fun , U : T> RegisteringDomainObjectDelegateProviderWithType.provideDelegate( receiver: Any?, property: KProperty<*> ) = ExistingDomainObjectDelegate.of( delegateProvider.register(property.name, type.java) ) /** * Registers an element and provides a delegate with the resulting [NamedDomainObjectProvider]. */ operator fun , U : T> RegisteringDomainObjectDelegateProviderWithTypeAndAction.provideDelegate( receiver: Any?, property: KProperty<*> ) = ExistingDomainObjectDelegate.of( delegateProvider.register(property.name, type.java, action) ) /** * Holds the delegate provider for the `registering` property delegate with * the purpose of providing specialized implementations for the `provideDelegate` operator * based on the static type of the provider. */ class RegisteringDomainObjectDelegateProvider private constructor( internal val delegateProvider: T ) { companion object { fun of(delegateProvider: T) = RegisteringDomainObjectDelegateProvider(delegateProvider) } } /** * Holds the delegate provider for the `registering` property delegate with * the purpose of providing specialized implementations for the `provideDelegate` operator * based on the static type of the provider. */ class RegisteringDomainObjectDelegateProviderWithAction private constructor( internal val delegateProvider: C, internal val action: T.() -> Unit ) { companion object { fun of(delegateProvider: C, action: T.() -> Unit) = RegisteringDomainObjectDelegateProviderWithAction(delegateProvider, action) } } /** * Holds the delegate provider and expected element type for the `registering` property delegate with * the purpose of providing specialized implementations for the `provideDelegate` operator * based on the static type of the provider. */ class RegisteringDomainObjectDelegateProviderWithType private constructor( internal val delegateProvider: T, internal val type: KClass ) { companion object { fun of(delegateProvider: T, type: KClass) = RegisteringDomainObjectDelegateProviderWithType(delegateProvider, type) } } /** * Holds the delegate provider and expected element type for the `registering` property delegate with * the purpose of providing specialized implementations for the `provideDelegate` operator * based on the static type of the provider. */ class RegisteringDomainObjectDelegateProviderWithTypeAndAction private constructor( internal val delegateProvider: T, internal val type: KClass, internal val action: U.() -> Unit ) { companion object { fun of(delegateProvider: T, type: KClass, action: U.() -> Unit) = RegisteringDomainObjectDelegateProviderWithTypeAndAction(delegateProvider, type, action) } } /** * Receiver for [NamedDomainObjectContainer] configuration blocks. */ open class NamedDomainObjectContainerScope internal constructor( override val delegate: NamedDomainObjectContainer ) : NamedDomainObjectContainerDelegate(), PolymorphicDomainObjectContainer { companion object { fun of(container: NamedDomainObjectContainer) = NamedDomainObjectContainerScope(container) } override fun register(name: String, type: Class, configurationAction: Action): NamedDomainObjectProvider = polymorphicDomainObjectContainer().register(name, type, configurationAction) override fun register(name: String, type: Class): NamedDomainObjectProvider = polymorphicDomainObjectContainer().register(name, type) override fun create(name: String, type: Class): U = polymorphicDomainObjectContainer().create(name, type) override fun create(name: String, type: Class, configuration: Action): U = polymorphicDomainObjectContainer().create(name, type, configuration) override fun maybeCreate(name: String, type: Class): U = polymorphicDomainObjectContainer().maybeCreate(name, type) override fun containerWithType(type: Class): NamedDomainObjectContainer = polymorphicDomainObjectContainer().containerWithType(type) /** * Configures an object by name, without triggering its creation or configuration, failing if there is no such object. * * @see [NamedDomainObjectContainer.named] * @see [NamedDomainObjectProvider.configure] */ operator fun String.invoke(configuration: T.() -> Unit): NamedDomainObjectProvider = named(this).apply { configure(configuration) } /** * Configures an object by name, without triggering its creation or configuration, failing if there is no such object. * * @see [PolymorphicDomainObjectContainer.named] * @see [NamedDomainObjectProvider.configure] */ operator fun String.invoke(type: KClass, configuration: U.() -> Unit): NamedDomainObjectProvider = delegate.named(this, type, configuration) /** * Locates an object by name and type, without triggering its creation or configuration, failing if there is no such object. * * @see [PolymorphicDomainObjectContainer.named] */ operator fun String.invoke(type: KClass): NamedDomainObjectProvider = delegate.named(this, type) /** * Cast this to [PolymorphicDomainObjectContainer] or throw [IllegalArgumentException]. * * We must rely on the dynamic cast and possible runtime failure here due to a Kotlin extension member limitation. * Kotlin currently can't disambiguate between invoke operators with more specific receivers in a type hierarchy. * * See https://youtrack.jetbrains.com/issue/KT-15711 */ private fun polymorphicDomainObjectContainer() = delegate as? PolymorphicDomainObjectContainer ?: throw IllegalArgumentException("Container '$delegate' is not polymorphic.") } /** * Provides a property delegate that creates elements of the default collection type. */ val NamedDomainObjectContainer.creating get() = NamedDomainObjectContainerCreatingDelegateProvider.of(this) /** * Provides a property delegate that creates elements of the default collection type with the given [configuration]. * * `val myElement by myContainer.creating { myProperty = 42 }` */ fun NamedDomainObjectContainer.creating(configuration: T.() -> Unit) = NamedDomainObjectContainerCreatingDelegateProvider.of(this, configuration) /** * A property delegate that creates elements in the given [NamedDomainObjectContainer]. * * See [creating] */ class NamedDomainObjectContainerCreatingDelegateProvider private constructor( internal val container: NamedDomainObjectContainer, internal val configuration: (T.() -> Unit)? = null ) { companion object { fun of(container: NamedDomainObjectContainer, configuration: (T.() -> Unit)? = null) = NamedDomainObjectContainerCreatingDelegateProvider(container, configuration) } operator fun provideDelegate(thisRef: Any?, property: KProperty<*>) = ExistingDomainObjectDelegate.of( when (configuration) { null -> container.create(property.name) else -> container.create(property.name, configuration) } ) } /** * Provides a property delegate that creates elements of the given [type]. */ fun PolymorphicDomainObjectContainer.creating(type: KClass) = PolymorphicDomainObjectContainerCreatingDelegateProvider.of(this, type.java) /** * Provides a property delegate that creates elements of the given [type] with the given [configuration]. */ fun PolymorphicDomainObjectContainer.creating(type: KClass, configuration: U.() -> Unit) = creating(type.java, configuration) /** * Provides a property delegate that creates elements of the given [type] expressed as a [java.lang.Class] * with the given [configuration]. */ fun PolymorphicDomainObjectContainer.creating(type: Class, configuration: U.() -> Unit) = PolymorphicDomainObjectContainerCreatingDelegateProvider.of(this, type, configuration) /** * A property delegate that creates elements of the given [type] with the given [configuration] in the given [container]. */ class PolymorphicDomainObjectContainerCreatingDelegateProvider private constructor( internal val container: PolymorphicDomainObjectContainer, internal val type: Class, internal val configuration: (U.() -> Unit)? = null ) { companion object { fun of(container: PolymorphicDomainObjectContainer, type: Class, configuration: (U.() -> Unit)? = null) = PolymorphicDomainObjectContainerCreatingDelegateProvider(container, type, configuration) } operator fun provideDelegate(thisRef: Any?, property: KProperty<*>) = ExistingDomainObjectDelegate.of( when (configuration) { null -> container.create(property.name, type) else -> container.create(property.name, type, configuration) } ) } /** * Provides a property delegate that gets elements of the given [type] and applies the given [configuration]. */ fun NamedDomainObjectContainer.getting(type: KClass, configuration: U.() -> Unit) = PolymorphicDomainObjectContainerGettingDelegateProvider.of(this, type, configuration) /** * Provides a property delegate that gets elements of the given [type]. */ fun NamedDomainObjectContainer.getting(type: KClass) = PolymorphicDomainObjectContainerGettingDelegateProvider.of(this, type) /** * A property delegate that gets elements of the given [type] from the given [container] * and applies the given [configuration]. */ class PolymorphicDomainObjectContainerGettingDelegateProvider private constructor( internal val container: NamedDomainObjectContainer, internal val type: KClass, internal val configuration: (U.() -> Unit)? = null ) { companion object { fun of(container: NamedDomainObjectContainer, type: KClass, configuration: (U.() -> Unit)? = null) = PolymorphicDomainObjectContainerGettingDelegateProvider(container, type, configuration) } operator fun provideDelegate(thisRef: Any?, property: KProperty<*>) = ExistingDomainObjectDelegate.of( when (configuration) { null -> container.getByName(property.name, type) else -> container.getByName(property.name, type, configuration) } ) }