From 8f1e56cb83030605fb9e146f182bd12cb93ad886 Mon Sep 17 00:00:00 2001 From: David Gregory <2992938+DavidGregory084@users.noreply.github.com> Date: Mon, 31 Jul 2023 14:52:27 +0100 Subject: [PATCH] Extract the common bits of runSourcesMigration and runBuildMigration into runSbtScalafix --- .../core/buildtool/sbt/SbtAlg.scala | 87 ++++++++++--------- .../core/buildtool/sbt/SbtAlgTest.scala | 12 +-- 2 files changed, 53 insertions(+), 46 deletions(-) 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 aa63728e4f..e6556f15a0 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 @@ -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 @@ -107,22 +107,17 @@ 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) + _ <- runSbtScalafix(buildRootDir, migration, metaBuilds, startDepth = 1) + } yield () private def latestSbtScalafixVersion: F[Option[Version]] = versionsCache @@ -132,48 +127,60 @@ final class SbtAlg[F[_]](config: Config)(implicit private def addScalafixPluginTemporarily( buildRootDir: File, pluginVersion: Version, - metaBuilds: Int + metaBuilds: Int, + startDepth: Int ): Resource[F, Unit] = { + val buildsDepth = metaBuilds + startDepth val plugin = scalaStewardSbtScalafix(pluginVersion) List - .iterate(buildRootDir / project / project, metaBuilds + 1)(_ / project) + .iterate(buildRootDir / project, buildsDepth + 1)(_ / project) + .drop(startDepth) .collectFold(fileAlg.createTemporarily(_, plugin)) } private def addScalacOptionsTemporarily( buildRootDir: File, scalacOptions: Option[Nel[String]], - metaBuilds: Int + 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 / project, metaBuilds + 1)(_ / project) + .iterate(buildRootDir, buildsDepth + 1)(_ / project) + .drop(startDepth) .collectFold(fileAlg.createTemporarily(_, options)) } - private def runBuildMigration(buildRoot: BuildRoot, migration: ScalafixMigration): F[Unit] = + private def runSbtScalafix( + buildRootDir: File, + migration: ScalafixMigration, + metaBuilds: Int, + startDepth: Int + ): F[Unit] = OptionT(latestSbtScalafixVersion).foreachF { pluginVersion => - for { - buildRootDir <- workspaceAlg.buildRootDir(buildRoot) - metaBuilds <- metaBuildsCount(buildRootDir) - _ <- addScalafixPluginTemporarily(buildRootDir, pluginVersion, metaBuilds).surround { - addScalacOptionsTemporarily(buildRootDir, migration.scalacOptions, metaBuilds).surround { - val scalafixCmds = migration.rewriteRules.map(rule => s"$scalafixAll $rule").toList - val slurpOptions = SlurpOptions.ignoreBufferOverflow - val commands = Nel.fromList( - List.fill(metaBuilds + 1)(List(reloadPlugins, scalafixEnable) ++ scalafixCmds).flatten - ) - commands.fold(F.unit) { cmds => - sbt( - cmds, - buildRootDir, - slurpOptions - ).void + 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 + } } - } } - } yield () } private def sbt( 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 a22b7b064f..ea06c3feb1 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 @@ -144,11 +144,11 @@ class SbtAlgTest extends FunSuite { val state = sbtAlg.runMigration(buildRoot, migration).runS(initialState).unsafeRunSync() val expected = initialState.copy( trace = Vector( + Cmd("test", "-d", s"$repoDir/project"), Cmd( "read", s"$workspace/store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json" ), - Cmd("test", "-d", s"$repoDir/project"), Cmd("write", s"$repoDir/project/project/scala-steward-sbt-scalafix.sbt"), Cmd.execSandboxed( repoDir, @@ -185,12 +185,12 @@ class SbtAlgTest extends FunSuite { val state = sbtAlg.runMigration(buildRoot, migration).runS(initialState).unsafeRunSync() val expected = initialState.copy( trace = Vector( + Cmd("test", "-d", s"$repoDir/project"), + Cmd("test", "-d", s"$repoDir/project/project"), Cmd( "read", s"$workspace/store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json" ), - Cmd("test", "-d", s"$repoDir/project"), - Cmd("test", "-d", s"$repoDir/project/project"), Cmd("write", s"$repoDir/project/project/scala-steward-sbt-scalafix.sbt"), Cmd("write", s"$repoDir/project/project/project/scala-steward-sbt-scalafix.sbt"), Cmd.execSandboxed( @@ -230,11 +230,11 @@ class SbtAlgTest extends FunSuite { val state = sbtAlg.runMigration(buildRoot, migration).runS(initialState).unsafeRunSync() val expected = initialState.copy( trace = Vector( + Cmd("test", "-d", s"$repoDir/project"), Cmd( "read", s"$workspace/store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json" ), - Cmd("test", "-d", s"$repoDir/project"), Cmd("write", s"$repoDir/project/project/scala-steward-sbt-scalafix.sbt"), Cmd("write", s"$repoDir/project/scala-steward-scalafix-options.sbt"), Cmd.execSandboxed( @@ -274,12 +274,12 @@ class SbtAlgTest extends FunSuite { val state = sbtAlg.runMigration(buildRoot, migration).runS(initialState).unsafeRunSync() val expected = initialState.copy( trace = Vector( + Cmd("test", "-d", s"$repoDir/project"), + Cmd("test", "-d", s"$repoDir/project/project"), Cmd( "read", s"$workspace/store/versions/v2/https/repo1.maven.org/maven2/ch/epfl/scala/sbt-scalafix_2.12_1.0/versions.json" ), - Cmd("test", "-d", s"$repoDir/project"), - Cmd("test", "-d", s"$repoDir/project/project"), Cmd("write", s"$repoDir/project/project/scala-steward-sbt-scalafix.sbt"), Cmd("write", s"$repoDir/project/project/project/scala-steward-sbt-scalafix.sbt"), Cmd("write", s"$repoDir/project/scala-steward-scalafix-options.sbt"),