Repository: scala-steward-org/scala-steward
Pull request #3095 (2023-06-19 18:53:04)

Different execution for mill 0.11
This address the issues discussed in https://github.com/scala-steward-org/scala-steward/pull/3083


Patch:
diff --git a/README.md b/README.md
index 5581efaa..a772813f 100644
--- a/README.md
+++ b/README.md
@@ -90,7 +90,6 @@ Thanks goes to these wonderful people for contributing to Scala Steward:
 * [Daniel Spiewak](https://github.com/djspiewak)
 * [Daniil Leontiev](https://github.com/danielleontiev)
 * [David Francoeur](https://github.com/daddykotex)
-* [Declan](https://github.com/d-g-n)
 * [Dominic Egger](https://github.com/GrafBlutwurst)
 * [Don Smith III](https://github.com/cactauz)
 * [Doug Roper](https://github.com/htmldoug)
diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/mill/MillAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/mill/MillAlg.scala
index b3f94843..13fa4edb 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/mill/MillAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/mill/MillAlg.scala
@@ -41,15 +41,26 @@ final class MillAlg[F[_]](implicit
       .buildRootDir(buildRoot)
       .flatMap(buildRootDir => fileAlg.isRegularFile(buildRootDir / "build.sc"))
 
+  private def runMill(buildRootDir: File) = {
+    val command = Nel("mill", List("-i", "--import", cliPluginCoordinate, "show", extractDeps))
+    processAlg.execSandboxed(command, buildRootDir)
+  }
+  private def runMillUnder011(buildRootDir: File, millBuildVersion: Option[Version]) = {
+    val predef = buildRootDir / "scala-steward.sc"
+    val predefContent = content(millBuildVersion)
+    val command = Nel("mill", List("-i", "-p", predef.toString, "show", extractDeps))
+    fileAlg.createTemporarily(predef, predefContent).surround {
+      processAlg.execSandboxed(command, buildRootDir)
+    }
+  }
+
   override def getDependencies(buildRoot: BuildRoot): F[List[Scope.Dependencies]] =
     for {
       buildRootDir <- workspaceAlg.buildRootDir(buildRoot)
-      predef = buildRootDir / "scala-steward.sc"
       millBuildVersion <- getMillVersion(buildRootDir)
-      extracted <- fileAlg.createTemporarily(predef, content(millBuildVersion)).surround {
-        val command = Nel("mill", List("-i", "-p", predef.toString, "show", extractDeps))
-        processAlg.execSandboxed(command, buildRootDir)
-      }
+      extracted <-
+        if (isMillVersionGreaterOrEqual011(millBuildVersion)) runMill(buildRootDir)
+        else runMillUnder011(buildRootDir, millBuildVersion)
       parsed <- F.fromEither(
         parser.parseModules(extracted.dropWhile(!_.startsWith("{")).mkString("\n"))
       )
@@ -92,21 +103,27 @@ final class MillAlg[F[_]](implicit
 }
 
 object MillAlg {
+  private[mill] def isMillVersionGreaterOrEqual011(millVersion: Option[Version]): Boolean =
+    millMinVersion(millVersion).flatMap(_.toIntOption).exists(_ >= 11)
+
+  private[mill] def millMinVersion(millVersion: Option[Version]): Option[String] =
+    millVersion.flatMap(_.value.trim.split("[.]", 3).take(2).lastOption)
+
+  private[mill] val cliPluginCoordinate: String =
+    s"ivy:${org.scalasteward.core.BuildInfo.organization}::${org.scalasteward.core.BuildInfo.millPluginArtifactName}::${org.scalasteward.core.BuildInfo.millPluginVersion}"
+
   private[mill] def content(millVersion: Option[Version]) = {
     def rawContent(millBinPlatform: String) =
       s"""|import $$ivy.`${org.scalasteward.core.BuildInfo.organization}::${org.scalasteward.core.BuildInfo.millPluginArtifactName}_mill${millBinPlatform}:${org.scalasteward.core.BuildInfo.millPluginVersion}`
           |""".stripMargin
 
-    millVersion match {
+    millMinVersion(millVersion) match {
       case None => rawContent("$MILL_BIN_PLATFORM")
-      case Some(millVersion) =>
-        millVersion.value.trim.split("[.]", 3).take(2) match {
-          // We support these platforms, but we can't take the $MILL_BIN_PLATFORM support for granted
-          case Array("0", "6")       => rawContent("0.6")
-          case Array("0", "7" | "8") => rawContent("0.7")
-          case Array("0", "9")       => rawContent("0.9")
-          case _                     => rawContent("$MILL_BIN_PLATFORM")
-        }
+      // We support these platforms, but we can't take the $MILL_BIN_PLATFORM support for granted
+      case Some("6")             => rawContent("0.6")
+      case Some("7") | Some("8") => rawContent("0.7")
+      case Some("9")             => rawContent("0.9")
+      case _                     => rawContent("$MILL_BIN_PLATFORM")
     }
   }
 
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 19460a3c..883d8316 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
@@ -154,7 +154,6 @@ final class SbtAlg[F[_]](config: Config)(implicit
           "-Dsbt.color=false",
           "-Dsbt.log.noformat=true",
           "-Dsbt.supershell=false",
-          "-Dsbt.server.forcestart=true",
           sbtCommands.mkString_(";", ";", "")
         )
       processAlg.execSandboxed(command, repoDir, slurpOptions = slurpOptions)


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala
index 897d852d..72d801ee 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/BuildToolDispatcherTest.scala
@@ -65,7 +65,6 @@ class BuildToolDispatcherTest extends FunSuite {
           "-Dsbt.color=false",
           "-Dsbt.log.noformat=true",
           "-Dsbt.supershell=false",
-          "-Dsbt.server.forcestart=true",
           s";$crossStewardDependencies;$reloadPlugins;$stewardDependencies"
         ),
         Cmd("rm", "-rf", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala"),
diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/mill/MillAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/mill/MillAlgTest.scala
index fe371883..c2969b4b 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/mill/MillAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/mill/MillAlgTest.scala
@@ -10,7 +10,7 @@ import org.scalasteward.core.mock.MockState
 import org.scalasteward.core.mock.MockState.TraceEntry.Cmd
 
 class MillAlgTest extends FunSuite {
-  test("getDependencies") {
+  test("getDependencies, version < 0.11") {
     val repo = Repo("lihaoyi", "fastparse")
     val buildRoot = BuildRoot(repo, ".")
     val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
@@ -30,6 +30,40 @@ class MillAlgTest extends FunSuite {
     )
     assertEquals(state, expected)
   }
+
+  test("getDependencies, version >= 0.11") {
+    val repo = Repo("lihaoyi", "fastparse")
+    val buildRoot = BuildRoot(repo, ".")
+    val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
+    val buildRootDir = workspaceAlg.buildRootDir(buildRoot).unsafeRunSync()
+    val millCmd = Cmd.execSandboxed(
+      repoDir,
+      "mill",
+      "-i",
+      "--import",
+      "ivy:org.scala-steward::scala-steward-mill-plugin::0.18.0",
+      "show",
+      extractDeps
+    )
+    val initial =
+      MockState.empty
+        .copy(
+          commandOutputs = Map(millCmd -> Right(List("""{"modules":[]}""")))
+        )
+        .addFiles(buildRootDir / ".mill-version" -> "0.11.0")
+        .unsafeRunSync()
+    val state = millAlg.getDependencies(buildRoot).runS(initial).unsafeRunSync()
+    val expected = initial.copy(
+      trace = Vector(
+        Cmd("read", s"$repoDir/.mill-version"),
+        Cmd("read", s"$repoDir/.config/mill-version"),
+        millCmd,
+        Cmd("read", s"$repoDir/build.sc")
+      )
+    )
+    assertEquals(state, expected)
+  }
+
   test("predef-content") {
     assert(MillAlg.content(None).contains("_mill$MILL_BIN_PLATFORM"))
     assert(MillAlg.content(Some(Version("0.6.1"))).contains("_mill0.6"))
@@ -38,4 +72,14 @@ class MillAlgTest extends FunSuite {
     assert(MillAlg.content(Some(Version("0.9.14"))).contains("_mill0.9"))
     assert(MillAlg.content(Some(Version("0.10.0"))).contains("_mill$MILL_BIN_PLATFORM"))
   }
+
+  test("mill version") {
+    assert(MillAlg.millMinVersion(None).isEmpty)
+    assert(MillAlg.millMinVersion(Some(Version("0.6.1"))).contains("6"))
+    assert(MillAlg.millMinVersion(Some(Version("0.7.0"))).contains("7"))
+    assert(MillAlg.millMinVersion(Some(Version("0.8.0"))).contains("8"))
+    assert(MillAlg.millMinVersion(Some(Version("0.9.14"))).contains("9"))
+    assert(MillAlg.millMinVersion(Some(Version("0.10.0"))).contains("10"))
+    assert(MillAlg.millMinVersion(Some(Version("0.11.0"))).contains("11"))
+  }
 }
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 f065d891..07c4a9a3 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
@@ -36,7 +36,6 @@ class SbtAlgTest extends FunSuite {
           "-Dsbt.color=false",
           "-Dsbt.log.noformat=true",
           "-Dsbt.supershell=false",
-          "-Dsbt.server.forcestart=true",
           s";$crossStewardDependencies;$reloadPlugins;$stewardDependencies"
         ),
         Cmd("rm", "-rf", s"$repoDir/project/project/scala-steward-StewardPlugin_1_3_11.scala"),
@@ -75,7 +74,6 @@ class SbtAlgTest extends FunSuite {
           "-Dsbt.color=false",
           "-Dsbt.log.noformat=true",
           "-Dsbt.supershell=false",
-          "-Dsbt.server.forcestart=true",
           s";$scalafixEnable;$scalafixAll github:functional-streams-for-scala/fs2/v1?sha=v1.0.5"
         ),
         Cmd("rm", "-rf", s"$repoDir/project/scala-steward-sbt-scalafix.sbt")
@@ -115,7 +113,6 @@ class SbtAlgTest extends FunSuite {
           "-Dsbt.color=false",
           "-Dsbt.log.noformat=true",
           "-Dsbt.supershell=false",
-          "-Dsbt.server.forcestart=true",
           s";$scalafixEnable;$scalafixAll github:cb372/cats/Cats_v2_2_0?sha=235bd7c92e431ab1902db174cf4665b05e08f2f1"
         ),
         Cmd("rm", "-rf", s"$repoDir/scala-steward-scalafix-options.sbt"),
diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala
index 4841481f..0adc4cc6 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/scalacli/ScalaCliAlgTest.scala
@@ -53,7 +53,6 @@ class ScalaCliAlgTest extends CatsEffectSuite {
           "-Dsbt.color=false",
           "-Dsbt.log.noformat=true",
           "-Dsbt.supershell=false",
-          "-Dsbt.server.forcestart=true",
           s";$crossStewardDependencies"
         ),
         Cmd("rm", "-rf", s"$sbtBuildDir/project/scala-steward-StewardPlugin_1_3_11.scala"),