diff --git a/bsp/worker/src/mill/bsp/worker/State.scala b/bsp/worker/src/mill/bsp/worker/State.scala index 9046f5e1472..5dc82caae91 100644 --- a/bsp/worker/src/mill/bsp/worker/State.scala +++ b/bsp/worker/src/mill/bsp/worker/State.scala @@ -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 @@ -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) } diff --git a/main/src/mill/main/MainModule.scala b/main/src/mill/main/MainModule.scala index 3babff06f10..5d31d5c231c 100644 --- a/main/src/mill/main/MainModule.scala +++ b/main/src/mill/main/MainModule.scala @@ -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 @@ -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) } } diff --git a/main/util/src/mill/util/PrefixLogger.scala b/main/util/src/mill/util/PrefixLogger.scala index 73d39789699..994ae13af2b 100644 --- a/main/util/src/mill/util/PrefixLogger.scala +++ b/main/util/src/mill/util/PrefixLogger.scala @@ -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( @@ -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) } diff --git a/runner/src/mill/runner/MillBuildBootstrap.scala b/runner/src/mill/runner/MillBuildBootstrap.scala index 5c9c0267429..fd2580795ae 100644 --- a/runner/src/mill/runner/MillBuildBootstrap.scala +++ b/runner/src/mill/runner/MillBuildBootstrap.scala @@ -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 @@ -35,7 +35,7 @@ class MillBuildBootstrap( threadCount: Option[Int], targetsAndParams: Seq[String], prevRunnerState: RunnerState, - logger: ColorLogger + logger: Logger ) { import MillBuildBootstrap._ @@ -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 @@ -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") } } diff --git a/runner/src/mill/runner/RootModuleFinder.scala b/runner/src/mill/runner/RootModuleFinder.scala new file mode 100644 index 00000000000..0940b08b7cb --- /dev/null +++ b/runner/src/mill/runner/RootModuleFinder.scala @@ -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 + } + +} diff --git a/runner/src/mill/runner/RunnerState.scala b/runner/src/mill/runner/RunnerState.scala index 399d8c20fa5..ac60248fcd6 100644 --- a/runner/src/mill/runner/RunnerState.scala +++ b/runner/src/mill/runner/RunnerState.scala @@ -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 /** @@ -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) } }