Skip to content

Commit

Permalink
Allow setting compiler options for Scalafix migrations (#1584)
Browse files Browse the repository at this point in the history
* Allow setting compiler options for Scalafix migrations

In anticipation to typelevel/cats#3566

* Use Option#fold and change order of fields

* Test migration with scalacOptions

* Use Option.when in sbtDependency
  • Loading branch information
fthomas authored Sep 1, 2020
1 parent 66d078c commit fb29255
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,21 @@ 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(
"scala-steward-scalafix.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
Expand All @@ -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")
)
)
}
}
Original file line number Diff line number Diff line change
@@ -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")"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
)
Expand All @@ -61,6 +62,7 @@ class MigrationAlgTest extends AnyFunSuite with Matchers {
Nel.of("yumyum-.*"),
Version("1.0.0"),
Nel.of("awesome rewrite rule"),
None,
None
)
)
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))

Expand Down

0 comments on commit fb29255

Please sign in to comment.