-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
202 additions
and
1 deletion.
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
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
/* | ||
* 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.parseDependencies) | ||
.map(ds => List(Scope(ds, List(defaultResolver)))) | ||
|
||
private def libsVersionsToml(buildRoot: BuildRoot): F[File] = | ||
workspaceAlg.buildRootDir(buildRoot).map(_ / "gradle" / "libs.versions.toml") | ||
} |
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
/* | ||
* 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 org.scalasteward.core.data.{ArtifactId, Dependency, GroupId, Version} | ||
import org.tomlj.{Toml, TomlTable} | ||
import scala.jdk.CollectionConverters.* | ||
|
||
object gradleParser { | ||
def parseDependencies(s: String): List[Dependency] = { | ||
val parsed = Toml.parse(s) | ||
val versions = parsed.getTable(key.versions) | ||
val libraries = parsed.getTable(key.libraries) | ||
libraries | ||
.entrySet() | ||
.asScala | ||
.map(_.getValue) | ||
.flatMap { | ||
case lib: TomlTable => parseDependency(lib, versions) | ||
case _ => None | ||
} | ||
.toList | ||
} | ||
|
||
private def parseDependency(lib: TomlTable, versions: TomlTable): Option[Dependency] = | ||
parseVersion(lib, versions).flatMap { version => | ||
if (lib.contains(key.module)) { | ||
val module = lib.getString(key.module) | ||
module.split(':') match { | ||
case Array(g, a) => | ||
val groupId = GroupId(g) | ||
val artifactId = ArtifactId(a) | ||
Some(Dependency(groupId, artifactId, version)) | ||
case _ => None | ||
} | ||
} else if (lib.contains(key.group)) { | ||
val groupId = GroupId(lib.getString(key.group)) | ||
val artifactId = ArtifactId(lib.getString(key.name)) | ||
Some(Dependency(groupId, artifactId, version)) | ||
} 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) | ||
Option.when(versions.isString(ref))(Version(versions.getString(ref))) | ||
} else if (lib.isString(key.version)) | ||
Some(Version(lib.getString(key.version))) | ||
else | ||
None | ||
|
||
object key { | ||
val group = "group" | ||
val libraries = "libraries" | ||
val module = "module" | ||
val name = "name" | ||
val version = "version" | ||
val versions = "versions" | ||
val versionRef = "version.ref" | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
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 | ||
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" / "libs.versions.toml" -> | ||
"""|[libraries] | ||
|tomlj = { group = "org.tomlj", name = "tomlj", version = "1.1.1" } | ||
|""".stripMargin | ||
) | ||
val obtained = initial.flatMap(gradleAlg.getDependencies(buildRoot).runA) | ||
val expected = List(List("org.tomlj".g % "tomlj".a % "1.1.1").withMavenCentral) | ||
assertIO(obtained, expected) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
package org.scalasteward.core.buildtool.gradle | ||
|
||
import munit.FunSuite | ||
|
||
class gradleParserTest extends FunSuite { | ||
test("parseDependencies") { | ||
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 | ||
gradleParser.parseDependencies(input) | ||
} | ||
} |
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