-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract dependencies from Gradle Version Catalogs
This adds partial support for Gradle builds that use a [version catalog](https://docs.gradle.org/current/userguide/version_catalogs.html) (i.e. a `gradle/libs.versions.toml` file). Dependencies are extracted from the version catalog just by parsing the `libs.versions.toml` file. Since the version catalog only contains libraries and no resolvers, the default resolver is used for the `Scope` of these libraries. This is one reason why this Gradle support is only partial. The other is that additional dependencies and plugins that are defined in other Gradle build files are also ignored. Closes: #3534
- Loading branch information
Showing
13 changed files
with
315 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
52 changes: 52 additions & 0 deletions
52
modules/core/src/main/scala/org/scalasteward/core/buildtool/gradle/GradleAlg.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2018-2025 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.buildtool.gradle | ||
|
||
import better.files.File | ||
import cats.Monad | ||
import cats.syntax.all.* | ||
import org.scalasteward.core.buildtool.{BuildRoot, BuildToolAlg} | ||
import org.scalasteward.core.data.Scope.Dependencies | ||
import org.scalasteward.core.data.{Resolver, Scope} | ||
import org.scalasteward.core.io.{FileAlg, WorkspaceAlg} | ||
import org.typelevel.log4cats.Logger | ||
|
||
final class GradleAlg[F[_]](defaultResolver: Resolver)(implicit | ||
fileAlg: FileAlg[F], | ||
override protected val logger: Logger[F], | ||
workspaceAlg: WorkspaceAlg[F], | ||
F: Monad[F] | ||
) extends BuildToolAlg[F] { | ||
override def name: String = "Gradle" | ||
|
||
override def containsBuild(buildRoot: BuildRoot): F[Boolean] = | ||
libsVersionsToml(buildRoot).flatMap(fileAlg.isRegularFile) | ||
|
||
override def getDependencies(buildRoot: BuildRoot): F[List[Dependencies]] = | ||
libsVersionsToml(buildRoot) | ||
.flatMap(fileAlg.readFile) | ||
.map(_.getOrElse("")) | ||
.map(gradleParser.parseDependenciesAndPlugins) | ||
.map { case (dependencies, plugins) => | ||
val ds = Option.when(dependencies.nonEmpty)(Scope(dependencies, List(defaultResolver))) | ||
val ps = Option.when(plugins.nonEmpty)(Scope(plugins, List(pluginsResolver))) | ||
ds.toList ++ ps.toList | ||
} | ||
|
||
private def libsVersionsToml(buildRoot: BuildRoot): F[File] = | ||
workspaceAlg.buildRootDir(buildRoot).map(_ / "gradle" / libsVersionsTomlName) | ||
} |
88 changes: 88 additions & 0 deletions
88
modules/core/src/main/scala/org/scalasteward/core/buildtool/gradle/gradleParser.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2018-2025 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.buildtool.gradle | ||
|
||
import cats.implicits.* | ||
import org.scalasteward.core.data.{ArtifactId, Dependency, GroupId, Module, Version} | ||
import org.tomlj.{Toml, TomlTable} | ||
import scala.jdk.CollectionConverters.* | ||
|
||
object gradleParser { | ||
def parseDependenciesAndPlugins(input: String): (List[Dependency], List[Dependency]) = { | ||
val parsed = Toml.parse(input) | ||
val versionsTable = getTableSafe(parsed, "versions") | ||
val librariesTable = getTableSafe(parsed, "libraries") | ||
val pluginsTable = getTableSafe(parsed, "plugins") | ||
|
||
val dependencies = collectEntries(librariesTable, parseDependency(_, versionsTable)) | ||
val plugins = collectEntries(pluginsTable, parsePlugin(_, versionsTable)) | ||
|
||
(dependencies, plugins) | ||
} | ||
|
||
private def collectEntries[A: Ordering](table: TomlTable, f: TomlTable => Option[A]): List[A] = { | ||
val aSet = table.entrySet().asScala.map(_.getValue).flatMap { | ||
case t: TomlTable => f(t) | ||
case _ => None | ||
} | ||
aSet.toList.sorted | ||
} | ||
|
||
private def parseDependency(lib: TomlTable, versions: TomlTable): Option[Dependency] = | ||
for { | ||
case (groupId, artifactId) <- parseModuleObj(lib).orElse(parseModuleString(lib)) | ||
version <- parseVersion(lib, versions) | ||
} yield Dependency(groupId, artifactId, version) | ||
|
||
private def parseModuleObj(lib: TomlTable): Option[(GroupId, ArtifactId)] = | ||
for { | ||
groupId <- getStringSafe(lib, "group").map(GroupId(_)) | ||
artifactId <- getStringSafe(lib, "name").map(ArtifactId(_)) | ||
} yield (groupId, artifactId) | ||
|
||
private def parseModuleString(lib: TomlTable): Option[(GroupId, ArtifactId)] = | ||
getStringSafe(lib, "module").flatMap { | ||
_.split(':') match { | ||
case Array(g, a) => Some((GroupId(g), ArtifactId(a))) | ||
case _ => None | ||
} | ||
} | ||
|
||
private def parsePlugin(plugin: TomlTable, versions: TomlTable): Option[Dependency] = | ||
for { | ||
id <- getStringSafe(plugin, "id") | ||
groupId = GroupId(id) | ||
artifactId = ArtifactId(s"$id.gradle.plugin") | ||
version <- parseVersion(plugin, versions) | ||
} yield Dependency(groupId, artifactId, version) | ||
|
||
private def parseVersion(table: TomlTable, versions: TomlTable): Option[Version] = { | ||
def versionString = getStringSafe(table, "version") | ||
def versionRef = getStringSafe(table, "version.ref").flatMap(getStringSafe(versions, _)) | ||
versionString.orElse(versionRef).map(Version.apply) | ||
} | ||
|
||
private def getTableSafe(table: TomlTable, key: String): TomlTable = | ||
Option | ||
.when(table.contains(key) && table.isTable(key))(table.getTableOrEmpty(key)) | ||
.getOrElse(emptyTable) | ||
|
||
private val emptyTable: TomlTable = Toml.parse("") | ||
|
||
private def getStringSafe(table: TomlTable, key: String): Option[String] = | ||
Option.when(table.contains(key) && table.isString(key))(table.getString(key)) | ||
} |
26 changes: 26 additions & 0 deletions
26
modules/core/src/main/scala/org/scalasteward/core/buildtool/gradle/package.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2018-2025 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.buildtool | ||
|
||
import org.scalasteward.core.data.Resolver | ||
|
||
package object gradle { | ||
val libsVersionsTomlName = "libs.versions.toml" | ||
|
||
val pluginsResolver: Resolver.MavenRepository = | ||
Resolver.MavenRepository("gradle-plugins", "https://plugins.gradle.org/m2/", None, None) | ||
} |
This file contains 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
This file contains 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
33 changes: 33 additions & 0 deletions
33
modules/core/src/test/scala/org/scalasteward/core/buildtool/gradle/GradleAlgTest.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.scalasteward.core.buildtool.gradle | ||
|
||
import munit.CatsEffectSuite | ||
import org.scalasteward.core.TestSyntax.* | ||
import org.scalasteward.core.buildtool.BuildRoot | ||
import org.scalasteward.core.data.{Repo, Scope} | ||
import org.scalasteward.core.mock.MockContext.context.* | ||
import org.scalasteward.core.mock.{MockEffOps, MockState} | ||
|
||
class GradleAlgTest extends CatsEffectSuite { | ||
test("getDependencies") { | ||
val repo = Repo("gradle-alg", "test-getDependencies") | ||
val buildRoot = BuildRoot(repo, ".") | ||
val buildRootDir = workspaceAlg.buildRootDir(buildRoot).unsafeRunSync() | ||
|
||
val initial = MockState.empty.addFiles( | ||
buildRootDir / "gradle" / libsVersionsTomlName -> | ||
"""|[libraries] | ||
|tomlj = { group = "org.tomlj", name = "tomlj", version = "1.1.1" } | ||
|[plugins] | ||
|kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version = "2.1.20-Beta1" } | ||
|""".stripMargin | ||
) | ||
val obtained = initial.flatMap(gradleAlg.getDependencies(buildRoot).runA) | ||
val kotlinJvm = | ||
"org.jetbrains.kotlin.jvm".g % "org.jetbrains.kotlin.jvm.gradle.plugin".a % "2.1.20-Beta1" | ||
val expected = List( | ||
List("org.tomlj".g % "tomlj".a % "1.1.1").withMavenCentral, | ||
Scope(List(kotlinJvm), List(pluginsResolver)) | ||
) | ||
assertIO(obtained, expected) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
modules/core/src/test/scala/org/scalasteward/core/buildtool/gradle/gradleParserTest.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.scalasteward.core.buildtool.gradle | ||
|
||
import munit.FunSuite | ||
import org.scalasteward.core.TestSyntax.* | ||
|
||
class gradleParserTest extends FunSuite { | ||
test("parseDependenciesAndPlugins: valid input") { | ||
val input = | ||
"""|[versions] | ||
|groovy = "3.0.5" | ||
|checkstyle = "8.37" | ||
| | ||
|[libraries] | ||
|groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" } | ||
|groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" } | ||
|groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" } | ||
|commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = { strictly = "[3.8, 4.0[", prefer="3.9" } } | ||
|tomlj = { group = "org.tomlj", name = "tomlj", version = "1.1.1" } | ||
| | ||
|[bundles] | ||
|groovy = ["groovy-core", "groovy-json", "groovy-nio"] | ||
| | ||
|[plugins] | ||
|versions = { id = "com.github.ben-manes.versions", version = "0.45.0" } | ||
|""".stripMargin | ||
val obtained = gradleParser.parseDependenciesAndPlugins(input) | ||
val expected = ( | ||
List( | ||
"org.codehaus.groovy".g % "groovy".a % "3.0.5", | ||
"org.codehaus.groovy".g % "groovy-json".a % "3.0.5", | ||
"org.codehaus.groovy".g % "groovy-nio".a % "3.0.5", | ||
"org.tomlj".g % "tomlj".a % "1.1.1" | ||
), | ||
List( | ||
"com.github.ben-manes.versions".g % "com.github.ben-manes.versions.gradle.plugin".a % "0.45.0" | ||
) | ||
) | ||
assertEquals(obtained, expected) | ||
} | ||
|
||
test("parseDependenciesAndPlugins: empty input") { | ||
val obtained = gradleParser.parseDependenciesAndPlugins("") | ||
assertEquals(obtained, (List.empty, List.empty)) | ||
} | ||
|
||
test("parseDependenciesAndPlugins: malformed input") { | ||
val input = | ||
"""|versions] | ||
|groovy = "3.0.5" | ||
|[libraries] | ||
|groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" | ||
|foo = { module = "bar:qux:foo", version = "1" } | ||
|[plugins] | ||
|foo = "" | ||
|""".stripMargin | ||
val obtained = gradleParser.parseDependenciesAndPlugins(input) | ||
assertEquals(obtained, (List.empty, List.empty)) | ||
} | ||
} |
Oops, something went wrong.