Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support all scala-cli dependency directives #3188

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
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"//> $alias ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't using missing here?

Suggested change
).map(alias => s"//> $alias ")
).map(alias => s"//> using $alias ")

Copy link
Contributor Author

@bishabosha bishabosha Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh good point!
Edit: added a new commit

}

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, _))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there is probably a more efficient way to do this, e.g. using one command, but I didn't spend too long looking into this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, git grep (which is used by findFilesContaining) can search for multiple patterns in one go by combining them via --or.

I just measured the wall clock time of containsBuild before and with this change for this repo and it went from 7 to 82 milliseconds. IMO this is acceptable since it is still dwarfed by actually running any build tool to extract dependencies.

.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,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