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

Add ability to watch T.inputs and interp.watchValues #2489

Merged
merged 29 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion bsp/src/mill/bsp/BSP.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ object BSP extends ExternalModule with CoursierModule with BspServerStarter {
val worker = BspWorker(ctx)

worker match {
case Result.Success(worker) =>
case Result.Success(worker, _) =>
worker.startBspServer(
initialEvaluator,
streams,
Expand Down
2 changes: 1 addition & 1 deletion bsp/worker/src/mill/bsp/worker/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Utils {
task: mill.define.Task[_]
): StatusCode = {
results.results(task) match {
case Success(_) => StatusCode.OK
case Success(_, _) => StatusCode.OK
case Skipped => StatusCode.CANCELLED
case _ => StatusCode.ERROR
}
Expand Down
6 changes: 3 additions & 3 deletions main/api/src/mill/api/Ctx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ object Ctx {
* implementation of a `Task`.
*/
class Ctx(
val args: IndexedSeq[_],
val args: IndexedSeq[Result.Success[_]],
dest0: () => os.Path,
val log: Logger,
val home: os.Path,
Expand All @@ -127,8 +127,8 @@ class Ctx(
with Ctx.Workspace {

def dest: Path = dest0()
def arg[T](index: Int): T = {
if (index >= 0 && index < args.length) args(index).asInstanceOf[T]
def arg[T](index: Int): Result.Success[T] = {
if (index >= 0 && index < args.length) args(index).asInstanceOf[Result.Success[T]]
else throw new IndexOutOfBoundsException(s"Index $index outside of range 0 - ${args.length}")
}
}
7 changes: 5 additions & 2 deletions main/api/src/mill/api/Result.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ object Result {
* @param value The value computed by the task.
* @tparam T The result type of the computed task.
*/
case class Success[+T](value: T) extends Result[T] {
def map[V](f: T => V): Success[V] = Result.Success(f(value))
case class Success[+T](value: T, recalc: () => T) extends Result[T] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on the purpose of recalc and add some API doc?

I only have a fuzzy idea how I should fill the recalc arg of a newly created Success.

def map[V](f: T => V): Success[V] = Result.Success(f(value), () => f(recalc()))
def flatMap[V](f: T => Result[V]): Result[V] = f(value)
override def asSuccess: Option[Success[T]] = Some(this)
}
object Success{
def apply[T](value: => T) = new Success(value, () => value)
}

/**
* A task execution was skipped because of failures in it's dependencies.
Expand Down
12 changes: 7 additions & 5 deletions main/define/src/mill/define/Task.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object Task {
val inputs = inputs0
def evaluate(ctx: mill.api.Ctx) = {
for (i <- 0 until ctx.args.length)
yield ctx.args(i).asInstanceOf[T]
yield ctx.arg[T](i).value
}
}
private[define] class TraverseCtx[+T, V](
Expand All @@ -69,17 +69,19 @@ object Task {
def evaluate(ctx: mill.api.Ctx) = {
f(
for (i <- 0 until ctx.args.length)
yield ctx.args(i).asInstanceOf[T],
yield ctx.arg[T](i).value,
ctx
)
}
}
private[define] class Mapped[+T, +V](source: Task[T], f: T => V) extends Task[V] {
def evaluate(ctx: mill.api.Ctx) = f(ctx.arg(0))
def evaluate(ctx: mill.api.Ctx) = ctx.arg(0).map(f)
val inputs = List(source)
}
private[define] class Zipped[+T, +V](source1: Task[T], source2: Task[V]) extends Task[(T, V)] {
def evaluate(ctx: mill.api.Ctx) = (ctx.arg(0), ctx.arg(1))
def evaluate(ctx: mill.api.Ctx) = (ctx.arg[T](0), ctx.arg[V](1)) match{
case (Result.Success(a, _), Result.Success(b, _)) => (a, b)
}
val inputs = List(source1, source2)
}
}
Expand All @@ -105,7 +107,7 @@ trait NamedTask[+T] extends Task[T] {
}
override def toString = ctx.segments.render

def evaluate(ctx: mill.api.Ctx) = ctx.arg[T](0)
def evaluate(ctx: mill.api.Ctx): Result[T] = ctx.arg[T](0)

val ctx = ctx0.withSegments(segments = ctx0.segments ++ Seq(ctx0.segment))
val inputs = Seq(t)
Expand Down
10 changes: 5 additions & 5 deletions main/eval/src/mill/eval/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ class Evaluator private (
upToDateWorker.map((_, inputsHash)) orElse cached match {
case Some((v, hashCode)) =>
val newResults = mutable.LinkedHashMap.empty[Task[_], mill.api.Result[(Any, Int)]]
newResults(labelledNamedTask.task) = mill.api.Result.Success((v, hashCode))
newResults(labelledNamedTask.task) = Result.Success((v, hashCode), () => (v, hashCode))

Evaluated(newResults, Nil, cached = true)

Expand Down Expand Up @@ -452,7 +452,7 @@ class Evaluator private (
case mill.api.Result.Failure(_, Some((v, _))) =>
handleTaskResult(v, v.##, paths.meta, inputsHash, labelledNamedTask)

case mill.api.Result.Success((v, _)) =>
case mill.api.Result.Success((v, _), _) =>
handleTaskResult(v, v.##, paths.meta, inputsHash, labelledNamedTask)

case _ =>
Expand Down Expand Up @@ -557,13 +557,13 @@ class Evaluator private (
newEvaluated.append(task)
val targetInputValues = task.inputs
.map { x => newResults.getOrElse(x, results(x)) }
.collect { case mill.api.Result.Success((v, _)) => v }
.collect { case Result.Success((v, _), sig) => Result.Success(v, sig) }

val res =
if (targetInputValues.length != task.inputs.length) mill.api.Result.Skipped
else {
val args = new Ctx(
args = targetInputValues.toArray[Any].toIndexedSeq,
args = targetInputValues.toArray[Result.Success[Any]].toIndexedSeq,
dest0 = () =>
paths match {
case Some(dest) =>
Expand Down Expand Up @@ -771,7 +771,7 @@ object Evaluator {
failing: MultiBiMap[Either[Task[_], Labelled[_]], mill.api.Result.Failing[_]],
results: collection.Map[Task[_], mill.api.Result[Any]]
) {
def values: Seq[Any] = rawValues.collect { case mill.api.Result.Success(v) => v }
def values: Seq[Any] = rawValues.collect { case mill.api.Result.Success(v, _) => v }
private def copy(
rawValues: Seq[Result[Any]] = rawValues,
evaluated: Agg[Task[_]] = evaluated,
Expand Down
140 changes: 72 additions & 68 deletions main/src/mill/main/MainModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package mill.main

import java.util.concurrent.LinkedBlockingQueue
import mill.{BuildInfo, T}
import mill.api.{Ctx, PathRef, Result, internal}
import mill.define.{Command, Segments, NamedTask, TargetImpl, Task}
import mill.api.{Ctx, Logger, PathRef, Result, internal}
import mill.define.{Command, NamedTask, Segments, TargetImpl, Task}
import mill.eval.{Evaluator, EvaluatorPaths}
import mill.util.{PrintLogger, Watched}
import mill.main.SelectMode.Separated
import mill.util.{PrintLogger, Watchable}
import pprint.{Renderer, Tree, Truncated}
import ujson.Value

Expand All @@ -25,32 +26,33 @@ object MainModule {
}
}

def evaluateTasks[T](
evaluator: Evaluator,
targets: Seq[String],
selectMode: SelectMode
)(f: Seq[(Any, Option[ujson.Value])] => T): Result[Watched[Unit]] = {
RunScript.evaluateTasks(evaluator, targets, selectMode) match {
private def show0(evaluator: Evaluator,
targets: Seq[String],
log: Logger,
watch0: Watchable => Unit)
(f: Seq[(Any, Option[(RunScript.TaskName, ujson.Value)])] => ujson.Value) = {
RunScript.evaluateTasksNamed(
evaluator.withBaseLogger(
// When using `show`, redirect all stdout of the evaluated tasks so the
// printed JSON is the only thing printed to stdout.
evaluator.baseLogger match {
case p: PrintLogger => p.withOutStream(p.errorStream)
case l => l
}
),
targets,
Separated
) match {
case Left(err) => Result.Failure(err)
case Right((watched, Left(err))) => Result.Failure(err, Some(Watched((), watched)))
case Right((watched, Right(res))) =>
f(res)
Result.Success(Watched((), watched))
}
}
case Right((watched, Left(err))) =>
watched.foreach(watch0)
Result.Failure(err)

@internal
def evaluateTasksNamed[T](
evaluator: Evaluator,
targets: Seq[String],
selectMode: SelectMode
)(f: Seq[(Any, Option[(RunScript.TaskName, ujson.Value)])] => T): Result[Watched[Option[T]]] = {
RunScript.evaluateTasksNamed(evaluator, targets, selectMode) match {
case Left(err) => Result.Failure(err)
case Right((watched, Left(err))) => Result.Failure(err, Some(Watched(None, watched)))
case Right((watched, Right(res))) =>
val fRes = f(res)
Result.Success(Watched(Some(fRes), watched))
val output = f(res)
watched.foreach(watch0)
log.outputStream.println(output.render(indent = 2))
Result.Success(output)
}
}
}
Expand All @@ -60,6 +62,37 @@ object MainModule {
* [[show]], [[inspect]], [[plan]], etc.
*/
trait MainModule extends mill.Module {
protected[mill] val watchedValues = mutable.Buffer.empty[Watchable]
protected[mill] val evalWatchedValues = mutable.Buffer.empty[Watchable]

object interp {

def watchValue[T](v0: => T)(implicit fn: sourcecode.FileName, ln: sourcecode.Line): T = {
val v = v0
val watchable = Watchable.Value(
() => v0.hashCode,
v.hashCode(),
fn.value + ":" + ln.value
)
watchedValues.append(watchable)
v
}

def watch(p: os.Path): os.Path = {
val watchable = Watchable.Path(PathRef(p))
watchedValues.append(watchable)
p
}

def watch0(w: Watchable): Unit = {
watchedValues.append(w)
}

def evalWatch0(w: Watchable): Unit = {
evalWatchedValues.append(w)
}
}


implicit def millDiscover: mill.define.Discover[_]

Expand Down Expand Up @@ -246,26 +279,11 @@ trait MainModule extends mill.Module {
* to integrate Mill into external scripts and tooling.
*/
def show(evaluator: Evaluator, targets: String*): Command[ujson.Value] = T.command {
MainModule.evaluateTasksNamed(
evaluator.withBaseLogger(
// When using `show`, redirect all stdout of the evaluated tasks so the
// printed JSON is the only thing printed to stdout.
evaluator.baseLogger match {
case p: PrintLogger => p.withOutStream(p.errorStream)
case l => l
}
),
targets,
SelectMode.Separated
) { res: Seq[(Any, Option[(String, ujson.Value)])] =>
val jsons = res.flatMap(_._2).map(_._2)
val output: ujson.Value =
if (jsons.size == 1) jsons.head
else { ujson.Arr.from(jsons) }
T.log.outputStream.println(output.render(indent = 2))
output
}.map { res: Watched[Option[Value]] =>
res.value.getOrElse(ujson.Null)
MainModule.show0(evaluator, targets, T.log, interp.evalWatch0){ res =>
res.flatMap(_._2).map(_._2) match{
case Seq(single) => single
case multiple => multiple
}
}
}

Expand All @@ -274,24 +292,8 @@ trait MainModule extends mill.Module {
* to integrate Mill into external scripts and tooling.
*/
def showNamed(evaluator: Evaluator, targets: String*): Command[ujson.Value] = T.command {
MainModule.evaluateTasksNamed(
evaluator.withBaseLogger(
// When using `show`, redirect all stdout of the evaluated tasks so the
// printed JSON is the only thing printed to stdout.
evaluator.baseLogger match {
case p: PrintLogger => p.withOutStream(outStream = p.errorStream)
case l => l
}
),
targets,
SelectMode.Separated
) { res: Seq[(Any, Option[(String, ujson.Value)])] =>
val nameAndJson = res.flatMap(_._2)
val output: ujson.Value = ujson.Obj.from(nameAndJson)
T.log.outputStream.println(output.render(indent = 2))
output
}.map { res: Watched[Option[Value]] =>
res.value.getOrElse(ujson.Null)
MainModule.show0(evaluator, targets, T.log, interp.evalWatch0) { res =>
ujson.Obj.from(res.flatMap(_._2))
}
}

Expand Down Expand Up @@ -382,17 +384,19 @@ trait MainModule extends mill.Module {
}

/**
* The `init`` command generates a project based on a Giter8 template. It
* The `init` command generates a project based on a Giter8 template. It
* prompts you to enter project name and creates a folder with that name.
* You can use it to quickly generate a starter project. There are lots of
* templates out there for many frameworks and tools!
*/
def init(evaluator: Evaluator, args: String*): Command[Unit] = T.command {
MainModule.evaluateTasks(
RunScript.evaluateTasksNamed(
evaluator,
Seq("mill.scalalib.giter8.Giter8Module/init") ++ args,
selectMode = SelectMode.Single
)(identity).map(_.value)
SelectMode.Single
)

()
}

private type VizWorker = (
Expand Down
21 changes: 2 additions & 19 deletions main/src/mill/main/RootModule.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package mill.main

import mill.api.{PathRef, internal}
import mill.define.{Caller, Discover, Segments, Watchable}
import mill.util.Watchable
import mill.define.{Caller, Discover, Segments}
import TokenReaders._

import scala.collection.mutable
Expand Down Expand Up @@ -36,24 +37,6 @@ abstract class RootModule()(implicit
// user-defined BaseModule can have a complete Discover[_] instance without
// needing to tediously call `override lazy val millDiscover = Discover[this.type]`
override lazy val millDiscover = baseModuleInfo.discover.asInstanceOf[Discover[this.type]]

object interp {

def watchValue[T](v0: => T): T = {
val v = v0
val watchable = Watchable.Value(() => v0.hashCode, v.hashCode())
watchedValues.append(watchable)
v
}

def watch(p: os.Path): os.Path = {
val watchable = Watchable.Path(PathRef(p))
watchedValues.append(watchable)
p
}
}

protected[mill] val watchedValues = mutable.Buffer.empty[Watchable]
}

@internal
Expand Down
Loading