From 554a3e27e79033eabd5705629a5cdeb7be70926f Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Wed, 18 Oct 2023 12:18:07 +0200 Subject: [PATCH 1/3] update scala cli directives for dependencies --- README.md | 3 ++- .../core/buildtool/scalacli/ScalaCliAlg.scala | 25 +++++++++++++++++-- .../buildtool/scalacli/ScalaCliAlgTest.scala | 21 ++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d665462f93..4b262914ec 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,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) * [Jeff Martin](https://github.com/custommonkey) @@ -125,7 +126,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..0976dbf9cb 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"//> $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/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, ".") From 62715a43400fc5550ad75b965572a23b94a8c056 Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Tue, 31 Oct 2023 16:51:07 +0100 Subject: [PATCH 2/3] fix BuildToolDispatcherTest --- .../buildtool/BuildToolDispatcherTest.scala | 75 +++++++++---------- 1 file changed, 36 insertions(+), 39 deletions(-) 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) From 1e69d10cbd8f9d4abaf521da44e4782e107e961d Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Tue, 28 Nov 2023 09:29:12 +0100 Subject: [PATCH 3/3] correct directive format --- .../org/scalasteward/core/buildtool/scalacli/ScalaCliAlg.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0976dbf9cb..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 @@ -46,7 +46,7 @@ object ScalaCliAlg { "compileOnly.dep", "compileOnly.deps", "compileOnly.dependencies" - ).map(alias => s"//> $alias ") + ).map(alias => s"//> using $alias ") } final class ScalaCliAlg[F[_]](implicit