rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Support for changing Gradle dependency versions in TOML files

Open michaelmcfadyensky opened this issue 1 year ago • 3 comments

What problem are you trying to solve?

We specify all of our dependencies and their versions inside toml files for our gradle projects. We would like a away to automate the upgrading of these versions via open rewrite.

Describe the solution you'd like

Recipe available for changing the version of a dependency specified in a toml file

Have you considered any alternatives or workarounds?

Consider using the find and replace recipe but it leads to several work arounds having to be in place for it to work.

Additional context

https://rewriteoss.slack.com/archives/C01A843MWG5/p1723123348552829

michaelmcfadyensky avatar Aug 08 '24 14:08 michaelmcfadyensky

Thanks for logging it as an issue! As indicated we do have a toml parser, but that'd need to be dusted off and cleaned up a bit before putting that to use. Likely to tie in with the recently added

  • https://github.com/openrewrite/rewrite/pull/4394

timtebeek avatar Aug 08 '24 15:08 timtebeek

@steplica thanks a lot for the offer to help!

  1. I think in the early stages of getting the parser to work @KnutWannheden might be best to guide you, as he's been most active with recent parser developments. I'd expect rewrite-toml should still be part of openrewrite/rewrite, with an eye towards integration with Gradle
  2. once the parser works we'd need to hook it into the rewrite-maven-plugin and rewrite-gradle-plugin, and rewrite-polyglot
  3. then once files are parsed as Toml, we'd need to integrate with our Gradle support; at that point we can tag Shannon Pamperl (also here) for guidance, likely looking towards our Trait support to minimize recipe changes

timtebeek avatar Sep 24 '24 10:09 timtebeek

will this also support gradle version catalogs defined in settings.gradle (no toml files involved) ?

tompson avatar Oct 18 '24 07:10 tompson

👍 sounds good. I will keep an eye on this issue.

jizhangattw avatar Oct 29 '24 07:10 jizhangattw

  • One step closer after https://github.com/openrewrite/rewrite/pull/4845

timtebeek avatar Jan 03 '25 21:01 timtebeek

Next steps:

  • [ ] Update gradle.UpgradeDependencyVersion to handle dependencies from the Gradle Version Catalog libraries table with the version appearing in a String literal [1]
  • [ ] Update gradle.UpgradeDependencyVersion to handle dependencies from the Gradle Version Catalog libraries table with the version appearing within the defined inline table [2]
  • [ ] Update gradle.UpgradeDependencyVersion to handle dependencies from the Gradle Version Catalog libraries table with a version reference being used [3]
  • [ ] Update gradle.plugins.UpgradePluginVersion to handle plugins from the Gradle Version Catalog plugins table with the version appearing in a String literal [4]
  • [ ] Update gradle.plugins.UpgradePluginVersion to handle plugins from the Gradle Version Catalog plugins table with the version appearing within the defined inline table [5]
  • [ ] Update gradle.plugins.UpgradePluginVersion to handle plugins from the Gradle Version Catalog plugins table with a version reference being used [6]

[1] build.gradle

dependencies {
    implementation(libs.guava)
}

gradle/libs.versions.toml

[libraries]
guava = "com.google.guava:guava:29.0-jre"

[2] build.gradle

dependencies {
    implementation(libs.guava)
}

gradle/libs.versions.toml

[libraries]
guava = { group = "com.google.guava", name = "guava", version = "29.0-jre" }

[3] build.gradle

dependencies {
    implementation(libs.guava)
}

gradle/libs.versions.toml

[versions]
guava = "29.0-jre"

[libraries]
guava = { group = "com.google.guava", name = "guava", version.ref = "guava" }

[4] build.gradle

plugins {
    alias(libs.plugins.openrewrite)
}

gradle/libs.versions.toml

[plugins]
openrewrite = "org.openrewrite.rewrite:5.40.0"

[5] build.gradle

plugins {
    alias(libs.plugins.openrewrite)
}

gradle/libs.versions.toml

[plugins]
openrewrite = { id = "org.openrewrite.rewrite", version = "5.40.0" }

[6] build.gradle

plugins {
    alias(libs.plugins.openrewrite)
}

gradle/libs.versions.toml

[versions]
openrewrite = "5.40.0"

[plugins]
openrewrite = { id = "org.openrewrite.rewrite", version.ref = "openrewrite" }

Additional notes:

With both of these recipes, versions found in .properties files are updated utilizing a separate visitor. The same should be expected for the .toml file as well. In this case, however, the .toml file has a lot more information to go off of given the GAV or plugin ID are directly available within the file itself meaning that the SCAN phase should not be strictly necessary like it is with the .properties variant.

Links:

  • https://docs.gradle.org/current/userguide/version_catalogs.html

shanman190 avatar Jan 06 '25 18:01 shanman190

@tompson, I went ahead and broke out your request to a separate issue as it'll be a little bit different than updating the TOML files themselves.

  • https://github.com/openrewrite/rewrite/issues/4852

shanman190 avatar Jan 06 '25 20:01 shanman190