diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/SbtAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/SbtAlg.scala index 7c857aff1e..6b58f697e9 100644 --- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/SbtAlg.scala +++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/SbtAlg.scala @@ -87,14 +87,18 @@ object SbtAlg { override def runMigrations(repo: Repo, migrations: Nel[Migration]): F[Unit] = addGlobalPluginTemporarily(scalaStewardScalafixSbt) { - for { - repoDir <- workspaceAlg.repoDir(repo) - scalafixCmds = for { - migration <- migrations - rule <- migration.rewriteRules - } yield s"$scalafixAll $rule" - _ <- exec(sbtCmd(scalafixEnable :: scalafixCmds.toList), repoDir) - } yield () + workspaceAlg.repoDir(repo).flatMap { repoDir => + migrations.traverse_ { migration => + val withScalacOptions = + migration.scalacOptions.fold[F[Unit] => F[Unit]](identity) { opts => + val file = scalaStewardScalafixOptions(opts.toList) + fileAlg.createTemporarily(repoDir / file.name, file.content)(_) + } + + val scalafixCmds = migration.rewriteRules.map(rule => s"$scalafixAll $rule").toList + withScalacOptions(exec(sbtCmd(scalafixEnable :: scalafixCmds), repoDir).void) + } + } } val sbtDir: F[File] = diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/package.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/package.scala index 6b3df03e22..f43798a601 100644 --- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/package.scala +++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/package.scala @@ -28,16 +28,9 @@ package object sbt { org.scalasteward.core.BuildInfo.scalaBinaryVersion def sbtDependency(sbtVersion: SbtVersion): Option[Dependency] = - if (sbtVersion.toVersion >= Version("1.0.0")) - Some( - Dependency( - GroupId("org.scala-sbt"), - ArtifactId("sbt"), - sbtVersion.value - ) - ) - else - None + Option.when(sbtVersion.toVersion >= Version("1.0.0")) { + Dependency(GroupId("org.scala-sbt"), ArtifactId("sbt"), sbtVersion.value) + } val scalaStewardScalafixSbt: FileData = FileData( @@ -45,6 +38,11 @@ package object sbt { """addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.19")""" ) + def scalaStewardScalafixOptions(scalacOptions: List[String]): FileData = { + val args = scalacOptions.map(s => s""""$s"""").mkString(", ") + FileData("scala-steward-scalafix-options.sbt", s"ThisBuild / scalacOptions ++= List($args)") + } + def stewardPlugin[F[_]](implicit fileAlg: FileAlg[F], F: Functor[F]): F[FileData] = { val name = "StewardPlugin.scala" fileAlg diff --git a/modules/core/src/main/scala/org/scalasteward/core/scalafix/Migration.scala b/modules/core/src/main/scala/org/scalasteward/core/scalafix/Migration.scala index d396bf26f1..f906452781 100644 --- a/modules/core/src/main/scala/org/scalasteward/core/scalafix/Migration.scala +++ b/modules/core/src/main/scala/org/scalasteward/core/scalafix/Migration.scala @@ -26,7 +26,8 @@ final case class Migration( artifactIds: Nel[String], newVersion: Version, rewriteRules: Nel[String], - doc: Option[String] + doc: Option[String], + scalacOptions: Option[Nel[String]] ) object Migration { diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/SbtAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/SbtAlgTest.scala index 619fb49fc5..ee150e9af2 100644 --- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/SbtAlgTest.scala +++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/SbtAlgTest.scala @@ -63,6 +63,7 @@ class SbtAlgTest extends AnyFunSuite with Matchers { Nel.of("fs2-core"), Version("1.0.0"), Nel.of("github:functional-streams-for-scala/fs2/v1?sha=v1.0.5"), + None, None ) ) @@ -88,4 +89,42 @@ class SbtAlgTest extends AnyFunSuite with Matchers { ) ) } + + test("runMigrations: migration with scalacOptions") { + val repo = Repo("fthomas", "scala-steward") + val repoDir = config.workspace / repo.show + val migrations = Nel.of( + Migration( + GroupId("org.typelevel"), + Nel.of("cats-core"), + Version("2.2.0"), + Nel.of("github:cb372/cats/Cats_v2_2_0?sha=235bd7c92e431ab1902db174cf4665b05e08f2f1"), + None, + Some(Nel.of("-P:semanticdb:synthetics:on")) + ) + ) + val state = sbtAlg.runMigrations(repo, migrations).runS(MockState.empty).unsafeRunSync() + + state shouldBe MockState.empty.copy( + commands = Vector( + List("write", "/tmp/steward/.sbt/0.13/plugins/scala-steward-scalafix.sbt"), + List("write", "/tmp/steward/.sbt/1.0/plugins/scala-steward-scalafix.sbt"), + List("write", s"$repoDir/scala-steward-scalafix-options.sbt"), + List( + "TEST_VAR=GREAT", + "ANOTHER_TEST_VAR=ALSO_GREAT", + repoDir.toString, + "firejail", + s"--whitelist=$repoDir", + "sbt", + "-batch", + "-no-colors", + s";$scalafixEnable;$scalafixAll github:cb372/cats/Cats_v2_2_0?sha=235bd7c92e431ab1902db174cf4665b05e08f2f1" + ), + List("rm", "-rf", s"$repoDir/scala-steward-scalafix-options.sbt"), + List("rm", "-rf", "/tmp/steward/.sbt/1.0/plugins/scala-steward-scalafix.sbt"), + List("rm", "-rf", "/tmp/steward/.sbt/0.13/plugins/scala-steward-scalafix.sbt") + ) + ) + } } diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/sbtTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/sbtTest.scala new file mode 100644 index 0000000000..d11ffbf2f2 --- /dev/null +++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/sbtTest.scala @@ -0,0 +1,11 @@ +package org.scalasteward.core.buildtool.sbt + +import org.scalatest.funsuite.AnyFunSuite +import org.scalatest.matchers.should.Matchers + +class sbtTest extends AnyFunSuite with Matchers { + test("scalaStewardScalafixOptions") { + scalaStewardScalafixOptions(List("-P:semanticdb:synthetics:on")).content shouldBe + """ThisBuild / scalacOptions ++= List("-P:semanticdb:synthetics:on")""" + } +} diff --git a/modules/core/src/test/scala/org/scalasteward/core/scalafix/MigrationAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/scalafix/MigrationAlgTest.scala index 6f48f292be..d31db699c0 100644 --- a/modules/core/src/test/scala/org/scalasteward/core/scalafix/MigrationAlgTest.scala +++ b/modules/core/src/test/scala/org/scalasteward/core/scalafix/MigrationAlgTest.scala @@ -34,7 +34,8 @@ class MigrationAlgTest extends AnyFunSuite with Matchers { Nel.of("yumyum-.*"), Version("1.0.0"), Nel.of("awesome rewrite rule"), - Some("https://scalacenter.github.io/scalafix/") + Some("https://scalacenter.github.io/scalafix/"), + None ) ) ) @@ -61,6 +62,7 @@ class MigrationAlgTest extends AnyFunSuite with Matchers { Nel.of("yumyum-.*"), Version("1.0.0"), Nel.of("awesome rewrite rule"), + None, None ) ) @@ -87,6 +89,6 @@ class MigrationAlgTest extends AnyFunSuite with Matchers { test("loadMigrations without extra file") { val migrations = MigrationAlg.loadMigrations[MockEff](None).runA(MockState.empty).unsafeRunSync() - migrations.size shouldBe 14 + migrations.size should be > 1 } } diff --git a/modules/core/src/test/scala/org/scalasteward/core/vcs/data/NewPullRequestDataTest.scala b/modules/core/src/test/scala/org/scalasteward/core/vcs/data/NewPullRequestDataTest.scala index 779cd80db1..77f2284209 100644 --- a/modules/core/src/test/scala/org/scalasteward/core/vcs/data/NewPullRequestDataTest.scala +++ b/modules/core/src/test/scala/org/scalasteward/core/vcs/data/NewPullRequestDataTest.scala @@ -99,6 +99,7 @@ class NewPullRequestDataTest extends AnyFunSuite with Matchers { Nel.of(update.artifactId.name), Version("0.7.0"), Nel.of("I am a rewrite rule"), + None, None ) val (label, appliedMigrations) = NewPullRequestData.migrationNote(List(migration)) @@ -120,7 +121,8 @@ class NewPullRequestDataTest extends AnyFunSuite with Matchers { Nel.of(update.artifactId.name), Version("0.7.0"), Nel.of("I am a rewrite rule"), - Some("https://scalacenter.github.io/scalafix/") + Some("https://scalacenter.github.io/scalafix/"), + None ) val (label, appliedMigrations) = NewPullRequestData.migrationNote(List(migration))