forked from ben-manes/gradle-versions-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyUpdatesTask.kt
180 lines (157 loc) · 6.43 KB
/
DependencyUpdatesTask.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
package com.github.benmanes.gradle.versions.updates
import com.github.benmanes.gradle.versions.reporter.Reporter
import com.github.benmanes.gradle.versions.reporter.result.Result
import com.github.benmanes.gradle.versions.updates.gradle.GradleReleaseChannel.RELEASE_CANDIDATE
import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ComponentFilter
import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ComponentSelectionWithCurrent
import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ResolutionStrategyWithCurrent
import groovy.lang.Closure
import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.util.ConfigureUtil
import java.lang.IllegalArgumentException
import javax.annotation.Nullable
/**
* A task that reports which dependencies have later versions.
*/
open class DependencyUpdatesTask : DefaultTask() { // tasks can't be final
/** Returns the resolution revision level. */
@Input
var revision: String = "milestone"
get() = (System.getProperties()["revision"] ?: field) as String
/** Returns the resolution revision level. */
@Input
var gradleReleaseChannel: String = RELEASE_CANDIDATE.id
get() = (System.getProperties()["gradleReleaseChannel"] ?: field) as String
/** Returns the outputDir destination. */
@Input
var outputDir: String =
"${project.buildDir.path.replace(project.projectDir.path + "/", "")}/dependencyUpdates"
get() = (System.getProperties()["outputDir"] ?: field) as String
/** Returns the filename of the report. */
@Input
@Optional
var reportfileName: String = "report"
get() = (System.getProperties()["reportfileName"] ?: field) as String
/**
* Sets an output formatting for the task result. It can either be a [String] referencing one of
* the existing output formatters (i.e. "text", "xml", "json" or "html"), a [String] containing a
* comma-separated list with any combination of the existing output formatters (e.g. "xml,json"),
* or a [Reporter]/a [Closure] with a custom output formatting implementation.
*
* Use the [outputFormatter] function as an alternative to set a custom output formatting using
* the trailing closure/lambda syntax.
*/
var outputFormatter: Any?
@Internal get() = null
set(value) {
outputFormatterArgument = when (value) {
is String -> OutputFormatterArgument.BuiltIn(value)
is Reporter -> OutputFormatterArgument.CustomReporter(value)
// Kept for retro-compatibility with "outputFormatter = {}" usages.
is Closure<*> -> OutputFormatterArgument.CustomAction { value.call(it) }
else -> throw IllegalArgumentException(
"Unsupported output formatter provided $value. Please use a String, a Reporter/Closure, " +
"or alternatively provide a function using the `outputFormatter(Action<Result>)` API."
)
}
}
/**
* Keeps a reference to the latest [OutputFormatterArgument] provided either via the [outputFormatter]
* property or the [outputFormatter] function.
*/
private var outputFormatterArgument: OutputFormatterArgument = OutputFormatterArgument.DEFAULT
@Input
@Optional
fun getOutputFormatterName(): String? {
return with(outputFormatterArgument) {
if (this is OutputFormatterArgument.BuiltIn) {
formatterNames
} else {
null
}
}
}
// Groovy generates both get/is accessors for boolean properties unless we manually define some.
// Gradle will reject this behavior starting in 7.0 so we make sure to define accessors ourselves.
@Input
var checkForGradleUpdate: Boolean = true
@Input
var checkConstraints: Boolean = false
@Input
var checkBuildEnvironmentConstraints: Boolean = false
@Internal
@Nullable
var resolutionStrategy: Closure<*>? = null
@Nullable
private var resolutionStrategyAction: Action<in ResolutionStrategyWithCurrent>? = null
init {
description = "Displays the dependency updates for the project."
group = "Help"
outputs.upToDateWhen { false }
callIncompatibleWithConfigurationCache()
}
@TaskAction
fun dependencyUpdates() {
project.evaluationDependsOnChildren()
if (resolutionStrategy != null) {
resolutionStrategy(ConfigureUtil.configureUsing(resolutionStrategy))
logger.warn(
"dependencyUpdates.resolutionStrategy: " +
"Remove the assignment operator, \"=\", when setting this task property"
)
}
val evaluator = DependencyUpdates(
project, resolutionStrategyAction, revision,
outputFormatter(), outputDir, reportfileName, checkForGradleUpdate,
gradleReleaseChannel, checkConstraints, checkBuildEnvironmentConstraints
)
val reporter = evaluator.run()
reporter.write()
}
fun rejectVersionIf(filter: ComponentFilter) {
resolutionStrategy { strategy ->
strategy.componentSelection { selection ->
selection.all(
Action<ComponentSelectionWithCurrent> { current ->
val isNotNull = current.currentVersion != null && current.candidate.version != null
if (isNotNull && filter.reject(current)) {
current.reject("Rejected by rejectVersionIf ")
}
}
)
}
}
}
/**
* Sets the [resolutionStrategy] to the provided strategy.
*
* @param resolutionStrategy the resolution strategy
*/
fun resolutionStrategy(resolutionStrategy: Action<in ResolutionStrategyWithCurrent>? = null) {
this.resolutionStrategyAction = resolutionStrategy
this.resolutionStrategy = null
}
/** Returns the outputDir format. */
private fun outputFormatter(): OutputFormatterArgument {
val outputFormatterProperty = System.getProperties()["outputFormatter"] as? String
return outputFormatterProperty?.let { OutputFormatterArgument.BuiltIn(it) }
?: outputFormatterArgument
}
/**
* Sets a custom output formatting for the task result.
*
* @param action [Action] implementing the desired custom output formatting.
*/
fun outputFormatter(action: Action<Result>) {
outputFormatterArgument = OutputFormatterArgument.CustomAction(action)
}
private fun callIncompatibleWithConfigurationCache() {
this::class.members.find { it.name == "notCompatibleWithConfigurationCache" }
?.call(this, "The gradle-versions-plugin isn't compatible with the configuration cache")
}
}