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

Run Scalafix build migrations using sbt #3133

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
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 added this because it drives me mad when VS Code uses 4 space indent by default and this configures all editors that understand it 😛

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.scalasteward.core.buildtool.sbt

import better.files.File
import cats.data.OptionT
import cats.data.{NonEmptyList, OptionT}
import cats.effect.{Concurrent, Resource}
import cats.syntax.all._
import org.scalasteward.core.application.Config
Expand All @@ -29,6 +29,7 @@ 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
import org.scalasteward.core.buildtool.sbt.scalaStewardSbtScalafix

final class SbtAlg[F[_]](config: Config)(implicit
fileAlg: FileAlg[F],
Expand Down Expand Up @@ -107,40 +108,101 @@ final class SbtAlg[F[_]](config: Config)(implicit
}

private def runSourcesMigration(buildRoot: BuildRoot, migration: ScalafixMigration): F[Unit] =
OptionT(latestSbtScalafixVersion).foreachF { pluginVersion =>
workspaceAlg.buildRootDir(buildRoot).flatMap { buildRootDir =>
val plugin = scalaStewardSbtScalafix(pluginVersion)
fileAlg.createTemporarily(buildRootDir / project, plugin).surround {
val withScalacOptions = migration.scalacOptions.fold(Resource.unit[F]) { opts =>
val options = scalaStewardScalafixOptions(opts.toList)
fileAlg.createTemporarily(buildRootDir, options)
}
withScalacOptions.surround {
val scalafixCmds = migration.rewriteRules.map(rule => s"$scalafixAll $rule").toList
val slurpOptions = SlurpOptions.ignoreBufferOverflow
sbt(Nel(scalafixEnable, scalafixCmds), buildRootDir, slurpOptions).void
}
}
for {
buildRootDir <- workspaceAlg.buildRootDir(buildRoot)
_ <- runSbtScalafix(buildRootDir, migration, metaBuilds = 0, startDepth = 0)
} yield ()

private def runBuildMigration(buildRoot: BuildRoot, migration: ScalafixMigration): F[Unit] =
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)
.map(_.lastOption)

private def runBuildMigration(buildRoot: BuildRoot, migration: ScalafixMigration): F[Unit] =
for {
buildRootDir <- workspaceAlg.buildRootDir(buildRoot)
projectDir = buildRootDir / project
files0 <- (
fileAlg.walk(buildRootDir, 1).filter(_.extension.contains(".sbt")) ++
fileAlg.walk(projectDir, 3).filter(_.extension.exists(Set(".sbt", ".scala")))
).compile.toList
_ <- Nel.fromList(files0).fold(F.unit) { files1 =>
scalafixCli.runMigration(buildRootDir, files1, migration)
}
} yield ()
private def addScalafixPluginTemporarily(
buildRootDir: File,
pluginVersion: Version,
metaBuilds: Int,
startDepth: Int
): Resource[F, Unit] = {
val buildsDepth = metaBuilds + startDepth
val plugin = scalaStewardSbtScalafix(pluginVersion)
List
.iterate(buildRootDir / project, buildsDepth + 1)(_ / project)
.drop(startDepth)
.collectFold(fileAlg.createTemporarily(_, plugin))
}

private def addScalacOptionsTemporarily(
buildRootDir: File,
scalacOptions: Option[Nel[String]],
metaBuilds: Int,
startDepth: Int
): Resource[F, Unit] =
scalacOptions.fold(Resource.unit[F]) { opts =>
val buildsDepth = metaBuilds + startDepth
val options = scalaStewardScalafixOptions(opts.toList)
List
.iterate(buildRootDir, buildsDepth + 1)(_ / project)
.drop(startDepth)
.collectFold(fileAlg.createTemporarily(_, options))
}

private def runSbtScalafix(
buildRootDir: File,
migration: ScalafixMigration,
metaBuilds: Int,
startDepth: Int
): F[Unit] =
OptionT(latestSbtScalafixVersion).foreachF { pluginVersion =>
addScalafixPluginTemporarily(buildRootDir, pluginVersion, metaBuilds, startDepth)
.surround {
addScalacOptionsTemporarily(buildRootDir, migration.scalacOptions, metaBuilds, startDepth)
.surround {
val scalafixCmds = migration.rewriteRules.map(rule => s"$scalafixAll $rule").toList
val slurpOptions = SlurpOptions.ignoreBufferOverflow
val buildsDepth = metaBuilds + startDepth
val scalafixCommands = scalafixEnable :: scalafixCmds
val commandLists =
(scalafixCommands :: List.fill(buildsDepth)(reloadPlugins :: scalafixCommands))
.drop(startDepth)
val commands = NonEmptyList.fromList(commandLists.flatten)
commands.fold(F.unit) { cmds =>
sbt(
cmds,
buildRootDir,
slurpOptions
).void
}
}
}
}

private def sbt(
sbtCommands: Nel[String],
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]
)
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that Files.walk throws an exception if the directory does not exist rather than simply returning an empty iterator


override def writeFile(file: File, content: String): F[Unit] =
logger.debug(s"Write $file") >>
Expand Down
Loading