From d1f7677f3176683c641a6dc28978b6f26ab80b21 Mon Sep 17 00:00:00 2001 From: Frank Thomas Date: Fri, 24 Jan 2025 15:51:27 +0100 Subject: [PATCH] Support Scalafix migrations in Scala CLI builds --- .../core/buildtool/scalacli/ScalaCliAlg.scala | 11 ++++++++-- .../buildtool/scalacli/ScalaCliAlgTest.scala | 20 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) 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 fc706d526a..71ffee1712 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 @@ -21,6 +21,7 @@ import cats.syntax.all.* import org.scalasteward.core.buildtool.sbt.SbtAlg import org.scalasteward.core.buildtool.{BuildRoot, BuildToolAlg} import org.scalasteward.core.data.Scope +import org.scalasteward.core.edit.scalafix.ScalafixMigration import org.scalasteward.core.git.GitAlg import org.scalasteward.core.io.process.SlurpOptions import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg} @@ -91,6 +92,12 @@ final class ScalaCliAlg[F[_]](implicit _ <- fileAlg.deleteForce(buildRootDir / exportDir) } yield dependencies - override protected val scalafixIssue: Option[String] = - Some("https://github.com/scala-steward-org/scala-steward/issues/3486") + override def runMigration(buildRoot: BuildRoot, migration: ScalafixMigration): F[Unit] = + for { + buildRootDir <- workspaceAlg.buildRootDir(buildRoot) + cmd = Nel.of("scala-cli", "--power", "fix", "--scalafix-rules") ::: + migration.rewriteRules.append(buildRootDir.pathAsString) + slurpOptions = SlurpOptions.ignoreBufferOverflow + _ <- processAlg.execSandboxed(cmd, buildRootDir, slurpOptions = slurpOptions) + } yield () } 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 6419c86a12..0ad9aff9c3 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 @@ -7,7 +7,7 @@ import org.scalasteward.core.buildtool.sbt.command.* import org.scalasteward.core.data.{GroupId, Repo, Version} import org.scalasteward.core.edit.scalafix.ScalafixMigration import org.scalasteward.core.mock.MockContext.context.* -import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log} +import org.scalasteward.core.mock.MockState.TraceEntry.Cmd import org.scalasteward.core.mock.{MockEffOps, MockState} import org.scalasteward.core.util.Nel @@ -84,8 +84,9 @@ class ScalaCliAlgTest extends CatsEffectSuite { } test("runMigration") { - val repo = Repo("user", "repo") + val repo = Repo("scala-cli-alg", "test-runMigration") val buildRoot = BuildRoot(repo, ".") + val buildRootDir = workspaceAlg.buildRootDir(buildRoot).unsafeRunSync() val migration = ScalafixMigration( GroupId("co.fs2"), Nel.of("fs2-core"), @@ -94,6 +95,19 @@ class ScalaCliAlgTest extends CatsEffectSuite { signoffCommits = None ) val obtained = scalaCliAlg.runMigration(buildRoot, migration).runS(MockState.empty) - assertIO(obtained.map(_.trace.collect { case Log(_) => () }.size), 1) + val expected = MockState.empty.copy(trace = + Vector( + Cmd.execSandboxed( + buildRootDir, + "scala-cli", + "--power", + "fix", + "--scalafix-rules", + "github:functional-streams-for-scala/fs2/v1?sha=v1.0.5", + buildRootDir.pathAsString + ) + ) + ) + assertIO(obtained, expected) } }