-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
NamedDomainObjectContainerExtensions.kt
428 lines (361 loc) · 15.6 KB
/
NamedDomainObjectContainerExtensions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* 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.internal.deprecation.DeprecationLogger
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 <T : Any, C : NamedDomainObjectContainer<T>> C.invoke(
configuration: Action<NamedDomainObjectContainerScope<T>>
): 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 <T : Any, C : NamedDomainObjectContainer<T>> C.registering: RegisteringDomainObjectDelegateProvider<out C>
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 <T : Any, C : NamedDomainObjectContainer<T>> C.registering(action: T.() -> Unit): RegisteringDomainObjectDelegateProviderWithAction<out C, T> =
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 <T : Any, C : PolymorphicDomainObjectContainer<T>, U : T> C.registering(type: KClass<U>): RegisteringDomainObjectDelegateProviderWithType<out C, U> =
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 <T : Any, C : PolymorphicDomainObjectContainer<T>, U : T> C.registering(
type: KClass<U>,
action: U.() -> Unit
): RegisteringDomainObjectDelegateProviderWithTypeAndAction<out C, U> =
RegisteringDomainObjectDelegateProviderWithTypeAndAction.of(this, type, action)
/**
* Registers an element and provides a delegate with the resulting [NamedDomainObjectProvider].
*/
operator fun <T : Any, C : NamedDomainObjectContainer<T>> RegisteringDomainObjectDelegateProvider<C>.provideDelegate(
receiver: Any?,
property: KProperty<*>
) = ExistingDomainObjectDelegate.of(
delegateProvider.register(property.name)
)
/**
* Registers an element and provides a delegate with the resulting [NamedDomainObjectProvider].
*/
operator fun <T : Any, C : NamedDomainObjectContainer<T>> RegisteringDomainObjectDelegateProviderWithAction<C, T>.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 <T : Any, C : PolymorphicDomainObjectContainer<T>, U : T> RegisteringDomainObjectDelegateProviderWithType<C, U>.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 <T : Any, C : PolymorphicDomainObjectContainer<T>, U : T> RegisteringDomainObjectDelegateProviderWithTypeAndAction<C, U>.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<T>
private constructor(
internal val delegateProvider: T
) {
companion object {
fun <T> 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<C, T>
private constructor(
internal val delegateProvider: C,
internal val action: T.() -> Unit
) {
companion object {
fun <C, T> 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<T, U : Any>
private constructor(
internal val delegateProvider: T,
internal val type: KClass<U>
) {
companion object {
fun <T, U : Any> of(delegateProvider: T, type: KClass<U>) =
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<T, U : Any>
private constructor(
internal val delegateProvider: T,
internal val type: KClass<U>,
internal val action: U.() -> Unit
) {
companion object {
fun <T, U : Any> of(delegateProvider: T, type: KClass<U>, action: U.() -> Unit) =
RegisteringDomainObjectDelegateProviderWithTypeAndAction(delegateProvider, type, action)
}
}
/**
* Receiver for [NamedDomainObjectContainer] configuration blocks.
*/
open
class NamedDomainObjectContainerScope<T : Any>
internal constructor(
override val delegate: NamedDomainObjectContainer<T>
) : NamedDomainObjectContainerDelegate<T>(), PolymorphicDomainObjectContainer<T> {
companion object {
fun <T : Any> of(container: NamedDomainObjectContainer<T>) =
NamedDomainObjectContainerScope(container)
}
override fun <U : T> register(name: String, type: Class<U>, configurationAction: Action<in U>): NamedDomainObjectProvider<U> =
polymorphicDomainObjectContainer().register(name, type, configurationAction)
override fun <U : T> register(name: String, type: Class<U>): NamedDomainObjectProvider<U> =
polymorphicDomainObjectContainer().register(name, type)
override fun <U : T> create(name: String, type: Class<U>): U =
polymorphicDomainObjectContainer().create(name, type)
override fun <U : T> create(name: String, type: Class<U>, configuration: Action<in U>): U =
polymorphicDomainObjectContainer().create(name, type, configuration)
override fun <U : T> maybeCreate(name: String, type: Class<U>): U =
polymorphicDomainObjectContainer().maybeCreate(name, type)
override fun <U : T> containerWithType(type: Class<U>): NamedDomainObjectContainer<U> =
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<T> =
named(this).apply { configure(configuration) }
/**
* Locates an object by name, without triggering its creation or configuration, failing if there is no such object.
*
* @see [NamedDomainObjectContainer.named]
*/
@Deprecated("Use named(String) instead.", ReplaceWith("named(this)"))
operator fun String.invoke(): NamedDomainObjectProvider<T> {
DeprecationLogger.deprecateBehaviour(String.format("Domain object '%s' found by String.invoke() notation.", this))
.withContext("The \"name\"() notation can cause confusion with methods provided by Kotlin or the JDK.")
.withAdvice("Use named(String) instead.")
.willBeRemovedInGradle9()
.withUpgradeGuideSection(8, "string_invoke")
.nagUser()
return delegate.named(this)
}
/**
* 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 <U : T> String.invoke(type: KClass<U>, configuration: U.() -> Unit): NamedDomainObjectProvider<U> =
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 <U : T> String.invoke(type: KClass<U>): NamedDomainObjectProvider<U> =
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<T>
?: throw IllegalArgumentException("Container '$delegate' is not polymorphic.")
}
/**
* Provides a property delegate that creates elements of the default collection type.
*/
val <T : Any> NamedDomainObjectContainer<T>.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 <T : Any> NamedDomainObjectContainer<T>.creating(configuration: T.() -> Unit) =
NamedDomainObjectContainerCreatingDelegateProvider.of(this, configuration)
/**
* A property delegate that creates elements in the given [NamedDomainObjectContainer].
*
* See [creating]
*/
class NamedDomainObjectContainerCreatingDelegateProvider<T : Any>
private constructor(
internal val container: NamedDomainObjectContainer<T>,
internal val configuration: (T.() -> Unit)? = null
) {
companion object {
fun <T : Any> of(container: NamedDomainObjectContainer<T>, 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 <T : Any, U : T> PolymorphicDomainObjectContainer<T>.creating(type: KClass<U>) =
PolymorphicDomainObjectContainerCreatingDelegateProvider.of(this, type.java)
/**
* Provides a property delegate that creates elements of the given [type] with the given [configuration].
*/
fun <T : Any, U : T> PolymorphicDomainObjectContainer<T>.creating(type: KClass<U>, 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 <T : Any, U : T> PolymorphicDomainObjectContainer<T>.creating(type: Class<U>, 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<T : Any, U : T>
private constructor(
internal val container: PolymorphicDomainObjectContainer<T>,
internal val type: Class<U>,
internal val configuration: (U.() -> Unit)? = null
) {
companion object {
fun <T : Any, U : T> of(container: PolymorphicDomainObjectContainer<T>, type: Class<U>, 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 <T : Any, U : T> NamedDomainObjectContainer<T>.getting(type: KClass<U>, configuration: U.() -> Unit) =
PolymorphicDomainObjectContainerGettingDelegateProvider.of(this, type, configuration)
/**
* Provides a property delegate that gets elements of the given [type].
*/
fun <T : Any, U : T> NamedDomainObjectContainer<T>.getting(type: KClass<U>) =
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<T : Any, U : T>
private constructor(
internal val container: NamedDomainObjectContainer<T>,
internal val type: KClass<U>,
internal val configuration: (U.() -> Unit)? = null
) {
companion object {
fun <T : Any, U : T> of(container: NamedDomainObjectContainer<T>, type: KClass<U>, 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)
}
)
}