/
NamedDomainObjectCollectionExtensions.kt
429 lines (364 loc) · 15.3 KB
/
NamedDomainObjectCollectionExtensions.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
429
/*
* 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.NamedDomainObjectCollection
import org.gradle.api.NamedDomainObjectProvider
import org.gradle.api.UnknownDomainObjectException
import org.gradle.kotlin.dsl.support.illegalElementType
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.full.safeCast
/**
* Returns a collection containing the objects in this collection of the given type. The
* returned collection is live, so that when matching objects are later added to this
* collection, they are also visible in the filtered collection.
*
* @param S The type of objects to find.
* @return The matching objects. Returns an empty collection if there are no such objects
* in this collection.
* @see [NamedDomainObjectCollection.withType]
*/
inline fun <reified S : Any> NamedDomainObjectCollection<in S>.withType(): NamedDomainObjectCollection<S> =
withType(S::class.java)
/**
* Idiomatic way of referring to the provider of a well-known element of a collection via a delegate property.
*
* `tasks { val jar by existing }`
*
* @param T the domain object type
* @param C the concrete container type
*/
inline val <T : Any, C : NamedDomainObjectCollection<T>> C.existing: ExistingDomainObjectDelegateProvider<out C>
get() = ExistingDomainObjectDelegateProvider.of(this)
/**
* Idiomatic way of referring to the provider of a well-known element of a collection via a delegate property.
*
* `tasks { val jar by existing { ... } }`
*
* @param T the domain object type
* @param C the concrete container type
* @param action the configuration action
*/
fun <T : Any, C : NamedDomainObjectCollection<T>> C.existing(action: T.() -> Unit): ExistingDomainObjectDelegateProviderWithAction<out C, T> =
ExistingDomainObjectDelegateProviderWithAction.of(this, action)
/**
* Idiomatic way of referring to the provider of a well-known element of a collection via a delegate property.
*
* `tasks { val jar by existing(Jar::class) }`
*
* @param T the domain object type
* @param C the concrete container type
* @param type the domain object type
*/
fun <T : Any, C : NamedDomainObjectCollection<T>, U : T> C.existing(type: KClass<U>): ExistingDomainObjectDelegateProviderWithType<out C, U> =
ExistingDomainObjectDelegateProviderWithType.of(this, type)
/**
* Idiomatic way of referring to the provider of a well-known element of a collection via a delegate property.
*
* `tasks { val jar by existing(Jar::class) { ... } }`
*
* @param T the domain object type
* @param C the concrete container type
* @param type the domain object type
* @param action the configuration action
*/
fun <T : Any, C : NamedDomainObjectCollection<T>, U : T> C.existing(type: KClass<U>, action: U.() -> Unit): ExistingDomainObjectDelegateProviderWithTypeAndAction<out C, U> =
ExistingDomainObjectDelegateProviderWithTypeAndAction.of(this, type, action)
/**
* Holds the delegate provider for the `existing` property delegate with
* the purpose of providing specialized implementations for the `provideDelegate` operator
* based on the static type of the provider.
*/
class ExistingDomainObjectDelegateProvider<T>
private constructor(
internal val delegateProvider: T
) {
companion object {
fun <T> of(delegateProvider: T) =
ExistingDomainObjectDelegateProvider(delegateProvider)
}
}
/**
* Holds the delegate provider for the `existing` property delegate with
* the purpose of providing specialized implementations for the `provideDelegate` operator
* based on the static type of the provider.
*/
class ExistingDomainObjectDelegateProviderWithAction<C, T>
private constructor(
internal val delegateProvider: C,
internal val action: T.() -> Unit
) {
companion object {
fun <C, T> of(delegateProvider: C, action: T.() -> Unit) =
ExistingDomainObjectDelegateProviderWithAction(delegateProvider, action)
}
}
/**
* Holds the delegate provider and expected element type for the `existing` property delegate with
* the purpose of providing specialized implementations for the `provideDelegate` operator
* based on the static type of the provider.
*/
class ExistingDomainObjectDelegateProviderWithType<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>) =
ExistingDomainObjectDelegateProviderWithType(delegateProvider, type)
}
}
/**
* Holds the delegate provider and expected element type for the `existing` property delegate with
* the purpose of providing specialized implementations for the `provideDelegate` operator
* based on the static type of the provider.
*/
class ExistingDomainObjectDelegateProviderWithTypeAndAction<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) =
ExistingDomainObjectDelegateProviderWithTypeAndAction(delegateProvider, type, action)
}
}
/**
* Provides access to the [NamedDomainObjectProvider] for the element of the given
* property name from the container via a delegated property.
*/
operator fun <T : Any, C : NamedDomainObjectCollection<T>> ExistingDomainObjectDelegateProvider<C>.provideDelegate(
receiver: Any?,
property: KProperty<*>
) = ExistingDomainObjectDelegate.of(
delegateProvider.named(property.name)
)
/**
* Provides access to the [NamedDomainObjectProvider] for the element of the given
* property name from the container via a delegated property.
*/
operator fun <T : Any, C : NamedDomainObjectCollection<T>> ExistingDomainObjectDelegateProviderWithAction<C, T>.provideDelegate(
receiver: Any?,
property: KProperty<*>
) = ExistingDomainObjectDelegate.of(
delegateProvider.named(property.name).apply { configure(action) }
)
/**
* Provides access to the [NamedDomainObjectProvider] for the element of the given
* property name from the container via a delegated property.
*/
operator fun <T : Any, C : NamedDomainObjectCollection<T>, U : T> ExistingDomainObjectDelegateProviderWithType<C, U>.provideDelegate(
receiver: Any?,
property: KProperty<*>
) = ExistingDomainObjectDelegate.of(
delegateProvider.named(property.name, type)
)
/**
* Provides access to the [NamedDomainObjectProvider] for the element of the given
* property name from the container via a delegated property.
*/
operator fun <T : Any, C : NamedDomainObjectCollection<T>, U : T> ExistingDomainObjectDelegateProviderWithTypeAndAction<C, U>.provideDelegate(
receiver: Any?,
property: KProperty<*>
) = ExistingDomainObjectDelegate.of(
delegateProvider.named(property.name, type, action)
)
/**
* Holds a property delegate with the purpose of providing specialized implementations for the
* `getValue` operator based on the static type of the delegate.
*/
class ExistingDomainObjectDelegate<T>
private constructor(
internal val delegate: T
) {
companion object {
fun <T> of(delegate: T) =
ExistingDomainObjectDelegate(delegate)
}
}
/**
* Gets the delegate value.
*/
operator fun <T> ExistingDomainObjectDelegate<out T>.getValue(receiver: Any?, property: KProperty<*>): T =
delegate
/**
* Locates an object by name and type, without triggering its creation or configuration, failing if there is no such object.
*
* @see [NamedDomainObjectCollection.named]
*/
@Suppress("extension_shadowed_by_member")
inline fun <reified T : Any> NamedDomainObjectCollection<out Any>.named(name: String): NamedDomainObjectProvider<T> =
named(name, T::class)
/**
* Locates an object by name and type, without triggering its creation or configuration, failing if there is no such object.
*
* @see [NamedDomainObjectCollection.named]
*/
@Suppress("unchecked_cast")
fun <T : Any> NamedDomainObjectCollection<out Any>.named(name: String, type: KClass<T>): NamedDomainObjectProvider<T> =
(this as NamedDomainObjectCollection<T>).named(name, type.java)
/**
* Configures an object by name and type, without triggering its creation or configuration, failing if there is no such object.
*
* @see [NamedDomainObjectCollection.named]
* @see [NamedDomainObjectProvider.configure]
*/
@Suppress("unchecked_cast")
inline fun <reified T : Any> NamedDomainObjectCollection<out Any>.named(name: String, noinline configuration: T.() -> Unit): NamedDomainObjectProvider<T> =
(this as NamedDomainObjectCollection<T>).named(name, T::class.java, configuration)
/**
* Configures an object by name and type, without triggering its creation or configuration, failing if there is no such object.
*
* @see [NamedDomainObjectCollection.named]
* @see [NamedDomainObjectProvider.configure]
*/
@Suppress("unchecked_cast")
fun <T : Any> NamedDomainObjectCollection<out Any>.named(name: String, type: KClass<T>, configuration: T.() -> Unit): NamedDomainObjectProvider<T> =
(this as NamedDomainObjectCollection<T>).named(name, type.java, configuration)
/**
* Locates an object by name and casts it to the expected type [T].
*
* If an object with the given [name] is not found, [UnknownDomainObjectException] is thrown.
* If the object is found but cannot be cast to the expected type [T], [IllegalArgumentException] is thrown.
*
* @param name object name
* @return the object, never null
* @throws [UnknownDomainObjectException] When the given object is not found.
* @throws [IllegalArgumentException] When the given object cannot be cast to the expected type.
*/
@Suppress("extension_shadowed_by_member")
inline fun <reified T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String) =
getByName(name).let {
it as? T
?: throw illegalElementType(this, name, T::class, it::class)
}
/**
* Locates an object by name and casts it to the expected [type].
*
* If an object with the given [name] is not found, [UnknownDomainObjectException] is thrown.
* If the object is found but cannot be cast to the expected [type], [IllegalArgumentException] is thrown.
*
* @param name object name
* @param type expected type
* @return the object, never null
* @throws [UnknownDomainObjectException] When the given object is not found.
* @throws [IllegalArgumentException] When the given object cannot be cast to the expected type.
*/
fun <T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String, type: KClass<T>): T =
getByName(name).let {
type.safeCast(it)
?: throw illegalElementType(this, name, type, it::class)
}
/**
* Locates an object by name and casts it to the expected [type] then configures it.
*
* If an object with the given [name] is not found, [UnknownDomainObjectException] is thrown.
* If the object is found but cannot be cast to the expected [type], [IllegalArgumentException] is thrown.
*
* @param name object name
* @param configure configuration action to apply to the object before returning it
* @return the object, never null
* @throws [UnknownDomainObjectException] When the given object is not found.
* @throws [IllegalArgumentException] When the given object cannot be cast to the expected type.
*/
fun <T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String, type: KClass<T>, configure: T.() -> Unit): T =
getByName(name, type).also(configure)
/**
* Locates an object by name and casts it to the expected type [T] then configures it.
*
* If an object with the given [name] is not found, [UnknownDomainObjectException] is thrown.
* If the object is found but cannot be cast to the expected type [T], [IllegalArgumentException] is thrown.
*
* @param name object name
* @param configure configuration action to apply to the object before returning it
* @return the object, never null
* @throws [UnknownDomainObjectException] When the given object is not found.
* @throws [IllegalArgumentException] When the given object cannot be cast to the expected type.
*/
inline fun <reified T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String, configure: T.() -> Unit) =
getByName<T>(name).also(configure)
/**
* Idiomatic way of referring to an existing element in a collection
* via a delegate property.
*
* `tasks { val jar by getting }`
*/
inline val <T : Any, U : NamedDomainObjectCollection<out T>> U.getting
get() = NamedDomainObjectCollectionDelegateProvider.of(this)
/**
* Idiomatic way of referring and configuring an existing element in a collection
* via a delegate property.
*
* `tasks { val jar by getting { group = "My" } }`
*/
fun <T : Any, U : NamedDomainObjectCollection<T>> U.getting(configuration: T.() -> Unit) =
NamedDomainObjectCollectionDelegateProvider.of(this, configuration)
/**
* Enables typed access to container elements via delegated properties.
*/
class NamedDomainObjectCollectionDelegateProvider<T : Any>
private constructor(
internal val collection: NamedDomainObjectCollection<T>,
internal val configuration: (T.() -> Unit)?
) {
companion object {
fun <T : Any> of(
collection: NamedDomainObjectCollection<T>,
configuration: (T.() -> Unit)? = null
) =
NamedDomainObjectCollectionDelegateProvider(collection, configuration)
}
operator fun provideDelegate(thisRef: Any?, property: kotlin.reflect.KProperty<*>) = ExistingDomainObjectDelegate.of(
when (configuration) {
null -> collection.getByName(property.name)
else -> collection.getByName(property.name, configuration)
}
)
}
/**
* Locates an object by name, failing if there is no such object.
*
* @param name The object name
* @return The object with the given name.
* @throws [UnknownDomainObjectException] when there is no such object in this collection.
*
* @see [NamedDomainObjectCollection.getByName]
*/
operator fun <T : Any> NamedDomainObjectCollection<T>.get(name: String): T =
getByName(name)
/**
* Allows a [NamedDomainObjectCollection] to be used as a property delegate.
*
* @see [NamedDomainObjectCollection.named]
*/
operator fun <T : Any> NamedDomainObjectCollection<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): NamedDomainObjectProvider<T> =
named(property.name)
/**
* Allows a [NamedDomainObjectProvider] to be used as a property delegate.
*
* @see [NamedDomainObjectProvider.get]
*/
@Suppress("nothing_to_inline", "unchecked_cast")
inline operator fun <T : Any, reified U : T> NamedDomainObjectProvider<out T>.getValue(thisRef: Any?, property: KProperty<*>): U =
get().let {
it as? U
?: throw illegalElementType(this, property.name, U::class, it::class)
}
/**
* Required due to [KT-25810](https://youtrack.jetbrains.com/issue/KT-25810).
*/
operator fun <T : Any> T.provideDelegate(receiver: Any?, property: KProperty<*>): T = this