-
Notifications
You must be signed in to change notification settings - Fork 30
/
version-catalog.gradle
286 lines (260 loc) · 10.6 KB
/
version-catalog.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
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
282
283
284
285
286
import org.tomlj.Toml
import org.tomlj.TomlTable
import org.tomlj.TomlArray
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.tomlj:tomlj:1.0.0'
}
}
def dependenciesYamlFile = new File("${rootProject.projectDir}/dependencies.yml")
if (dependenciesYamlFile.exists()) {
throw new IllegalStateException(
"'dependencies.yml' file is no longer supported. Please use 'dependencies.toml' instead.")
}
def dependenciesTomlFile = new File("${rootProject.projectDir}/dependencies.toml")
if (!dependenciesTomlFile.exists()) {
throw new IllegalStateException(
"'dependencies.toml' file is not found. Please create one at the root of the project.")
}
def dependenciesToml = Collections.unmodifiableMap(Toml.parse(dependenciesTomlFile.text).toMap())
def metadata = [:]
def boms = []
dependencyResolutionManagement {
versionCatalogs {
create("libs") { libs ->
dependenciesToml.forEach { String key, value ->
def map = value.toMap()
switch (key) {
case "versions":
map.forEach { alias, version -> addVersion(libs, alias, version) }
break
case "libraries":
map.forEach { alias, dependency -> addLibrary(libs, alias, dependency, metadata) }
break
case "bundles":
map.forEach { alias, modules -> addBundle(libs, alias, modules) }
break
case "plugins":
map.forEach { alias, plugin -> addPlugin(libs, alias, plugin) }
break
case "boms":
map.forEach { alias, dependency -> addBom(libs, alias, dependency, boms) }
break
default:
throw new IllegalStateException(
"Unknown version catalog: '$key'. (expected: one of 'versions', " +
"'libraries', 'bundles', 'plungins' and 'boms')")
}
}
}
}
}
gradle.projectsLoaded {
def exclusions = [:].withDefault { [] }
def relocations = []
def javadocLinks = []
metadata.forEach { alias, map ->
def rel = map.get("relocations")
if (rel != null) {
relocations.addAll(rel)
}
def ex = map.get("exclusions")
if (ex != null) {
ex.each {
exclusions[it["name"]].add(ex["exclusion"])
}
}
def javadocs = map.get("javadocs")
if (javadocs != null) {
javadocLinks.addAll(javadocs)
}
}
gradle.allprojects { p ->
p.ext.dependenciesTomlBoms = boms
p.ext.exclusions = exclusions
p.ext.relocations = relocations
p.ext.javadocLinks = javadocLinks
}
}
static def addVersion(VersionCatalogBuilder libs, String alias, Object version) {
if (version instanceof String) {
libs.version(alias, version)
} else if (version instanceof TomlTable) {
libs.version(alias) { setRichVersion(it, version) }
} else {
throw new IllegalStateException("An invalid vesion declaration on ${alias}: ${version} (expected: string or objects compatible with MutableVersionConstraint)");
}
}
private static String setRichVersion(MutableVersionConstraint constraint, TomlTable version) {
if (version.contains("require")) {
constraint.require(version["require"])
}
if (version.contains("strictly")) {
constraint.strictly(version["strictly"])
}
if (version.contains("prefer")) {
constraint.prefer(version["prefer"])
}
if (version.contains("reject")) {
def rejected = version["reject"]
if (rejected instanceof TomlArray) {
constraint.reject(rejected.toList() as String[])
} else {
constraint.reject(rejected)
}
}
if (version["rejectAll"]) {
constraint.rejectAll()
}
}
static def addBom(VersionCatalogBuilder libs, String alias, TomlTable dependency, List<String> boms) {
def version = dependency["version"]
alias = "boms-" + alias
if (version instanceof String) {
def library = newLibrary(libs, alias, dependency)["lib"]
library.version(version)
boms.add(alias)
return
} else if (version instanceof TomlTable) {
if (version.contains("ref")) {
def library = newLibrary(libs, alias, dependency)["lib"]
library.versionRef(version["ref"])
boms.add(alias)
return
}
}
throw new IllegalStateException("An invalid BOM version on ${alias}: ${dependency.toJson()}" +
" (expected: 'version' or 'version.ref')")
}
static def addLibrary(VersionCatalogBuilder libs, String alias, TomlTable dependency,
Map<String, Map> dependenciesMetadata) {
def result = newLibrary(libs, alias, dependency)
def library = result.lib
def version = dependency["version"]
if (version == null) {
library.withoutVersion()
} else if (version instanceof String) {
library.version(version)
} else if (version instanceof TomlTable) {
if (version.contains("ref")) {
library.versionRef(version["ref"])
} else {
library.version { setRichVersion(it, version) }
}
}
def metadata = extractMetadata(dependency, result.group, result.artifact, alias)
if (!metadata.isEmpty()) {
dependenciesMetadata[alias] = metadata
}
}
static def newLibrary(VersionCatalogBuilder libs, String alias, TomlTable dependency) {
if (dependency.contains("group") && dependency.contains("name")) {
def group = dependency["group"]
def artifact = dependency["name"]
return [lib: libs.library(alias, group, artifact), group: group, artifact: artifact]
} else if (dependency.contains("module")) {
def module = dependency["module"].split(":")
if (module.length != 2) {
throw new IllegalStateException("An invalid module declaration on ${alias}: ${module} (expected: 'groupId:artifactId')");
}
def group = module[0]
def artifact = module[1]
return [lib: libs.library(alias, group, artifact), group: group, artifact: artifact]
} else {
throw new IllegalStateException("An invalid library declaration on ${alias}: ${dependency.toJson()}" +
" (expected: 'module' or 'group' and 'name')")
}
}
static Map<String, Object> extractMetadata(TomlTable dependency, String group, String artifact, String alias) {
def metadata = [:]
def javadocs = dependency["javadocs"]
if (javadocs != null) {
if (javadocs instanceof String) {
metadata["javadocs"] = [[groupId: group, artifactId: artifact, url: javadocs]]
} else if (javadocs instanceof TomlArray) {
metadata["javadocs"] = javadocs.toList().collect {
[groupId: group, artifactId: artifact, url: it]
}
} else {
throw new IllegalStateException("An invalid javadoc links on ${alias}: $javadocs'" +
" (expected: A string or an array of strings)")
}
}
def module = "$group:$artifact"
def exclusions = dependency["exclusions"]
if (exclusions != null) {
if (exclusions instanceof String) {
def excluded = exclusions.split(":")
metadata["exclusions"] = [[name: module, exclusion: [group: excluded[0], module: excluded[1]]]]
} else if (exclusions instanceof TomlArray) {
metadata["exclusions"] = exclusions.toList().collect {
def excluded = it.split(":")
[name: module, exclusion: [group: excluded[0], module: excluded[1]]]
}
} else {
throw new IllegalStateException("An invalid exclusions on ${alias}: $exclusions" +
" (expected: A string or an array of strings)")
}
}
def relocations = dependency["relocations"]
if (relocations != null) {
if (relocations instanceof TomlTable) {
metadata["relocations"] = [[name: module, from: relocations["from"], to: relocations["to"]]]
} else if (relocations instanceof TomlArray) {
metadata["relocations"] = relocations.toList().collect {
[name: module, from: it["from"], to: it["to"]]
}
} else {
throw new IllegalStateException("An invalid relocations on ${alias}: $relocations" +
" (expected: A map or an array of maps)")
}
}
metadata
}
static def addBundle(VersionCatalogBuilder libs, String alias, List<String> aliases) {
libs.bundle(alias, aliases)
}
static def addPlugin(VersionCatalogBuilder libs, String alias, TomlTable plugin) {
def version = plugin['version']
if (version instanceof TomlTable) {
def ref = version["ref"]
if (ref == null) {
throw new IllegalStateException("An invalid plugin declaration on $alias: ${version.toJson()}")
}
libs.plugin(alias, plugin["id"]).versionRef(ref)
} else {
libs.plugin(alias, plugin["id"]).version(plugin['version'])
}
}
gradle.projectsLoaded {
gradle.rootProject { p ->
// Prints all versions registered through 'dependencies.toml'
p.tasks.register("printVersionCatalogs") {
def catalogs = p.extensions.getByType(VersionCatalogsExtension)
doLast {
catalogs.catalogNames.forEach { name ->
def catalog = catalogs.named(name)
catalog.libraryAliases.forEach {
def library = catalog.findLibrary(it).get().get()
println "$name / library / $it / $library"
}
catalog.pluginAliases.forEach {
def plugin = catalog.findPlugin(it).get().get()
println "$name / plugin / $it / $plugin"
}
catalog.versionAliases.forEach {
def version = catalog.findVersion(it).get()
println "$name / version / $it / $version"
}
catalog.bundleAliases.forEach {
def bundle = catalog.findBundle(it).get().get()
println "$name / bundle / $it / $bundle"
}
}
}
}
}
}