Skip to content

Commit 1a9c3c1

Browse files
authored
Merge pull request #3188 from bishabosha/support-more-deps-scala-cli
support all scala-cli dependency directives
2 parents 3b049d6 + 1e69d10 commit 1a9c3c1

File tree

4 files changed

+82
-42
lines changed

4 files changed

+82
-42
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Thanks goes to these wonderful people for contributing to Scala Steward:
112112
* [Ikenna Darlington Ogbajie](https://github.com/idarlington)
113113
* [Ingar Abrahamsen](https://github.com/ingarabr)
114114
* [Jakub Kozłowski](https://github.com/kubukoz)
115+
* [Jamie Thompson](https://github.com/bishabosha)
115116
* [Javier Arrieta](https://github.com/javierarrieta)
116117
* [JCollier](https://github.com/Slakah)
117118
* [jduval87](https://github.com/jduval87)
@@ -127,7 +128,7 @@ Thanks goes to these wonderful people for contributing to Scala Steward:
127128
* [Leonhard Riedißer](https://github.com/L7R7)
128129
* [Maksym Ochenashko](https://github.com/iRevive)
129130
* [Manuel Cueto](https://github.com/manuelcueto)
130-
* [Marco Zühlke](https://github.com/mzuehlke)
131+
* [Marco Zühlke](https://github.com/mzuehlke)
131132
* [Mark Canlas](https://github.com/mcanlas)
132133
* [Mark van der Tol](https://github.com/markvandertol)
133134
* [MaT1g3R](https://github.com/MaT1g3R)

modules/core/src/main/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlg.scala

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
2828
import org.scalasteward.core.util.Nel
2929
import org.typelevel.log4cats.Logger
3030

31+
object ScalaCliAlg {
32+
val directives =
33+
// sourced from https://github.com/VirtusLab/scala-cli/blob/9e22d4a91ba8699ac2727d2ac3042d64abe951e1/modules/directives/src/main/scala/scala/build/preprocessing/directives/Dependency.scala#L33-L48
34+
List(
35+
"lib",
36+
"libs",
37+
"dep",
38+
"deps",
39+
"dependencies",
40+
"test.dependency",
41+
"test.dep",
42+
"test.deps",
43+
"test.dependencies",
44+
"compileOnly.lib",
45+
"compileOnly.libs",
46+
"compileOnly.dep",
47+
"compileOnly.deps",
48+
"compileOnly.dependencies"
49+
).map(alias => s"//> using $alias ")
50+
}
51+
3152
final class ScalaCliAlg[F[_]](implicit
3253
fileAlg: FileAlg[F],
3354
gitAlg: GitAlg[F],
@@ -42,8 +63,8 @@ final class ScalaCliAlg[F[_]](implicit
4263
override def containsBuild(buildRoot: BuildRoot): F[Boolean] = {
4364
val buildRootPath = buildRoot.relativePath.dropWhile(Set('.', '/'))
4465
val extensions = Set(".sc", ".scala")
45-
gitAlg
46-
.findFilesContaining(buildRoot.repo, "//> using lib ")
66+
ScalaCliAlg.directives
67+
.flatTraverse(gitAlg.findFilesContaining(buildRoot.repo, _))
4768
.map(_.exists(path => path.startsWith(buildRootPath) && extensions.exists(path.endsWith)))
4869
}
4970

modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log}
1010
import org.scalasteward.core.repoconfig.{BuildRootConfig, RepoConfig}
1111
import org.scalasteward.core.scalafmt
1212
import org.scalasteward.core.scalafmt.scalafmtConfName
13+
import org.scalasteward.core.buildtool.scalacli.ScalaCliAlg
1314

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

32+
val allGreps = ScalaCliAlg.directives.map { search =>
33+
Cmd.git(
34+
repoDir,
35+
"grep",
36+
"-I",
37+
"--fixed-strings",
38+
"--files-with-matches",
39+
search
40+
)
41+
}
42+
3143
val expectedState = initial.copy(trace =
32-
Vector(
33-
Cmd("test", "-f", s"$repoDir/pom.xml"),
34-
Cmd("test", "-f", s"$repoDir/build.sc"),
35-
Cmd("test", "-f", s"$repoDir/build.sbt"),
36-
Cmd.git(
37-
repoDir,
38-
"grep",
39-
"-I",
40-
"--fixed-strings",
41-
"--files-with-matches",
42-
"//> using lib "
43-
),
44-
Cmd("test", "-f", s"$repoDir/mvn-build/pom.xml"),
45-
Cmd("test", "-f", s"$repoDir/mvn-build/build.sc"),
46-
Cmd("test", "-f", s"$repoDir/mvn-build/build.sbt"),
47-
Cmd.git(
48-
repoDir,
49-
"grep",
50-
"-I",
51-
"--fixed-strings",
52-
"--files-with-matches",
53-
"//> using lib "
54-
),
55-
Log("Get dependencies in . from sbt"),
56-
Cmd("read", s"$repoDir/project/build.properties"),
57-
Cmd("test", "-d", s"$repoDir/project"),
58-
Cmd("test", "-d", s"$repoDir/project/project"),
59-
Cmd("read", "classpath:StewardPlugin_1_0_0.scala"),
60-
Cmd("write", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala"),
61-
Cmd("write", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala"),
44+
Cmd("test", "-f", s"$repoDir/pom.xml") +:
45+
Cmd("test", "-f", s"$repoDir/build.sc") +:
46+
Cmd("test", "-f", s"$repoDir/build.sbt") +:
47+
allGreps ++:
48+
Cmd("test", "-f", s"$repoDir/mvn-build/pom.xml") +:
49+
Cmd("test", "-f", s"$repoDir/mvn-build/build.sc") +:
50+
Cmd("test", "-f", s"$repoDir/mvn-build/build.sbt") +:
51+
allGreps ++:
52+
Log("Get dependencies in . from sbt") +:
53+
Cmd("read", s"$repoDir/project/build.properties") +:
54+
Cmd("test", "-d", s"$repoDir/project") +:
55+
Cmd("test", "-d", s"$repoDir/project/project") +:
56+
Cmd("read", "classpath:StewardPlugin_1_0_0.scala") +:
57+
Cmd("write", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala") +:
58+
Cmd("write", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala") +:
6259
Cmd.execSandboxed(
6360
repoDir,
6461
"sbt",
@@ -67,26 +64,26 @@ class BuildToolDispatcherTest extends FunSuite {
6764
"-Dsbt.supershell=false",
6865
"-Dsbt.server.forcestart=true",
6966
s";$crossStewardDependencies;$reloadPlugins;$stewardDependencies"
70-
),
71-
Cmd("rm", "-rf", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala"),
72-
Cmd("rm", "-rf", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala"),
73-
Cmd("read", s"$repoDir/$scalafmtConfName"),
74-
Log("Get dependencies in mvn-build from Maven"),
67+
) +:
68+
Cmd("rm", "-rf", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala") +:
69+
Cmd("rm", "-rf", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala") +:
70+
Cmd("read", s"$repoDir/$scalafmtConfName") +:
71+
Log("Get dependencies in mvn-build from Maven") +:
7572
Cmd.execSandboxed(
7673
repoDir / "mvn-build",
7774
"mvn",
7875
maven.args.batchMode,
7976
maven.command.listDependencies,
8077
maven.args.excludeTransitive
81-
),
78+
) +:
8279
Cmd.execSandboxed(
8380
repoDir / "mvn-build",
8481
"mvn",
8582
maven.args.batchMode,
8683
maven.command.listRepositories
87-
),
88-
Cmd("read", s"$repoDir/mvn-build/$scalafmtConfName")
89-
)
84+
) +:
85+
Cmd("read", s"$repoDir/mvn-build/$scalafmtConfName") +:
86+
Vector.empty[MockState.TraceEntry]
9087
)
9188

9289
assertEquals(state, expectedState)

modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import org.scalasteward.core.mock.MockState
1010
import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log}
1111
import org.scalasteward.core.util.Nel
1212

13+
import cats.syntax.parallel._
14+
1315
class ScalaCliAlgTest extends CatsEffectSuite {
1416
test("containsBuild: directive in non-source file") {
1517
val repo = Repo("user", "repo")
@@ -24,6 +26,25 @@ class ScalaCliAlgTest extends CatsEffectSuite {
2426
assertIO(obtained, false)
2527
}
2628

29+
test("containsBuild: directive with test.dep, dep, and lib") {
30+
val repo = Repo("user", "repo")
31+
val buildRoot = BuildRoot(repo, ".")
32+
val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
33+
val fileWithUsingDirective = "project.scala"
34+
35+
ScalaCliAlg.directives
36+
.map { search =>
37+
val grepCmd =
38+
Cmd.git(repoDir, "grep", "-I", "--fixed-strings", "--files-with-matches", search)
39+
val initial =
40+
MockState.empty.copy(commandOutputs = Map(grepCmd -> Right(List(fileWithUsingDirective))))
41+
val obtained = scalaCliAlg.containsBuild(buildRoot).runA(initial)
42+
assertIO(obtained, true)
43+
}
44+
.parSequence
45+
.void
46+
}
47+
2748
test("getDependencies") {
2849
val repo = Repo("user", "repo")
2950
val buildRoot = BuildRoot(repo, ".")

0 commit comments

Comments
 (0)