allow disabling version.refs generation
FyiurAmron opened this issue · 2 comments
In some cases, the explicit versioning is easier to manage (e.g. with external tooling). It would be good to be able to disable the generation of version.refs and force explicit version strings in the generated entries instead.
I've run into this as an actual issue: Running the versionCatalogFormat
task resulted in a version for androidx-test
being added and used in the [libraries]
section. However, the current version of those three dependencies in my project being the same is due to a coincidence: Each dependency is actually released on its own, but right now they happen to have the same version.
Anyway, since this isn't configurable yet, I applied the following workaround:
Workaround
// https://github.com/littlerobots/version-catalog-update-plugin/issues/114
tasks.named<VersionCatalogFormatTask>("versionCatalogFormat") {
fun getFile() = catalogFile.asFile.get()
fun File.parse() = inputStream().use { VersionCatalogParser().parse(it) }
var original: VersionCatalog? = null
doFirst { original = getFile().parse() }
doLast {
val file = getFile()
val updated = file.parse()
val addedVersionKeys =
updated.versions.keys.minus(original!!.versions.keys)
.ifEmpty {
return@doLast
}
val fixed =
updated.copy(
libraries =
updated.libraries.mapValues { (_, library) ->
val version = library.version
if (
version is VersionDefinition.Reference &&
version.ref in addedVersionKeys
) {
library.copy(
version =
VersionDefinition.Simple(
(updated.versions.getValue(version.ref)
as VersionDefinition.Simple)
.version
)
)
} else {
library
}
},
plugins =
updated.plugins.mapValues { (_, plugin) ->
val version = plugin.version
if (
version is VersionDefinition.Reference &&
version.ref in addedVersionKeys
) {
plugin.copy(
version =
VersionDefinition.Simple(
(updated.versions.getValue(version.ref)
as VersionDefinition.Simple)
.version
)
)
} else {
plugin
}
},
versions = updated.versions.minus(addedVersionKeys)
)
file.writer().use { VersionCatalogWriter().write(fixed, it) }
}
}
A proper upstream change to allow this would probably be to make execution of this line conditional based on some configuration:
I'm still not really eager to make this configurable as it adds to the complexity of the plugin (config, testing etc). The generation of version refs is there for convenience but it needs to use a heuristic to do the grouping, since there's no way to know for sure how dependencies are released. I've also observed that most folks want a version.ref
, even for dependencies that don't strictly need it. The plugin wil retain those version groups (the line you are pointing to).
In this particular case you could do two things:
- Let the version be pointing to the "wrong" version ref; the plugin will "ungroup" that entry when an update happens and it will warn you if you try to update the ref and the version doesn't exist for one or more entries in that group.
- Add your own
version.ref
for that specific entry. The plugin will keep that version ref for future updates.
Though if you feel very strong about making this configurable, I'm happy to take a PR for it. Please add tests too in that case :) I currently don't have the bandwidth to work on it.