/
DefaultDependencyFilter.groovy
128 lines (111 loc) · 4.18 KB
/
DefaultDependencyFilter.groovy
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
package com.github.jengelman.gradle.plugins.shadow.internal
import groovy.util.logging.Slf4j
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.file.FileCollection
import org.gradle.api.specs.Spec
import org.gradle.api.specs.Specs
@Slf4j
class DefaultDependencyFilter implements DependencyFilter {
private final Project project
private final List<Spec<? super ResolvedDependency>> includeSpecs = []
private final List<Spec<? super ResolvedDependency>> excludeSpecs = []
DefaultDependencyFilter(Project project) {
assert project
this.project = project
}
FileCollection resolve(Configuration configuration) {
Set<ResolvedDependency> includedDeps = []
Set<ResolvedDependency> excludedDeps = []
resolve(configuration.resolvedConfiguration.firstLevelModuleDependencies, includedDeps, excludedDeps)
return project.files(configuration.files) - project.files(excludedDeps.collect {
it.moduleArtifacts*.file
}.flatten())
}
FileCollection resolve(Collection<Configuration> configurations) {
configurations.collect {
resolve(it)
}.sum() as FileCollection ?: project.files()
}
/**
* Exclude dependencies that match the provided spec.
*
* @param spec
* @return
*/
DependencyFilter exclude(Spec<? super ResolvedDependency> spec) {
excludeSpecs << spec
return this
}
/**
* Include dependencies that match the provided spec.
*
* @param spec
* @return
*/
DependencyFilter include(Spec<? super ResolvedDependency> spec) {
includeSpecs << spec
return this
}
/**
* Create a spec that matches the provided project notation on group, name, and version
* @param notation
* @return
*/
Spec<? super ResolvedDependency> project(Map<String, ?> notation) {
dependency(project.dependencies.project(notation))
}
/**
* Create a spec that matches the default configuration for the provided project path on group, name, and version
*
* @param notation
* @return
*/
Spec<? super ResolvedDependency> project(String notation) {
dependency(project.dependencies.project(path: notation, configuration: 'default'))
}
/**
* Create a spec that matches dependencies using the provided notation on group, name, and version
* @param notation
* @return
*/
Spec<? super ResolvedDependency> dependency(Object notation) {
dependency(project.dependencies.create(notation))
}
/**
* Create a spec that matches the provided dependency on group, name, and version
* @param dependency
* @return
*/
Spec<? super ResolvedDependency> dependency(Dependency dependency) {
this.dependency({ ResolvedDependency it ->
(!dependency.group || it.moduleGroup.matches(dependency.group)) &&
(!dependency.name || it.moduleName.matches(dependency.name)) &&
(!dependency.version || it.moduleVersion.matches(dependency.version))
})
}
/**
* Create a spec that matches the provided closure
* @param spec
* @return
*/
Spec<? super ResolvedDependency> dependency(Closure spec) {
return Specs.<ResolvedDependency>convertClosureToSpec(spec)
}
protected void resolve(Set<ResolvedDependency> dependencies,
Set<ResolvedDependency> includedDependencies,
Set<ResolvedDependency> excludedDependencies) {
dependencies.each {
if (isIncluded(it) ? includedDependencies.add(it) : excludedDependencies.add(it)) {
resolve(it.children, includedDependencies, excludedDependencies)
}
}
}
protected boolean isIncluded(ResolvedDependency dependency) {
boolean include = includeSpecs.empty || includeSpecs.any { it.isSatisfiedBy(dependency) }
boolean exclude = !excludeSpecs.empty && excludeSpecs.any { it.isSatisfiedBy(dependency) }
return include && !exclude
}
}