Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow disabling version.refs generation #114

Open
FyiurAmron opened this issue May 30, 2023 · 2 comments
Open

allow disabling version.refs generation #114

FyiurAmron opened this issue May 30, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@FyiurAmron
Copy link

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.

@FyiurAmron FyiurAmron added the enhancement New feature or request label May 30, 2023
@bddckr
Copy link

bddckr commented May 17, 2024

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:

retainCurrentVersionReferences(versions, libraries, plugins)

@hvisser
Copy link
Contributor

hvisser commented May 20, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants