-
Notifications
You must be signed in to change notification settings - Fork 30
/
settings-flags.gradle
195 lines (166 loc) · 6.37 KB
/
settings-flags.gradle
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
import org.gradle.util.GradleVersion
import java.util.concurrent.ConcurrentHashMap
import java.util.regex.Pattern
import static java.util.Objects.requireNonNull
// A version catalog should be defined in the settings.gradle or directly imported files.
// https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog-declaration
apply from: "${rootDir}/gradle/scripts/version-catalog.gradle"
// Ensure the Gradle version first of all.
GradleVersion minimumSupportedGradleVersion = GradleVersion.version('6.0')
if (GradleVersion.current() < minimumSupportedGradleVersion) {
throw new IllegalStateException("${minimumSupportedGradleVersion} or above is required to build this project.")
}
// Add the 'includeWithFlags' directive to 'settings'.
ext.includeWithFlags = this.&includeWithFlags
def includeWithFlags(CharSequence path, ...flags) {
requireNonNull(path, "path is null.")
if (path.length() == 0) {
throw new IllegalArgumentException("path is empty.")
}
def pathStr = path.toString()
def actualFlags = addFlags(new LinkedHashSet(), flags)
settings.include pathStr
// Ensure all projects has the extension methods and properties.
if (!gradle.ext.has('installedFlagsExtension')) {
gradle.ext.installedFlagsExtension = true
gradle.projectsLoaded {
gradle.allprojects { p ->
p.ext.flags = Collections.emptySet()
p.ext.hasFlag = p.ext.hasFlags = this.&hasFlags.curry(p)
p.ext.assertFlag = p.ext.assertFlags = this.&assertFlags.curry(p)
p.ext.projectsWithFlag = p.ext.projectsWithFlags = this.&projectsWithFlags.curry(p)
p.ext.afterProjectsWithFlag = this.&afterProjectsWithFlag.curry(p)
p.ext.afterProjectsWithFlags = this.&afterProjectsWithFlags.curry(p)
p.ext.addFlag = p.ext.addFlags = { args -> p.ext.flags = addFlags(p.ext.flags, args) }
}
}
}
// Set the flags property.
gradle.projectsLoaded {
try {
gradle.rootProject.project(pathStr).ext.flags = actualFlags
} catch (UnknownProjectException ignored) {
// Ignore
}
}
}
static def addFlags(Set<String> actualFlags, ...flags) {
if (actualFlags == null) {
actualFlags = new LinkedHashSet<>()
} else {
actualFlags = new LinkedHashSet<>(actualFlags)
}
if (flags != null) {
if (flags instanceof CharSequence) {
actualFlags.add(flags.toString())
} else if (flags instanceof Iterable) {
flags.each { it ->
if (it instanceof CharSequence) {
actualFlags.add(it.toString())
} else {
throw new IllegalArgumentException("flags must contain only CharSequences: ${flags}")
}
}
} else if (flags instanceof Object[]) {
flags.each { it ->
if (it instanceof CharSequence) {
actualFlags.add(it.toString())
} else {
throw new IllegalArgumentException("flags must contain only CharSequences: ${flags}")
}
}
} else {
throw new IllegalArgumentException(
"flags must be a CharSequence or an Iterable of CharSequences: ${flags}")
}
}
// 'trim' implies 'shade'
if (actualFlags.contains('trim')) {
actualFlags.add('shade')
}
// 'shade' implies 'relocate'.
if (actualFlags.contains('shade')) {
actualFlags.add('relocate')
}
// 'bom' implies 'publish'.
if (actualFlags.contains('bom')) {
actualFlags.add('publish')
}
// 'version-catalog' implies 'publish'.
if (actualFlags.contains('version-catalog')) {
actualFlags.add('publish')
}
// 'java(\\d+)' implies 'java'
if (!actualFlags.contains('java')) {
def pattern = Pattern.compile('^java(\\d+)$')
for (String flag: actualFlags) {
if (pattern.matcher(flag).matches()) {
actualFlags.add('java')
break
}
}
}
// 'kotlin(\\d+\\.\\+d)' implies 'kotlin'
if (!actualFlags.contains('kotlin')) {
def pattern = Pattern.compile('^kotlin(\\d+\\.\\d+)$')
for (String flag: actualFlags) {
if (pattern.matcher(flag).matches()) {
actualFlags.add('kotlin')
break
}
}
}
// 'kotlin' implies 'java'
if (actualFlags.contains('kotlin')) {
actualFlags.add('java')
}
return Collections.unmodifiableSet(actualFlags)
}
static def hasFlags(Project project, ...flags) {
if (!project.ext.has('flags')) {
return flags.length == 0
}
Set<String> actualFlags = project.ext.flags
for (def f in flags.flatten()) {
if (!(f instanceof CharSequence)) {
throw new IllegalArgumentException("not a CharSequence: ${f}")
}
if (!actualFlags.contains(f.toString())) {
return false
}
}
return true
}
static def assertFlags(Project project, ...flags) {
assert hasFlags(project, flags)
}
static def projectsWithFlags(Project project, ...flags) {
return new LinkedHashSet<Project>(
project.rootProject.allprojects.findAll {
it.ext.has('hasFlags') && it.ext.hasFlags(flags)
})
}
static void afterProjectsWithFlag(Project project, Object flag, Closure<Set<Project>> closure) {
afterProjectsWithFlags(project, [flag], closure)
}
static void afterProjectsWithFlags(Project project, Iterable<?> flags, Closure<Set<Project>> closure) {
def projects = projectsWithFlags(project, flags)
def remainingProjects = Collections.newSetFromMap(new ConcurrentHashMap<Project, Boolean>())
remainingProjects.addAll(projects)
projects.each { p ->
p.afterEvaluate {
remainingProjects.remove(p)
if (remainingProjects.isEmpty()) {
closure(projects)
}
}
}
}
// We add a "virtual project", with no source code, to control dependency management using Gradle's platform
// functionality.
def virtualProjectsPath = "${rootDir}/build/virtual-projects"
includeWithFlags ':dependencyManagement', 'dependencyManagement'
project(":dependencyManagement").with {
projectDir = file("${virtualProjectsPath}/dependencyManagement")
projectDir.mkdirs()
}