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

Refactored root modules finder #2648

Closed
wants to merge 6 commits into from
Closed
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
39 changes: 4 additions & 35 deletions bsp/worker/src/mill/bsp/worker/State.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mill.bsp.worker

import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import mill.runner.MillBuildBootstrap
import mill.runner.{MillBuildBootstrap, RootModuleFinder}
import mill.scalalib.bsp.BspModule
import mill.scalalib.internal.{JavaModuleUtils, ModuleUtils}
import mill.define.Module
Expand All @@ -24,44 +24,13 @@ private class State(projectRoot: os.Path, baseLogger: ColorLogger, debug: String
}
}
.toMap
debug(s"BspModules: ${map.mapValues(_.bspDisplayName)}")
debug(s"BspModules: ${map.view.mapValues(_.bspDisplayName)}")

map
}

lazy val rootModules: Seq[mill.main.RootModule] = {
val evaluated = new mill.runner.MillBuildBootstrap(
projectRoot = projectRoot,
home = os.home,
keepGoing = false,
imports = Nil,
env = Map.empty,
threadCount = None,
targetsAndParams = Seq("resolve", "_"),
prevRunnerState = mill.runner.RunnerState.empty,
logger = baseLogger
).evaluate()
lazy val rootModules: Seq[mill.main.RootModule] =
RootModuleFinder.findRootModules(projectRoot, baseLogger)

val rootModules0 = evaluated.result.frames
.flatMap(_.classLoaderOpt)
.zipWithIndex
.map { case (c, i) =>
MillBuildBootstrap
.getRootModule(c, i, projectRoot)
.fold(sys.error(_), identity(_))
}

val bootstrapModule = evaluated.result.bootstrapModuleOpt.map(m =>
MillBuildBootstrap
.getChildRootModule(
m,
evaluated.result.frames.length,
projectRoot
)
.fold(sys.error(_), identity(_))
)

rootModules0 ++ bootstrapModule
}
lazy val bspIdByModule: Map[BspModule, BuildTargetIdentifier] = bspModulesById.map(_.swap)
}
25 changes: 19 additions & 6 deletions main/src/mill/main/MainModule.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package mill.main

import mainargs.Flag

import java.util.concurrent.LinkedBlockingQueue
import mill.define.Target
import mill.define.{Args, Command, NamedTask, Segments, Target, Task}
import mill.main.BuildInfo
import mill.api.{Ctx, Logger, PathRef, Result}
import mill.define.{Command, NamedTask, Segments, Task}
import mill.eval.{Evaluator, EvaluatorPaths, Terminal}
import mill.resolve.{Resolve, SelectMode}
import mill.resolve.SelectMode.Separated
Expand Down Expand Up @@ -105,21 +106,33 @@ trait MainModule extends mill.define.Module {
res
}

@deprecated("Binary compatibility shim. Use other overload instead.", "Mill 0.11.2")
private[main] def resolve(evaluator: Evaluator, targets: String*): Command[List[String]] =
resolve(evaluator, Flag(false), Args(targets))

/**
* Resolves a mill query string and prints out the tasks it resolves to.
* @param quiet If true, do not print the resolved tasks
* @param targets The target patterns to resolve
*/
def resolve(evaluator: Evaluator, targets: String*): Command[List[String]] = Target.command {
def resolve(
evaluator: Evaluator,
quiet: Flag = Flag(false),
targets: Args = Args()
): Command[List[String]] = Target.command {
val resolved = Resolve.Segments.resolve(
evaluator.rootModule,
targets,
targets.value,
SelectMode.Multi
)

resolved match {
case Left(err) => Result.Failure(err)
case Right(resolvedSegmentsList) =>
val resolvedStrings = resolvedSegmentsList.map(_.render)
resolvedStrings.sorted.foreach(Target.log.outputStream.println)
val resolvedStrings = resolvedSegmentsList.map(_.render).sorted
if (!quiet.value) {
Target.log.outputStream.println(resolvedStrings.mkString("\n"))
}
Result.Success(resolvedStrings)
}
}
Expand Down
27 changes: 21 additions & 6 deletions main/util/src/mill/util/PrefixLogger.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package mill.util

import mill.api.SystemStreams
import mill.api.{Logger, SystemStreams}

import java.io.PrintStream

class PrefixLogger(out: ColorLogger, context: String, tickerContext: String = "")
class PrefixLogger(out: Logger, context: String, tickerContext: String = "")
extends ColorLogger {
override def colored = out.colored

def infoColor = out.infoColor
def errorColor = out.errorColor
@deprecated("Binary compatibility shim", "Mill 0.11.2")
def this(out: ColorLogger, context: String, tickerContext: String) = {
this(out: Logger, context, tickerContext)
}

private val colorLogger = out match {
case c: ColorLogger => Some(c)
case _ => None
}

override def colored = colorLogger.fold(false)(_.colored)

def infoColor = colorLogger.fold(mill.util.Colors.BlackWhite.info)(_.infoColor)
def errorColor = colorLogger.fold(mill.util.Colors.BlackWhite.error)(_.infoColor)

val systemStreams = new SystemStreams(
new PrintStream(new LinePrefixOutputStream(
Expand All @@ -35,6 +46,10 @@ class PrefixLogger(out: ColorLogger, context: String, tickerContext: String = ""
}

object PrefixLogger {
def apply(out: ColorLogger, context: String, tickerContext: String = ""): PrefixLogger =
def apply(out: Logger, context: String, tickerContext: String = ""): PrefixLogger =
new PrefixLogger(out, context, tickerContext)

@deprecated("Binary compatibility shim", "Mill 0.11.2")
def apply(out: ColorLogger, context: String, tickerContext: String): PrefixLogger =
new PrefixLogger(out: Logger, context, tickerContext)
}
14 changes: 9 additions & 5 deletions runner/src/mill/runner/MillBuildBootstrap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mill.runner
import mill.util.{ColorLogger, PrefixLogger, Util, Watchable}
import mill.T
import mill.main.BuildInfo
import mill.api.{PathRef, Val, internal}
import mill.api.{Logger, PathRef, Val, internal}
import mill.eval.Evaluator
import mill.main.{RootModule, RunScript}
import mill.resolve.SelectMode
Expand Down Expand Up @@ -35,7 +35,7 @@ class MillBuildBootstrap(
threadCount: Option[Int],
targetsAndParams: Seq[String],
prevRunnerState: RunnerState,
logger: ColorLogger
logger: Logger
) {
import MillBuildBootstrap._

Expand Down Expand Up @@ -348,7 +348,11 @@ object MillBuildBootstrap {
getChildRootModule(rootModule0, depth, projectRoot)
}

def getChildRootModule(rootModule0: RootModule, depth: Int, projectRoot: os.Path) = {
def getChildRootModule(
rootModule0: RootModule,
depth: Int,
projectRoot: os.Path
): Either[String, RootModule] = {

val childRootModules: Seq[RootModule] = rootModule0
.millInternal
Expand Down Expand Up @@ -382,11 +386,11 @@ object MillBuildBootstrap {
)
}

def recRoot(projectRoot: os.Path, depth: Int) = {
def recRoot(projectRoot: os.Path, depth: Int): os.Path = {
projectRoot / Seq.fill(depth)("mill-build")
}

def recOut(projectRoot: os.Path, depth: Int) = {
def recOut(projectRoot: os.Path, depth: Int): os.Path = {
projectRoot / "out" / Seq.fill(depth)("mill-build")
}
}
41 changes: 41 additions & 0 deletions runner/src/mill/runner/RootModuleFinder.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mill.runner

import mill.api.Logger

object RootModuleFinder {
def findRootModules(projectRoot: os.Path, logger: Logger): Seq[mill.main.RootModule] = {
val evaluated = new mill.runner.MillBuildBootstrap(
projectRoot = projectRoot,
home = os.home,
keepGoing = false,
imports = Nil,
env = Map.empty,
threadCount = None,
targetsAndParams = Seq("resolve", "--quiet", "_"),
prevRunnerState = mill.runner.RunnerState.empty,
logger = logger
).evaluate()

val rootModules0 = evaluated.result.frames
.flatMap(_.classLoaderOpt)
.zipWithIndex
.map { case (c, i) =>
MillBuildBootstrap
.getRootModule(c, i, projectRoot)
.fold(sys.error(_), identity(_))
}

val bootstrapModule = evaluated.result.bootstrapModuleOpt.map(m =>
MillBuildBootstrap
.getChildRootModule(
m,
evaluated.result.frames.length,
projectRoot
)
.fold(sys.error(_), identity(_))
)

rootModules0 ++ bootstrapModule
}

}
6 changes: 4 additions & 2 deletions runner/src/mill/runner/RunnerState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import mill.define.{BaseModule, Segments}
import mill.util.Watchable
import upickle.default.{ReadWriter, macroRW}
import mill.api.JsonFormatters._
import mill.eval.Evaluator
import mill.main.RootModule

/**
Expand Down Expand Up @@ -33,7 +32,10 @@ case class RunnerState(
frames: Seq[RunnerState.Frame],
errorOpt: Option[String]
) {
def add(frame: RunnerState.Frame = RunnerState.Frame.empty, errorOpt: Option[String] = None) = {
def add(
frame: RunnerState.Frame = RunnerState.Frame.empty,
errorOpt: Option[String] = None
): RunnerState = {
this.copy(frames = Seq(frame) ++ frames, errorOpt = errorOpt)
}
}
Expand Down