-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DependenciesExtensions.kt
281 lines (229 loc) · 9.45 KB
/
DependenciesExtensions.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
/*
* Copyright 2022 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:Incubating
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")
package org.gradle.kotlin.dsl
import org.gradle.api.Action
import org.gradle.api.Incubating
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.DependencyConstraint
import org.gradle.api.artifacts.ExternalModuleDependency
import org.gradle.api.artifacts.FileCollectionDependency
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.dsl.Dependencies
import org.gradle.api.artifacts.dsl.DependencyCollector
import org.gradle.api.artifacts.dsl.DependencyFactory
import org.gradle.api.artifacts.dsl.DependencyModifier
import org.gradle.api.artifacts.dsl.GradleDependencies
import org.gradle.api.file.FileCollection
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderConvertible
/**
* This file is used to add [Kotlin extension functions](https://kotlinlang.org/docs/extensions.html) to [Dependencies], [DependencyCollector] and [DependencyModifier] to make the Kotlin DSL more idiomatic.
*
* These extension functions allow an interface to implement a dependencies block in the Kotlin DSL by
* - exposing an instance of [DependencyCollector] to add dependencies without explicitly calling [DependencyCollector.add] or [DependencyCollector.addConstraint]
* - exposing an instance of [DependencyModifier] to modify dependencies without explicitly calling [DependencyModifier.modify]
*
* There are `invoke(...)` equivalents for all the `add(...)` and `addConstraint(...)` methods in [DependencyCollector].
*
* There are `invoke(...)` equivalents for all the `modify(...)` methods in [DependencyModifier].
*
* @since 8.0
*
* @see org.gradle.api.internal.artifacts.dsl.dependencies.DependenciesExtensionModule
* @see Dependencies
* @see DependencyCollector
* @see DependencyModifier
* @see DependencyFactory
*
* @sample DependenciesExtensions.sample
*/
@Suppress("unused")
private
class DependenciesExtensions {
interface MyDependencies : GradleDependencies {
val implementation: DependencyCollector
val testFixtures: DependencyModifier
operator fun invoke(action: Action<in MyDependencies>)
}
fun sample(dependencies: MyDependencies) {
// In a Kotlin DSL build script
dependencies {
// Add a dependency by String
implementation("org:foo:1.0") // is getImplementation().add("org:foo:1.0")
// Add a dependency with explicit coordinate parameters
implementation(module(group = "org", name = "foo", version = "1.0")) // is getImplementation().add(module("org", "foo", "1.0"))
// Add dependencies on projects
implementation(project(":path")) // is getImplementation().add(project(":path"))
implementation(project()) // is getImplementation().add(project())
// Add a dependency on the Gradle API
implementation(gradleApi()) // is getImplementation().add(gradleApi())
// Modify a dependency to select test fixtures
implementation(testFixtures("org:foo:1.0")) // is getImplementation().add(getTestFixtures().modify("org:foo:1.0"))
// Add a constraint by String
implementation(constraint("org:foo:1.0")) // is getImplementation().addConstraint(constraint("org:foo:1.0"))
// Add a constraint on projects
implementation(constraint(project(":path"))) // is getImplementation().addConstraint(constraint(project(":path")))
implementation(constraint(project())) // is getImplementation().addConstraint(constraint(project()))
}
}
}
// The #module and #constraint methods here allow the usage of named arguments in Kotlin, even though the signature is overall the same as the Java method.
/**
* Creates a dependency based on the group, name and version (GAV) coordinates.
*
* @since 8.0
*/
fun Dependencies.module(group: String?, name: String, version: String?): ExternalModuleDependency = module(group, name, version)
/**
* Modifies a dependency to select the variant of the given module.
*
* @see DependencyModifier
* @since 8.0
*/
operator fun <D : ModuleDependency> DependencyModifier.invoke(dependency: D): D = modify(dependency)
/**
* Modifies a dependency to select the variant of the given module.
*
* @see DependencyModifier
* @since 8.0
*/
operator fun DependencyModifier.invoke(dependencyNotation: CharSequence): ExternalModuleDependency = modify(dependencyNotation)
/**
* Modifies a dependency to select the variant of the given module.
*
* @see DependencyModifier
* @since 8.0
*/
operator fun DependencyModifier.invoke(dependency: ProviderConvertible<out MinimalExternalModuleDependency>): Provider<out MinimalExternalModuleDependency> = modify(dependency)
/**
* Modifies a dependency to select the variant of the given module.
*
* @see DependencyModifier
* @since 8.0
*/
operator fun DependencyModifier.invoke(dependency: Provider<out ModuleDependency>): Provider<out ModuleDependency> = modify(dependency)
/**
* Add a dependency.
*
* @param dependencyNotation dependency to add
* @see DependencyFactory.create
* @since 8.6
*/
operator fun DependencyCollector.invoke(dependencyNotation: CharSequence) = add(dependencyNotation)
/**
* Add a dependency.
*
* @param dependencyNotation dependency to add
* @param configuration an action to configure the dependency
* @see DependencyFactory.create
* @since 8.6
*/
operator fun DependencyCollector.invoke(dependencyNotation: CharSequence, configuration: Action<in ExternalModuleDependency>) = add(dependencyNotation, configuration)
/**
* Add a dependency.
*
* @param files files to add as a dependency
* @since 8.6
*/
operator fun DependencyCollector.invoke(files: FileCollection) = add(files)
/**
* Add a dependency.
*
* @param files files to add as a dependency
* @param configuration an action to configure the dependency
* @since 8.6
*/
operator fun DependencyCollector.invoke(files: FileCollection, configuration: Action<in FileCollectionDependency>) = add(files, configuration)
/**
* Add a dependency.
*
* @param externalModule external module to add as a dependency
* @since 8.6
*/
operator fun DependencyCollector.invoke(externalModule: ProviderConvertible<out MinimalExternalModuleDependency>) = add(externalModule)
/**
* Add a dependency.
*
* @param externalModule external module to add as a dependency
* @param configuration an action to configure the dependency
* @since 8.6
*/
operator fun DependencyCollector.invoke(externalModule: ProviderConvertible<out MinimalExternalModuleDependency>, configuration: Action<in ExternalModuleDependency>) = add(externalModule, configuration)
/**
* Add a dependency.
*
* @param dependency dependency to add
* @since 8.6
*/
operator fun DependencyCollector.invoke(dependency: Dependency) = add(dependency)
/**
* Add a dependency.
*
* @param dependency dependency to add
* @param configuration an action to configure the dependency
* @since 8.6
*/
operator fun <D : Dependency> DependencyCollector.invoke(dependency: D, configuration: Action<in D>) = add(dependency, configuration)
/**
* Add a dependency.
*
* @param dependency dependency to add
* @since 8.6
*/
operator fun DependencyCollector.invoke(dependency: Provider<out Dependency>) = add(dependency)
/**
* Add a dependency.
*
* @param dependency dependency to add
* @param configuration an action to configure the dependency
* @since 8.6
*/
operator fun <D : Dependency> DependencyCollector.invoke(dependency: Provider<out D>, configuration: Action<in D>) = add(dependency, configuration)
/**
* Add a dependency constraint.
*
* @param dependencyConstraint dependency constraint to add
* @since 8.7
*/
operator fun DependencyCollector.invoke(dependencyConstraint: DependencyConstraint) = addConstraint(dependencyConstraint)
/**
* Add a dependency constraint.
*
* @param dependencyConstraint dependency constraint to add
* @param configuration an action to configure the dependency constraint
* @since 8.7
*/
operator fun DependencyCollector.invoke(dependencyConstraint: DependencyConstraint, configuration: Action<in DependencyConstraint>) = addConstraint(dependencyConstraint, configuration)
/**
* Add a dependency constraint.
*
* @param dependencyConstraint dependency constraint to add
* @since 8.7
*/
@JvmName("invokeConstraint")
operator fun DependencyCollector.invoke(dependencyConstraint: Provider<out DependencyConstraint>) = addConstraint(dependencyConstraint)
/**
* Add a dependency constraint.
*
* @param dependencyConstraint dependency constraint to add
* @param configuration an action to configure the dependency constraint
* @since 8.7
*/
@JvmName("invokeConstraint")
operator fun DependencyCollector.invoke(dependencyConstraint: Provider<out DependencyConstraint>, configuration: Action<in DependencyConstraint>) = addConstraint(dependencyConstraint, configuration)