Repository: scala-steward-org/scala-steward (scala)

Issue:
SBT Plugin not cleaned up after run
The `StewardPlugin.scala` plugin is not cleaned up after using scala-steward. The following 2 plugin files persist.

`/jenkins/.sbt/0.13/plugins/StewardPlugin.scala` and `/jenkins/.sbt/1.0/plugins/StewardPlugin.scala`

It seems they're being created here "temporarily" but never cleaned up. https://github.com/fthomas/scala-steward/blob/f251fcdaac80f55bc0fb21017268c8a77fe70be4/modules/core/src/main/scala/org/scalasteward/core/sbt/SbtAlg.scala#L71-L79

Thank you for  👀

Pull request description:
Uses `addGlobalPluginTemporarily` to ensure, that the plugins are cleaned up afterwards automatically.

fixes #855

Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
index a5d4a71fb5..46bf446028 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
@@ -61,12 +61,6 @@ final class StewardAlg[F[_]](
     logger.info(msg)
   }
 
-  private def prepareEnv: F[Unit] =
-    for {
-      _ <- sbtAlg.addGlobalPlugins
-      _ <- workspaceAlg.cleanWorkspace
-    } yield ()
-
   private def readRepos(reposFile: File): F[List[Repo]] =
     fileAlg.readFile(reposFile).map { maybeContent =>
       val regex = """-\s+(.+)/([^/]+)""".r
@@ -95,9 +89,13 @@ final class StewardAlg[F[_]](
       for {
         _ <- printBanner
         _ <- selfCheckAlg.checkAll
-        _ <- prepareEnv
-        repos <- readRepos(config.reposFile)
-        result <- Stream.emits(repos).evalMap(steward).compile.foldMonoid
-      } yield result.fold(_ => ExitCode.Error, _ => ExitCode.Success)
+        exitCode <- sbtAlg.addGlobalPlugins {
+          for {
+            _ <- workspaceAlg.cleanWorkspace
+            repos <- readRepos(config.reposFile)
+            result <- Stream.emits(repos).evalMap(steward).compile.foldMonoid
+          } yield result.fold(_ => ExitCode.Error, _ => ExitCode.Success)
+        }
+      } yield exitCode
     }
 }
diff --git a/modules/core/src/main/scala/org/scalasteward/core/sbt/SbtAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/sbt/SbtAlg.scala
index 9926ffb14b..2080e1f009 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/sbt/SbtAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/sbt/SbtAlg.scala
@@ -33,11 +33,9 @@ import org.scalasteward.core.util.Nel
 import org.scalasteward.core.vcs.data.Repo
 
 trait SbtAlg[F[_]] {
-  def addGlobalPlugin(plugin: FileData): F[Unit]
-
   def addGlobalPluginTemporarily[A](plugin: FileData)(fa: F[A]): F[A]
 
-  def addGlobalPlugins: F[Unit]
+  def addGlobalPlugins[A](fa: F[A]): F[A]
 
   def getSbtVersion(repo: Repo): F[Option[SbtVersion]]
 
@@ -64,11 +62,6 @@ object SbtAlg {
       F: Monad[F]
   ): SbtAlg[F] =
     new SbtAlg[F] {
-      override def addGlobalPlugin(plugin: FileData): F[Unit] =
-        List("0.13", "1.0").traverse_ { series =>
-          sbtDir.flatMap(dir => fileAlg.writeFileData(dir / series / "plugins", plugin))
-        }
-
       override def addGlobalPluginTemporarily[A](plugin: FileData)(fa: F[A]): F[A] =
         sbtDir.flatMap { dir =>
           val plugins = "plugins"
@@ -79,12 +72,15 @@ object SbtAlg {
           }
         }
 
-      override def addGlobalPlugins: F[Unit] =
+      override def addGlobalPlugins[A](fa: F[A]): F[A] =
         for {
           _ <- logger.info("Add global sbt plugins")
-          _ <- addGlobalPlugin(scalaStewardSbt)
-          _ <- addGlobalPlugin(stewardPlugin)
-        } yield ()
+          result <- addGlobalPluginTemporarily(scalaStewardSbt) {
+            addGlobalPluginTemporarily(stewardPlugin) {
+              fa
+            }
+          }
+        } yield result
 
       override def getSbtVersion(repo: Repo): F[Option[SbtVersion]] =
         for {


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/sbt/SbtAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/sbt/SbtAlgTest.scala
index 145786f375..fd950cb7a3 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/sbt/SbtAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/sbt/SbtAlgTest.scala
@@ -1,6 +1,6 @@
 package org.scalasteward.core.sbt
 
-import better.files.File
+import cats.data.StateT
 import org.scalasteward.core.application.Config
 import org.scalasteward.core.data.{GroupId, Version}
 import org.scalasteward.core.mock.MockContext._
@@ -13,21 +13,25 @@ import org.scalatest.funsuite.AnyFunSuite
 import org.scalatest.matchers.should.Matchers
 
 class SbtAlgTest extends AnyFunSuite with Matchers {
+
   test("addGlobalPlugins") {
-    sbtAlg.addGlobalPlugins.runS(MockState.empty).unsafeRunSync() shouldBe MockState.empty.copy(
+    sbtAlg
+      .addGlobalPlugins(StateT.modify(_.exec(List("fa", "fa"))))
+      .runS(MockState.empty)
+      .unsafeRunSync() shouldBe MockState.empty.copy(
       commands = Vector(
-        List("write", "/tmp/steward/.sbt/0.13/plugins/scala-steward.sbt"),
-        List("write", "/tmp/steward/.sbt/1.0/plugins/scala-steward.sbt"),
-        List("write", "/tmp/steward/.sbt/0.13/plugins/StewardPlugin.scala"),
-        List("write", "/tmp/steward/.sbt/1.0/plugins/StewardPlugin.scala")
+        List("create", "/tmp/steward/.sbt/0.13/plugins/scala-steward.sbt"),
+        List("create", "/tmp/steward/.sbt/1.0/plugins/scala-steward.sbt"),
+        List("create", "/tmp/steward/.sbt/0.13/plugins/StewardPlugin.scala"),
+        List("create", "/tmp/steward/.sbt/1.0/plugins/StewardPlugin.scala"),
+        List("fa", "fa"),
+        List("rm", "/tmp/steward/.sbt/1.0/plugins/StewardPlugin.scala"),
+        List("rm", "/tmp/steward/.sbt/0.13/plugins/StewardPlugin.scala"),
+        List("rm", "/tmp/steward/.sbt/1.0/plugins/scala-steward.sbt"),
+        List("rm", "/tmp/steward/.sbt/0.13/plugins/scala-steward.sbt")
       ),
       logs = Vector((None, "Add global sbt plugins")),
-      files = Map(
-        File("/tmp/steward/.sbt/0.13/plugins/scala-steward.sbt") -> scalaStewardSbt.content,
-        File("/tmp/steward/.sbt/1.0/plugins/scala-steward.sbt") -> scalaStewardSbt.content,
-        File("/tmp/steward/.sbt/0.13/plugins/StewardPlugin.scala") -> stewardPlugin.content,
-        File("/tmp/steward/.sbt/1.0/plugins/StewardPlugin.scala") -> stewardPlugin.content
-      )
+      files = Map.empty
     )
   }