littlerobots / version-catalog-update-plugin

Gradle plugin for updating a project version catalog
Apache License 2.0
565 stars 23 forks source link

allow disabling version.refs generation #114

Open FyiurAmron opened 1 year ago

FyiurAmron commented 1 year ago

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.

bddckr commented 6 months ago

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 ```kotlin // https://github.com/littlerobots/version-catalog-update-plugin/issues/114 tasks.named("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: https://github.com/littlerobots/version-catalog-update-plugin/blob/32f7d9b0d0de36d517b37f4005ca311a839dd6cf/catalog/src/main/kotlin/nl/littlerobots/vcu/model/VersionCatalog.kt#L126

hvisser commented 6 months ago

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:

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.