Last active
August 13, 2024 21:37
-
-
Save SIMULATAN/f07fe8c344d72a36a92f56caf8f72446 to your computer and use it in GitHub Desktop.
Print DSL-based Gradle Version Catalog as TOML - useful to migrate from kotlin-defined dependencies to a TOML-based version catalog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val versionLines = mutableListOf<String>() | |
val libs = mutableMapOf<String, List<String>>() | |
val plugs = mutableMapOf<String, List<String>>() | |
versionCatalogs.forEach { catalog -> | |
// map of version - alias | needed to guess the aliases used | |
val versions = catalog.versionAliases.associate { alias -> | |
val version = catalog.findVersion(alias) | |
version.get().displayName to alias.replace(".", "-") | |
} | |
versionLines += versions.map { (value, key) -> "$key = \"$value\"" }.joinToString("\n").trim('\n') | |
fun buildLibraries(): List<String> { | |
return catalog.libraryAliases.map { ogAlias -> | |
// toml version catalogs, despite the docs, don't support dots in keys | |
val alias = ogAlias.replace(".", "-") | |
val lib = catalog.findLibrary(alias).get().get() | |
val version = versions[lib.version]?.let { "version.ref = \"$it\"" } | |
?: "version = \"${lib.version}\"" | |
"$alias = { group = \"${lib.group}\", name = \"${lib.name}\", $version }" | |
} | |
} | |
fun buildPlugins(): List<String> { | |
return catalog.pluginAliases.map { ogAlias -> | |
val alias = ogAlias.replace(".", "-") | |
val lib = catalog.findPlugin(alias).get().get() | |
val version = versions[lib.version.toString()]?.let { "version.ref = \"$it\"" } | |
?: "version = \"${lib.version}\"" | |
"$alias = { id = \"${lib.pluginId}\", $version }" | |
} | |
} | |
libs[catalog.name] = buildLibraries() | |
plugs[catalog.name] = buildPlugins() | |
} | |
println("[versions]\n${versionLines.filter { it.isNotEmpty() }.joinToString("\n")}") | |
fun buildOutput(name: String, lines: Map<String, List<String>>, withKey: Boolean) = "[$name]\n${ | |
lines.mapNotNull { (key, lines) -> | |
if (lines.isEmpty()) return@mapNotNull null | |
else if (withKey) "${lines.joinToString("\n") { "$key-$it" }}\n" | |
else lines.joinToString("\n") | |
}.joinToString("\n") | |
}" | |
println("\n" + buildOutput("plugins", plugs, false)) | |
println("\n" + buildOutput("libraries", libs, true)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment