-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ConventionExtensions.kt
110 lines (95 loc) · 4.31 KB
/
ConventionExtensions.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
/*
* 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.
*/
@file:Suppress("DEPRECATION")
package org.gradle.kotlin.dsl
import org.gradle.api.plugins.Convention
import org.gradle.kotlin.dsl.accessors.runtime.conventionOf
import org.gradle.kotlin.dsl.accessors.runtime.conventionPluginByName
import kotlin.reflect.KClass
/**
* Looks for the convention plugin of a given name and casts it to the expected type [T].
*
* If no convention is found or if the one found cannot be cast to the expected type it will throw an [IllegalStateException].
*
* @param name convention plugin name
* @return the convention plugin, never null
* @throws [IllegalStateException] When the convention cannot be found or cast to the expected type.
*/
@Deprecated("The concept of conventions is deprecated. Use extensions instead.")
inline fun <reified T : Any> Convention.getPluginByName(name: String): T =
conventionPluginByName(this, name).let {
(it as T?) ?: throw IllegalStateException("Convention '$name' of type '${it::class.java.name}' cannot be cast to '${T::class.java.name}'.")
}
/**
* Locates the plugin convention object with the given type.
*
* @param T the convention plugin type
* @return the convention plugin
* @throws [IllegalStateException] when there is no such object contained in this convention, or when there are multiple such objects
* @see [Convention.getPlugin]
*/
@Deprecated("The concept of conventions is deprecated. Use extensions instead.")
inline fun <reified T : Any> Convention.getPlugin(): T =
getPlugin(T::class)
/**
* Locates the plugin convention object with the given type.
*
* @param conventionType the convention plugin type
* @return the convention plugin
* @throws [IllegalStateException] when there is no such object contained in this convention, or when there are multiple such objects
* @see [Convention.getPlugin]
*/
@Deprecated("The concept of conventions is deprecated. Use extensions instead.")
fun <T : Any> Convention.getPlugin(conventionType: KClass<T>): T =
getPlugin(conventionType.java)
/**
* Locates the plugin convention object with the given type.
*
* @param T the convention plugin type.
* @return the convention plugin, or null if there is no such convention plugin
* @throws [IllegalStateException] when there are multiple matching objects
* @see [Convention.findPlugin]
*/
@Deprecated("The concept of conventions is deprecated. Use extensions instead.")
inline fun <reified T : Any> Convention.findPlugin(): T? =
findPlugin(T::class)
/**
* Locates the plugin convention object with the given type.
*
* @param conventionType the convention plugin type.
* @return the convention plugin, or null if there is no such convention plugin
* @throws [IllegalStateException] when there are multiple matching objects
* @see [Convention.findPlugin]
*/
@Deprecated("The concept of conventions is deprecated. Use extensions instead.")
fun <T : Any> Convention.findPlugin(conventionType: KClass<T>): T? =
findPlugin(conventionType.java)
/**
* Evaluates the given [function] against the convention plugin of the given [conventionType].
*
* @param conventionType the type of the convention to be located.
* @param function function to be evaluated.
* @return the value returned by the given [function].
* @throws [IllegalStateException] When the receiver does not support convention plugins, when there is no convention plugin of the given type, or when there are multiple such plugins.
*
* @see [Convention.getPlugin]
*/
@Deprecated("The concept of conventions is deprecated. Use extensions instead.")
inline fun <ConventionType : Any, ReturnType> Any.withConvention(
conventionType: KClass<ConventionType>,
function: ConventionType.() -> ReturnType
): ReturnType =
conventionOf(this).getPlugin(conventionType.java).run(function)