Skip to content

Commit

Permalink
Merge pull request #3188 from bishabosha/support-more-deps-scala-cli
Browse files Browse the repository at this point in the history
support all scala-cli dependency directives
  • Loading branch information
fthomas authored Nov 29, 2023
2 parents 3b049d6 + 1e69d10 commit 1a9c3c1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 42 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Thanks goes to these wonderful people for contributing to Scala Steward:
* [Ikenna Darlington Ogbajie](https://github.com/idarlington)
* [Ingar Abrahamsen](https://github.com/ingarabr)
* [Jakub Kozłowski](https://github.com/kubukoz)
* [Jamie Thompson](https://github.com/bishabosha)
* [Javier Arrieta](https://github.com/javierarrieta)
* [JCollier](https://github.com/Slakah)
* [jduval87](https://github.com/jduval87)
Expand All @@ -127,7 +128,7 @@ Thanks goes to these wonderful people for contributing to Scala Steward:
* [Leonhard Riedißer](https://github.com/L7R7)
* [Maksym Ochenashko](https://github.com/iRevive)
* [Manuel Cueto](https://github.com/manuelcueto)
* [Marco Zühlke](https://github.com/mzuehlke)
* [Marco Zühlke](https://github.com/mzuehlke)
* [Mark Canlas](https://github.com/mcanlas)
* [Mark van der Tol](https://github.com/markvandertol)
* [MaT1g3R](https://github.com/MaT1g3R)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
import org.scalasteward.core.util.Nel
import org.typelevel.log4cats.Logger

object ScalaCliAlg {
val directives =
// sourced from https://github.com/VirtusLab/scala-cli/blob/9e22d4a91ba8699ac2727d2ac3042d64abe951e1/modules/directives/src/main/scala/scala/build/preprocessing/directives/Dependency.scala#L33-L48
List(
"lib",
"libs",
"dep",
"deps",
"dependencies",
"test.dependency",
"test.dep",
"test.deps",
"test.dependencies",
"compileOnly.lib",
"compileOnly.libs",
"compileOnly.dep",
"compileOnly.deps",
"compileOnly.dependencies"
).map(alias => s"//> using $alias ")
}

final class ScalaCliAlg[F[_]](implicit
fileAlg: FileAlg[F],
gitAlg: GitAlg[F],
Expand All @@ -42,8 +63,8 @@ final class ScalaCliAlg[F[_]](implicit
override def containsBuild(buildRoot: BuildRoot): F[Boolean] = {
val buildRootPath = buildRoot.relativePath.dropWhile(Set('.', '/'))
val extensions = Set(".sc", ".scala")
gitAlg
.findFilesContaining(buildRoot.repo, "//> using lib ")
ScalaCliAlg.directives
.flatTraverse(gitAlg.findFilesContaining(buildRoot.repo, _))
.map(_.exists(path => path.startsWith(buildRootPath) && extensions.exists(path.endsWith)))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log}
import org.scalasteward.core.repoconfig.{BuildRootConfig, RepoConfig}
import org.scalasteward.core.scalafmt
import org.scalasteward.core.scalafmt.scalafmtConfName
import org.scalasteward.core.buildtool.scalacli.ScalaCliAlg

class BuildToolDispatcherTest extends FunSuite {
test("getDependencies") {
Expand All @@ -28,37 +29,33 @@ class BuildToolDispatcherTest extends FunSuite {
val (state, deps) =
buildToolDispatcher.getDependencies(repo, repoConfig).runSA(initial).unsafeRunSync()

val allGreps = ScalaCliAlg.directives.map { search =>
Cmd.git(
repoDir,
"grep",
"-I",
"--fixed-strings",
"--files-with-matches",
search
)
}

val expectedState = initial.copy(trace =
Vector(
Cmd("test", "-f", s"$repoDir/pom.xml"),
Cmd("test", "-f", s"$repoDir/build.sc"),
Cmd("test", "-f", s"$repoDir/build.sbt"),
Cmd.git(
repoDir,
"grep",
"-I",
"--fixed-strings",
"--files-with-matches",
"//> using lib "
),
Cmd("test", "-f", s"$repoDir/mvn-build/pom.xml"),
Cmd("test", "-f", s"$repoDir/mvn-build/build.sc"),
Cmd("test", "-f", s"$repoDir/mvn-build/build.sbt"),
Cmd.git(
repoDir,
"grep",
"-I",
"--fixed-strings",
"--files-with-matches",
"//> using lib "
),
Log("Get dependencies in . from sbt"),
Cmd("read", s"$repoDir/project/build.properties"),
Cmd("test", "-d", s"$repoDir/project"),
Cmd("test", "-d", s"$repoDir/project/project"),
Cmd("read", "classpath:StewardPlugin_1_0_0.scala"),
Cmd("write", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala"),
Cmd("write", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala"),
Cmd("test", "-f", s"$repoDir/pom.xml") +:
Cmd("test", "-f", s"$repoDir/build.sc") +:
Cmd("test", "-f", s"$repoDir/build.sbt") +:
allGreps ++:
Cmd("test", "-f", s"$repoDir/mvn-build/pom.xml") +:
Cmd("test", "-f", s"$repoDir/mvn-build/build.sc") +:
Cmd("test", "-f", s"$repoDir/mvn-build/build.sbt") +:
allGreps ++:
Log("Get dependencies in . from sbt") +:
Cmd("read", s"$repoDir/project/build.properties") +:
Cmd("test", "-d", s"$repoDir/project") +:
Cmd("test", "-d", s"$repoDir/project/project") +:
Cmd("read", "classpath:StewardPlugin_1_0_0.scala") +:
Cmd("write", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala") +:
Cmd("write", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala") +:
Cmd.execSandboxed(
repoDir,
"sbt",
Expand All @@ -67,26 +64,26 @@ class BuildToolDispatcherTest extends FunSuite {
"-Dsbt.supershell=false",
"-Dsbt.server.forcestart=true",
s";$crossStewardDependencies;$reloadPlugins;$stewardDependencies"
),
Cmd("rm", "-rf", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala"),
Cmd("rm", "-rf", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala"),
Cmd("read", s"$repoDir/$scalafmtConfName"),
Log("Get dependencies in mvn-build from Maven"),
) +:
Cmd("rm", "-rf", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala") +:
Cmd("rm", "-rf", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala") +:
Cmd("read", s"$repoDir/$scalafmtConfName") +:
Log("Get dependencies in mvn-build from Maven") +:
Cmd.execSandboxed(
repoDir / "mvn-build",
"mvn",
maven.args.batchMode,
maven.command.listDependencies,
maven.args.excludeTransitive
),
) +:
Cmd.execSandboxed(
repoDir / "mvn-build",
"mvn",
maven.args.batchMode,
maven.command.listRepositories
),
Cmd("read", s"$repoDir/mvn-build/$scalafmtConfName")
)
) +:
Cmd("read", s"$repoDir/mvn-build/$scalafmtConfName") +:
Vector.empty[MockState.TraceEntry]
)

assertEquals(state, expectedState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import org.scalasteward.core.mock.MockState
import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log}
import org.scalasteward.core.util.Nel

import cats.syntax.parallel._

class ScalaCliAlgTest extends CatsEffectSuite {
test("containsBuild: directive in non-source file") {
val repo = Repo("user", "repo")
Expand All @@ -24,6 +26,25 @@ class ScalaCliAlgTest extends CatsEffectSuite {
assertIO(obtained, false)
}

test("containsBuild: directive with test.dep, dep, and lib") {
val repo = Repo("user", "repo")
val buildRoot = BuildRoot(repo, ".")
val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
val fileWithUsingDirective = "project.scala"

ScalaCliAlg.directives
.map { search =>
val grepCmd =
Cmd.git(repoDir, "grep", "-I", "--fixed-strings", "--files-with-matches", search)
val initial =
MockState.empty.copy(commandOutputs = Map(grepCmd -> Right(List(fileWithUsingDirective))))
val obtained = scalaCliAlg.containsBuild(buildRoot).runA(initial)
assertIO(obtained, true)
}
.parSequence
.void
}

test("getDependencies") {
val repo = Repo("user", "repo")
val buildRoot = BuildRoot(repo, ".")
Expand Down

0 comments on commit 1a9c3c1

Please sign in to comment.