From feefd0b44f29b8b1a63e44c562d9db5058adb638 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Mon, 16 Dec 2024 15:56:03 +0800 Subject: [PATCH] wip --- main/eval/src/mill/eval/Evaluator.scala | 1 + main/eval/src/mill/eval/EvaluatorImpl.scala | 9 ++-- main/src/mill/main/MainModule.scala | 3 +- main/src/mill/main/RunScript.scala | 49 ++++++++++--------- .../src/mill/runner/MillBuildBootstrap.scala | 3 +- testkit/src/mill/testkit/UnitTester.scala | 3 +- 6 files changed, 39 insertions(+), 29 deletions(-) diff --git a/main/eval/src/mill/eval/Evaluator.scala b/main/eval/src/mill/eval/Evaluator.scala index d8c99768d67..d349fa6b67b 100644 --- a/main/eval/src/mill/eval/Evaluator.scala +++ b/main/eval/src/mill/eval/Evaluator.scala @@ -19,6 +19,7 @@ trait Evaluator extends AutoCloseable { def rootModule: BaseModule def effectiveThreadCount: Int def outPath: os.Path + def selectiveExecution: Boolean = false def externalOutPath: os.Path def pathsResolver: EvaluatorPathsResolver def methodCodeHashSignatures: Map[String, Int] = Map.empty diff --git a/main/eval/src/mill/eval/EvaluatorImpl.scala b/main/eval/src/mill/eval/EvaluatorImpl.scala index 507497cc135..92f3e8ed59d 100644 --- a/main/eval/src/mill/eval/EvaluatorImpl.scala +++ b/main/eval/src/mill/eval/EvaluatorImpl.scala @@ -34,7 +34,8 @@ private[mill] case class EvaluatorImpl( val systemExit: Int => Nothing, val exclusiveSystemStreams: SystemStreams, protected[eval] val chromeProfileLogger: ChromeProfileLogger, - protected[eval] val profileLogger: ProfileLogger + protected[eval] val profileLogger: ProfileLogger, + override val selectiveExecution: Boolean = false ) extends Evaluator with EvaluatorCore { import EvaluatorImpl._ @@ -93,7 +94,8 @@ private[mill] object EvaluatorImpl { disableCallgraph: Boolean, allowPositionalCommandArgs: Boolean, systemExit: Int => Nothing, - exclusiveSystemStreams: SystemStreams + exclusiveSystemStreams: SystemStreams, + selectiveExecution: Boolean ) = new EvaluatorImpl( home, workspace, @@ -114,7 +116,8 @@ private[mill] object EvaluatorImpl { systemExit, exclusiveSystemStreams, chromeProfileLogger = new ChromeProfileLogger(outPath / millChromeProfile), - profileLogger = new ProfileLogger(outPath / millProfile) + profileLogger = new ProfileLogger(outPath / millProfile), + selectiveExecution = selectiveExecution ) class EvalOrThrow(evaluator: Evaluator, exceptionFactory: Evaluator.Results => Throwable) diff --git a/main/src/mill/main/MainModule.scala b/main/src/mill/main/MainModule.scala index ebcad880e70..28c02867665 100644 --- a/main/src/mill/main/MainModule.scala +++ b/main/src/mill/main/MainModule.scala @@ -61,7 +61,8 @@ object MainModule { RunScript.evaluateTasksNamed( evaluator.withBaseLogger(redirectLogger), targets, - Separated + Separated, + selectiveExecution = evaluator.selectiveExecution ) match { case Left(err) => Result.Failure(err) case Right((watched, Left(err))) => diff --git a/main/src/mill/main/RunScript.scala b/main/src/mill/main/RunScript.scala index 0c97e44bd75..a1eedbfe6b4 100644 --- a/main/src/mill/main/RunScript.scala +++ b/main/src/mill/main/RunScript.scala @@ -35,44 +35,47 @@ object RunScript { ): Either[ String, (Seq[Watchable], Either[String, Seq[(Any, Option[(TaskName, ujson.Value)])]]) - ] = { + ] = mill.eval.Evaluator.currentEvaluator.withValue(evaluator) { + val enableSelective = selectiveExecution && os.exists(evaluator.outPath / OutFiles.millSelectiveExecution) val selectedTargetsOrErr = - if (selectiveExecution && os.exists(evaluator.outPath / OutFiles.millSelectiveExecution)) { + if (enableSelective) { SelectiveExecution.resolve0(evaluator, scriptArgs).map(_.flatMap(Array("+", _)).drop(1)) } else { Right(scriptArgs.toArray) } selectedTargetsOrErr.flatMap { resolvedTaskNames => - val resolved = mill.eval.Evaluator.currentEvaluator.withValue(evaluator) { - Resolve.Tasks.resolve( + if (enableSelective && resolvedTaskNames.isEmpty) Right((Nil, Right(Nil))) + else { + val resolved = Resolve.Tasks.resolve( evaluator.rootModule, resolvedTaskNames, selectMode, evaluator.allowPositionalCommandArgs ) - } - resolved.map { t => - val evaluated = evaluateNamed0(evaluator, Agg.from(t)) - if (selectiveExecution) { - for (res <- evaluated._2) { - val (results, terminals, _) = res - val allInputHashes = results - .iterator - .collect { - case (t: InputImpl[_], TaskResult(Result.Success(Val(value)), _)) => - (terminals(t).render, value.##) - } - .toMap - SelectiveExecution.saveMetadata( - evaluator, - SelectiveExecution.Metadata(allInputHashes, evaluator.methodCodeHashSignatures) - ) + + resolved.map { t => + val evaluated = evaluateNamed0(evaluator, Agg.from(t)) + if (selectiveExecution) { + for (res <- evaluated._2) { + val (results, terminals, _) = res + val allInputHashes = results + .iterator + .collect { + case (t: InputImpl[_], TaskResult(Result.Success(Val(value)), _)) => + (terminals(t).render, value.##) + } + .toMap + SelectiveExecution.saveMetadata( + evaluator, + SelectiveExecution.Metadata(allInputHashes, evaluator.methodCodeHashSignatures) + ) + } } + val (ws, either) = evaluated + (ws, either.map { case (r, t, v) => v }) } - val (ws, either) = evaluated - (ws, either.map { case (r, t, v) => v }) } } } diff --git a/runner/src/mill/runner/MillBuildBootstrap.scala b/runner/src/mill/runner/MillBuildBootstrap.scala index 1c1bff77e07..79735a66b74 100644 --- a/runner/src/mill/runner/MillBuildBootstrap.scala +++ b/runner/src/mill/runner/MillBuildBootstrap.scala @@ -353,7 +353,8 @@ class MillBuildBootstrap( disableCallgraph = disableCallgraph, allowPositionalCommandArgs = allowPositionalCommandArgs, systemExit = systemExit, - exclusiveSystemStreams = streams0 + exclusiveSystemStreams = streams0, + selectiveExecution = selectiveExecution ) } diff --git a/testkit/src/mill/testkit/UnitTester.scala b/testkit/src/mill/testkit/UnitTester.scala index 5303b9c8804..d8e9cf791bc 100644 --- a/testkit/src/mill/testkit/UnitTester.scala +++ b/testkit/src/mill/testkit/UnitTester.scala @@ -102,7 +102,8 @@ class UnitTester( disableCallgraph = false, allowPositionalCommandArgs = false, systemExit = i => ???, - exclusiveSystemStreams = new SystemStreams(outStream, errStream, inStream) + exclusiveSystemStreams = new SystemStreams(outStream, errStream, inStream), + selectiveExecution = false ) def apply(args: String*): Either[Result.Failing[_], UnitTester.Result[Seq[_]]] = {