Skip to content

Commit

Permalink
Also extract Gradle plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Jan 10, 2025
1 parent c9762e2 commit a95fb0c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ final class GradleAlg[F[_]](defaultResolver: Resolver)(implicit
libsVersionsToml(buildRoot)
.flatMap(fileAlg.readFile)
.map(_.getOrElse(""))
.map(gradleParser.parseDependencies)
.map(dependencies => List(Scope(dependencies, List(defaultResolver))))
.map(gradleParser.parseDependenciesAndPlugins)
.map { case (dependencies, plugins) =>
Option.when(dependencies.nonEmpty)(Scope(dependencies, List(defaultResolver))).toList ++
Option.when(plugins.nonEmpty)(Scope(plugins, List(pluginsResolver))).toList
}

private def libsVersionsToml(buildRoot: BuildRoot): F[File] =
workspaceAlg.buildRootDir(buildRoot).map(_ / "gradle" / "libs.versions.toml")

private val pluginsResolver =
Resolver.MavenRepository("gradle-plugins", "https://plugins.gradle.org/m2/", None, None)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,28 @@ import org.tomlj.{Toml, TomlTable}
import scala.jdk.CollectionConverters.*

object gradleParser {
def parseDependencies(s: String): List[Dependency] = {
def parseDependenciesAndPlugins(s: String): (List[Dependency], List[Dependency]) = {
val parsed = Toml.parse(s)
val versions = parsed.getTable(key.versions)
val libraries = parsed.getTable(key.libraries)
val dependencies = libraries.entrySet().asScala.map(_.getValue).flatMap {
case lib: TomlTable => parseDependency(lib, versions)
case _ => None
}
dependencies.toList.sortBy(d => (d.groupId.value, d.artifactId.name, d.version.value))
val versions = parsed.getTableOrEmpty(key.versions)
val librariesTable = parsed.getTableOrEmpty(key.libraries)
val dependencies = parseEntries(librariesTable, parseDependency(_, versions))
val pluginsTable = parsed.getTableOrEmpty(key.plugins)
val plugins = parseEntries(pluginsTable, parsePlugin(_, versions))
(dependencies, plugins)
}

private def parseEntries(table: TomlTable, f: TomlTable => Option[Dependency]): List[Dependency] =
table
.entrySet()
.asScala
.map(_.getValue)
.flatMap {
case t: TomlTable => f(t)
case _ => None
}
.toList
.sortBy(d => (d.groupId.value, d.artifactId.name, d.version.value))

private def parseDependency(lib: TomlTable, versions: TomlTable): Option[Dependency] =
parseVersion(lib, versions).flatMap { version =>
if (lib.contains(key.module)) {
Expand All @@ -50,20 +61,31 @@ object gradleParser {
} else None
}

private def parseVersion(lib: TomlTable, versions: TomlTable): Option[Version] =
if (lib.isTable(key.version) && lib.contains(key.versionRef)) {
val ref = lib.getString(key.versionRef)
private def parsePlugin(plugin: TomlTable, versions: TomlTable): Option[Dependency] =
parseVersion(plugin, versions).flatMap { version =>
Option.when(plugin.contains(key.id)) {
val groupId = GroupId(plugin.getString(key.id))
val artifactId = ArtifactId(groupId.value + ".gradle.plugin")
Dependency(groupId, artifactId, version)
}
}

private def parseVersion(table: TomlTable, versions: TomlTable): Option[Version] =
if (table.isTable(key.version) && table.contains(key.versionRef)) {
val ref = table.getString(key.versionRef)
Option.when(versions.isString(ref))(Version(versions.getString(ref)))
} else if (lib.isString(key.version))
Some(Version(lib.getString(key.version)))
} else if (table.isString(key.version))
Some(Version(table.getString(key.version)))
else
None

object key {
val group = "group"
val id = "id"
val libraries = "libraries"
val module = "module"
val name = "name"
val plugins = "plugins"
val version = "version"
val versions = "versions"
val versionRef = "version.ref"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ class gradleParserTest extends FunSuite {
|[plugins]
|versions = { id = "com.github.ben-manes.versions", version = "0.45.0" }
|""".stripMargin
val obtained = gradleParser.parseDependencies(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"
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)
}
Expand Down

0 comments on commit a95fb0c

Please sign in to comment.