diff --git a/README.md b/README.md index 21e21f53b3..23c545e9a9 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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) diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlg.scala index ca572170f5..6e2e52b51f 100644 --- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlg.scala +++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlg.scala @@ -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], @@ -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))) } diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala index 897d852dc2..2a691ecc81 100644 --- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala +++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala @@ -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") { @@ -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", @@ -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) diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala index 4841481f71..e70768ca99 100644 --- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala +++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala @@ -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") @@ -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, ".")