Skip to content

Commit

Permalink
Run syntactic build migrations with the Scalafix CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGregory084 committed Aug 2, 2023
1 parent 8f1e56c commit be3c89a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.{scala,sbt}]
indent_size = 2
indent_style = space
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.scalasteward.core.buildtool.sbt.command._
import org.scalasteward.core.buildtool.{BuildRoot, BuildToolAlg}
import org.scalasteward.core.coursier.VersionsCache
import org.scalasteward.core.data.{Dependency, Scope, Version}
import org.scalasteward.core.edit.scalafix.ScalafixMigration
import org.scalasteward.core.edit.scalafix.{ScalafixCli, ScalafixMigration}
import org.scalasteward.core.io.process.SlurpOptions
import org.scalasteward.core.io.{FileAlg, FileData, ProcessAlg, WorkspaceAlg}
import org.scalasteward.core.util.Nel
Expand All @@ -34,6 +34,7 @@ import org.scalasteward.core.buildtool.sbt.scalaStewardSbtScalafix
final class SbtAlg[F[_]](config: Config)(implicit
fileAlg: FileAlg[F],
processAlg: ProcessAlg[F],
scalafixCli: ScalafixCli[F],
workspaceAlg: WorkspaceAlg[F],
versionsCache: VersionsCache[F],
F: Concurrent[F]
Expand Down Expand Up @@ -116,9 +117,29 @@ final class SbtAlg[F[_]](config: Config)(implicit
for {
buildRootDir <- workspaceAlg.buildRootDir(buildRoot)
metaBuilds <- metaBuildsCount(buildRootDir)
_ <- runSyntacticBuildMigrations(buildRootDir, migration)
_ <- runSbtScalafix(buildRootDir, migration, metaBuilds, startDepth = 1)
} yield ()

private def runSyntacticBuildMigrations(
buildRootDir: File,
migration: ScalafixMigration
): F[Unit] = {
val rootSbtFiles =
fileAlg.walk(buildRootDir, 1).filter(_.extension.contains(".sbt"))

val metaBuildFiles =
fileAlg.walk(buildRootDir / project, 3).filter(_.extension.exists(Set(".sbt", ".scala")))

val allBuildFiles = (rootSbtFiles ++ metaBuildFiles).compile.toList

allBuildFiles.flatMap { buildFiles =>
Nel.fromList(buildFiles).fold(F.unit) { files =>
scalafixCli.runMigration(buildRootDir, files, migration)
}
}
}

private def latestSbtScalafixVersion: F[Option[Version]] =
versionsCache
.getVersions(Scope(sbtScalafixDependency, List(config.defaultResolver)), None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ScalafixCli[F[_]](implicit
) {
def runMigration(workingDir: File, files: Nel[File], migration: ScalafixMigration): F[Unit] = {
val rules = migration.rewriteRules.map("--rules=" + _)
val cmd = scalafixBinary :: rules ::: files.map(_.pathAsString)
val cmd = scalafixBinary :: "--syntactic" :: rules ::: files.map(_.pathAsString)
processAlg.exec(cmd, workingDir, slurpOptions = SlurpOptions.ignoreBufferOverflow).void
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ object FileAlg {
Resource.fromAutoCloseable(source).use(src => F.blocking(src.mkString))

override def walk(dir: File, maxDepth: Int): Stream[F, File] =
Stream.eval(F.delay(dir.walk(maxDepth))).flatMap(Stream.fromBlockingIterator(_, 1))
Stream.force(
F.blocking(dir.exists)
.ifF(
Stream.eval(F.delay(dir.walk(maxDepth))).flatMap(Stream.fromBlockingIterator(_, 1)),
Stream.empty.covary[F]
)
)

override def writeFile(file: File, content: String): F[Unit] =
logger.debug(s"Write $file") >>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.scalasteward.core.buildtool.BuildRoot
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.edit.scalafix.ScalafixCli._
import org.scalasteward.core.mock.MockContext.context._
import org.scalasteward.core.mock.MockState
import org.scalasteward.core.mock.MockState.TraceEntry.Cmd
Expand Down Expand Up @@ -138,13 +139,23 @@ class SbtAlgTest extends FunSuite {
)
val initialState = MockState.empty
.addFiles(
repoDir / "build.sbt" -> "",
workspace / s"store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json" -> sbtScalafixVersionJson
)
.unsafeRunSync()
val state = sbtAlg.runMigration(buildRoot, migration).runS(initialState).unsafeRunSync()
val expected = initialState.copy(
trace = Vector(
Cmd("test", "-d", s"$repoDir/project"),
Cmd(
"VAR1=val1",
"VAR2=val2",
repoDir.pathAsString,
scalafixBinary,
"--syntactic",
"--rules=github:typelevel/sbt-tpolecat/v0_5?sha=v0.5.0",
s"$repoDir/build.sbt"
),
Cmd(
"read",
s"$workspace/store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json"
Expand Down Expand Up @@ -178,7 +189,9 @@ class SbtAlgTest extends FunSuite {
)
val initialState = MockState.empty
.addFiles(
repoDir / "project" / "Dependencies.scala" -> "object Dependencies", // put anything at all into the meta-build
repoDir / "build.sbt" -> "",
repoDir / "project" / "plugins.sbt" -> "",
repoDir / "project" / "Dependencies.scala" -> "object Dependencies",
workspace / s"store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json" -> sbtScalafixVersionJson
)
.unsafeRunSync()
Expand All @@ -187,6 +200,17 @@ class SbtAlgTest extends FunSuite {
trace = Vector(
Cmd("test", "-d", s"$repoDir/project"),
Cmd("test", "-d", s"$repoDir/project/project"),
Cmd(
"VAR1=val1",
"VAR2=val2",
repoDir.pathAsString,
scalafixBinary,
"--syntactic",
"--rules=github:typelevel/sbt-tpolecat/v0_5?sha=v0.5.0",
s"$repoDir/build.sbt",
s"$repoDir/project/Dependencies.scala",
s"$repoDir/project/plugins.sbt"
),
Cmd(
"read",
s"$workspace/store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json"
Expand Down Expand Up @@ -224,13 +248,23 @@ class SbtAlgTest extends FunSuite {
)
val initialState = MockState.empty
.addFiles(
repoDir / "build.sbt" -> "",
workspace / s"store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json" -> sbtScalafixVersionJson
)
.unsafeRunSync()
val state = sbtAlg.runMigration(buildRoot, migration).runS(initialState).unsafeRunSync()
val expected = initialState.copy(
trace = Vector(
Cmd("test", "-d", s"$repoDir/project"),
Cmd(
"VAR1=val1",
"VAR2=val2",
repoDir.pathAsString,
scalafixBinary,
"--syntactic",
"--rules=github:typelevel/sbt-tpolecat/v0_5?sha=v0.5.0",
s"$repoDir/build.sbt"
),
Cmd(
"read",
s"$workspace/store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json"
Expand Down Expand Up @@ -267,7 +301,9 @@ class SbtAlgTest extends FunSuite {
)
val initialState = MockState.empty
.addFiles(
repoDir / "project" / "Dependencies.scala" -> "object Dependencies", // put anything at all into the meta-build
repoDir / "build.sbt" -> "",
repoDir / "project" / "plugins.sbt" -> "",
repoDir / "project" / "Dependencies.scala" -> "object Dependencies",
workspace / s"store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json" -> sbtScalafixVersionJson
)
.unsafeRunSync()
Expand All @@ -276,6 +312,17 @@ class SbtAlgTest extends FunSuite {
trace = Vector(
Cmd("test", "-d", s"$repoDir/project"),
Cmd("test", "-d", s"$repoDir/project/project"),
Cmd(
"VAR1=val1",
"VAR2=val2",
repoDir.pathAsString,
scalafixBinary,
"--syntactic",
"--rules=github:typelevel/sbt-tpolecat/v0_5?sha=v0.5.0",
s"$repoDir/build.sbt",
s"$repoDir/project/Dependencies.scala",
s"$repoDir/project/plugins.sbt"
),
Cmd(
"read",
s"$workspace/store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json"
Expand Down

0 comments on commit be3c89a

Please sign in to comment.