|
| 1 | +/* |
| 2 | + * Copyright 2018-2025 Scala Steward contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.scalasteward.core.buildtool.gradle |
| 18 | + |
| 19 | +import cats.implicits.* |
| 20 | +import org.scalasteward.core.data.{ArtifactId, Dependency, GroupId, Module, Version} |
| 21 | +import org.tomlj.{Toml, TomlTable} |
| 22 | +import scala.jdk.CollectionConverters.* |
| 23 | + |
| 24 | +object gradleParser { |
| 25 | + def parseDependenciesAndPlugins(input: String): (List[Dependency], List[Dependency]) = { |
| 26 | + val parsed = Toml.parse(input) |
| 27 | + val versionsTable = getTableSafe(parsed, "versions") |
| 28 | + val librariesTable = getTableSafe(parsed, "libraries") |
| 29 | + val pluginsTable = getTableSafe(parsed, "plugins") |
| 30 | + |
| 31 | + val dependencies = collectEntries(librariesTable, parseDependency(_, versionsTable)) |
| 32 | + val plugins = collectEntries(pluginsTable, parsePlugin(_, versionsTable)) |
| 33 | + |
| 34 | + (dependencies, plugins) |
| 35 | + } |
| 36 | + |
| 37 | + private def collectEntries[A: Ordering](table: TomlTable, f: TomlTable => Option[A]): List[A] = { |
| 38 | + val aSet = table.entrySet().asScala.map(_.getValue).flatMap { |
| 39 | + case t: TomlTable => f(t) |
| 40 | + case _ => None |
| 41 | + } |
| 42 | + aSet.toList.sorted |
| 43 | + } |
| 44 | + |
| 45 | + private def parseDependency(lib: TomlTable, versions: TomlTable): Option[Dependency] = |
| 46 | + for { |
| 47 | + case (groupId, artifactId) <- parseModuleObj(lib).orElse(parseModuleString(lib)) |
| 48 | + version <- parseVersion(lib, versions) |
| 49 | + } yield Dependency(groupId, artifactId, version) |
| 50 | + |
| 51 | + private def parseModuleObj(lib: TomlTable): Option[(GroupId, ArtifactId)] = |
| 52 | + for { |
| 53 | + groupId <- getStringSafe(lib, "group").map(GroupId(_)) |
| 54 | + artifactId <- getStringSafe(lib, "name").map(ArtifactId(_)) |
| 55 | + } yield (groupId, artifactId) |
| 56 | + |
| 57 | + private def parseModuleString(lib: TomlTable): Option[(GroupId, ArtifactId)] = |
| 58 | + getStringSafe(lib, "module").flatMap { |
| 59 | + _.split(':') match { |
| 60 | + case Array(g, a) => Some((GroupId(g), ArtifactId(a))) |
| 61 | + case _ => None |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private def parsePlugin(plugin: TomlTable, versions: TomlTable): Option[Dependency] = |
| 66 | + for { |
| 67 | + id <- getStringSafe(plugin, "id") |
| 68 | + groupId = GroupId(id) |
| 69 | + artifactId = ArtifactId(s"$id.gradle.plugin") |
| 70 | + version <- parseVersion(plugin, versions) |
| 71 | + } yield Dependency(groupId, artifactId, version) |
| 72 | + |
| 73 | + private def parseVersion(table: TomlTable, versions: TomlTable): Option[Version] = { |
| 74 | + def versionString = getStringSafe(table, "version") |
| 75 | + def versionRef = getStringSafe(table, "version.ref").flatMap(getStringSafe(versions, _)) |
| 76 | + versionString.orElse(versionRef).map(Version.apply) |
| 77 | + } |
| 78 | + |
| 79 | + private def getTableSafe(table: TomlTable, key: String): TomlTable = |
| 80 | + Option |
| 81 | + .when(table.contains(key) && table.isTable(key))(table.getTableOrEmpty(key)) |
| 82 | + .getOrElse(emptyTable) |
| 83 | + |
| 84 | + private val emptyTable: TomlTable = Toml.parse("") |
| 85 | + |
| 86 | + private def getStringSafe(table: TomlTable, key: String): Option[String] = |
| 87 | + Option.when(table.contains(key) && table.isString(key))(table.getString(key)) |
| 88 | +} |
0 commit comments