Skip to content

Commit

Permalink
Support build.sc in Bloop (#3208)
Browse files Browse the repository at this point in the history
After Mill `0.11` `mill-contrib-bloop` stopped supporting the build
module.
Now `BloopImpl` gets a sequence of `Evaluator`s and gets all modules for
all the evaluators in `Evaluator.allBootstrapEvaluators`, so it supports
the build module again.

Pull Request: #3208
  • Loading branch information
lolgab authored Jun 18, 2024
1 parent f4c84d1 commit d7e471e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
1 change: 1 addition & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,7 @@ object dev extends MillPublishScalaModule {
def testTransitiveDeps = super.testTransitiveDeps() ++ Seq(
runner.linenumbers.testDep(),
scalalib.backgroundwrapper.testDep(),
contrib.bloop.testDep(),
contrib.buildinfo.testDep(),
contrib.scoverage.testDep(),
contrib.scoverage.worker2.testDep(),
Expand Down
2 changes: 1 addition & 1 deletion contrib/bloop/src/mill/contrib/bloop/Bloop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import mill.eval.Evaluator
/**
* Usage : `mill mill.contrib.bloop.Bloop/install`
*/
object Bloop extends BloopImpl(() => Evaluator.currentEvaluator.value, os.pwd)
object Bloop extends BloopImpl(() => Evaluator.allBootstrapEvaluators.value.value, os.pwd)
28 changes: 15 additions & 13 deletions contrib/bloop/src/mill/contrib/bloop/BloopImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mill._
import mill.api.Result
import mill.define.{Discover, ExternalModule, Module => MillModule}
import mill.eval.Evaluator
import mill.scalalib.internal.{JavaModuleUtils, ModuleUtils}
import mill.scalalib.internal.JavaModuleUtils
import mill.scalajslib.ScalaJSModule
import mill.scalajslib.api.{JsEnvConfig, ModuleKind}
import mill.scalalib._
Expand All @@ -17,7 +17,8 @@ import mill.scalanativelib.api.ReleaseMode
* `mill.contrib.bloop.Bloop` object, and usable in tests by passing
* a custom evaluator.
*/
class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer =>
class BloopImpl(evs: () => Seq[Evaluator], wd: os.Path) extends ExternalModule {
outer =>
import BloopFormats._

private val bloopDir = wd / ".bloop"
Expand Down Expand Up @@ -116,12 +117,14 @@ class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer
}

protected def computeModules: Seq[JavaModule] = {
val eval = ev()
if (eval != null)
JavaModuleUtils.transitiveModules(eval.rootModule, accept)
.collect { case jm: JavaModule => jm }
else
Seq.empty
val evals = evs()
evals.flatMap { eval =>
if (eval != null)
JavaModuleUtils.transitiveModules(eval.rootModule, accept)
.collect { case jm: JavaModule => jm }
else
Seq.empty
}
}

// class-based pattern matching against path-dependant types doesn't seem to work.
Expand All @@ -144,10 +147,7 @@ class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer
Result.Success(sources.toMap)
}

protected def name(m: JavaModule): String = ModuleUtils.moduleDisplayName(m) match {
case "" => "root-module"
case n => n
}
protected def name(m: JavaModule): String = m.bspDisplayName.replace('/', '-')

protected def bloopConfigPath(module: JavaModule): os.Path =
bloopDir / s"${name(module)}.json"
Expand Down Expand Up @@ -365,6 +365,7 @@ class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer
.groupBy {
case (org, mod, version, _, _) => (org, mod, version)
}
.view
.mapValues {
_.map {
case (_, mod, _, classifier, file) =>
Expand All @@ -381,8 +382,9 @@ class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer
artifacts = artifacts
)
}
.toList

gatherTask.unsafeRun().toList
gatherTask.unsafeRun()
}

val bloopResolution: Task[BloopConfig.Resolution] = T.task {
Expand Down
2 changes: 1 addition & 1 deletion contrib/bloop/test/src/mill/contrib/bloop/BloopTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object BloopTests extends TestSuite {

val workdir = os.pwd / "target" / "workspace" / "bloop"
val testEvaluator = TestEvaluator.static(build)
val testBloop = new BloopImpl(() => testEvaluator.evaluator, workdir)
val testBloop = new BloopImpl(() => Seq(testEvaluator.evaluator), workdir)

object build extends TestUtil.BaseModule {

Expand Down
8 changes: 8 additions & 0 deletions integration/feature/bloop/repo/build.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// mill plugins
import $ivy.`com.lihaoyi::mill-contrib-bloop:`

// imports
import mill._
import mill.scalalib._

object root extends RootModule with JavaModule
30 changes: 30 additions & 0 deletions integration/feature/bloop/test/src/BloopTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mill.integration

import utest._

object BloopTests extends IntegrationTestSuite {
initWorkspace()

val installResult: Boolean = eval("mill.contrib.bloop.Bloop/install")

val tests: Tests = Tests {
test("test") - {
assert(installResult)

"root module bloop config should be created" - {
assert(os.exists(wd / ".bloop" / "root-module.json"))
}
val millBuildJsonFile = wd / ".bloop" / "mill-build-.json"
"mill-build module bloop config should be created" - {
assert(os.exists(millBuildJsonFile))
}

val config = ujson.read(os.read.stream(millBuildJsonFile))
"mill-build config should contain build.sc source" - {
assert(config("project")("sources").arr.exists(path =>
os.Path(path.str).last == "build.sc"
))
}
}
}
}

0 comments on commit d7e471e

Please # to comment.