diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-12-20 08:00:16 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-12-20 08:00:16 -0800 |
commit | 5f200e4a1037c2ce477096a8da58561e86a58f30 (patch) | |
tree | 6f6928041d61d401b4dfc70ba763181b1805e5fe /core/src/main/scala | |
parent | b83546cacea1292af3d4ae8e8c349f529b201ed2 (diff) | |
download | mill-5f200e4a1037c2ce477096a8da58561e86a58f30.tar.gz mill-5f200e4a1037c2ce477096a8da58561e86a58f30.tar.bz2 mill-5f200e4a1037c2ce477096a8da58561e86a58f30.zip |
Remove implicit `Discovered.apply[T]` method. This forces people to pass around their generated `Discovered.make` structures by hand, making it harder to accidentally generate the same `Discovered` twice and slow down runtime/compilation
Diffstat (limited to 'core/src/main/scala')
-rw-r--r-- | core/src/main/scala/mill/Main.scala | 34 | ||||
-rw-r--r-- | core/src/main/scala/mill/discover/Discovered.scala | 27 | ||||
-rw-r--r-- | core/src/main/scala/mill/eval/Evaluator.scala | 3 | ||||
-rw-r--r-- | core/src/main/scala/mill/main/ReplApplyHandler.scala | 6 |
4 files changed, 39 insertions, 31 deletions
diff --git a/core/src/main/scala/mill/Main.scala b/core/src/main/scala/mill/Main.scala index 6a9d1945..86ee8649 100644 --- a/core/src/main/scala/mill/Main.scala +++ b/core/src/main/scala/mill/Main.scala @@ -39,13 +39,12 @@ object Main { } - def discoverMirror[T: Discovered](obj: T): Either[String, Discovered[T]] = { - val discovered = implicitly[Discovered[T]] - val consistencyErrors = Discovered.consistencyCheck(obj, discovered) + def consistencyCheck[T](mapping: Discovered.Mapping[T]): Either[String, Unit] = { + val consistencyErrors = Discovered.consistencyCheck(mapping) if (consistencyErrors.nonEmpty) { Left(s"Failed Discovered.consistencyCheck: ${consistencyErrors.map(Mirror.renderSelector)}") } else { - Right(discovered) + Right(()) } } @@ -77,10 +76,10 @@ object Main { } } - def apply[T: Discovered](args: Seq[String], - obj: T, - watch: Path => Unit, - coloredOutput: Boolean): Int = { + def apply[T](args: Seq[String], + mapping: Discovered.Mapping[T], + watch: Path => Unit, + coloredOutput: Boolean): Int = { val log = new PrintLogger(coloredOutput) @@ -88,13 +87,13 @@ object Main { val res = for { sel <- parseArgs(selectorString) - disc <- discoverMirror(obj) + _ <- consistencyCheck(mapping) crossSelectors = sel.map{ case Mirror.Segment.Cross(x) => x.toList.map(_.toString) case _ => Nil } - target <- mill.main.Resolve.resolve(sel, disc.mirror, obj, rest, crossSelectors, Nil) - evaluator = new Evaluator(pwd / 'out, Discovered.mapping(obj)(disc), log, sel) + target <- mill.main.Resolve.resolve(sel, mapping.mirror, mapping.base, rest, crossSelectors, Nil) + evaluator = new Evaluator(pwd / 'out, mapping.value, log, sel) _ <- evaluate(evaluator, target, watch).toLeft(()) } yield () @@ -119,20 +118,17 @@ object Main { s"""import $$file.^.build |import mill._ | - |val discovered = implicitly[mill.discover.Discovered[build.type]] + |val mapping = mill.discover.Discovered.mapping(build) | - |@main def run(args: String*) = mill.Main(args, build, interp.watch, true)(discovered) + |mill.Main.consistencyCheck(mapping).left.foreach(msg => throw new Exception(msg)) | - |@main def idea() = mill.scalaplugin.GenIdea(build)(discovered) + |@main def run(args: String*) = mill.Main(args, mapping, interp.watch, true) | - |val mirror = mill.Main.discoverMirror(build) match{ - | case Left(err) => throw new Exception("Failed discovery consistency check: " + err) - | case Right(mirror) => mirror - |} + |@main def idea() = mill.scalaplugin.GenIdea(mapping) | |val evaluator = new mill.eval.Evaluator( | ammonite.ops.pwd / 'out, - | mill.discover.Discovered.mapping(build)(mirror), + | mapping.value, | new mill.util.PrintLogger(true) |) | diff --git a/core/src/main/scala/mill/discover/Discovered.scala b/core/src/main/scala/mill/discover/Discovered.scala index 753ac141..0034a877 100644 --- a/core/src/main/scala/mill/discover/Discovered.scala +++ b/core/src/main/scala/mill/discover/Discovered.scala @@ -15,24 +15,37 @@ class Discovered[T](val mirror: Mirror[T, T]){ def targets(obj: T) = Mirror.traverse(obj, mirror) { (h, p) => h.labelled(obj, p) } + def mapping(t: T) = { + Discovered.Mapping( + targets(t).map(x => x.target -> x).toMap[Target[Any], LabelledTarget[_]], + mirror, + t + ) + } } object Discovered { - def consistencyCheck[T](base: T, d: Discovered[T]) = { + case class Mapping[T](value: Map[Target[Any], LabelledTarget[_]], + mirror: Mirror[T, T], + base: T) + + def consistencyCheck[T](mapping: Discovered.Mapping[T]) = { + val d = new Discovered(mapping.mirror) val inconsistent = for{ - (t1, t2) <- d.targets(base).zip(d.targets(base)) + (t1, t2) <- d.targets(mapping.base).zip(d.targets(mapping.base)) if t1.target ne t2.target } yield t1.segments inconsistent } - def mapping[T: Discovered](t: T): Map[Target[_], LabelledTarget[_]] = { - implicitly[Discovered[T]].targets(t).map(x => x.target -> x).toMap - } - - implicit def apply[T]: Discovered[T] = macro applyImpl[T] + def make[T]: Discovered[T] = macro applyImpl[T] + def mapping[T](t: T): Discovered.Mapping[T] = macro mappingImpl[T] def tupleLeft[T, V](items: List[(T, V)]) = items.map(_._1) + def mappingImpl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]): c.Expr[Discovered.Mapping[T]] = { + import c.universe._ + c.Expr[Discovered.Mapping[T]](q"${applyImpl[T](c)}.mapping($t)") + } def applyImpl[T: c.WeakTypeTag](c: Context): c.Expr[Discovered[T]] = { import c.universe._ diff --git a/core/src/main/scala/mill/eval/Evaluator.scala b/core/src/main/scala/mill/eval/Evaluator.scala index fab5dbe9..ea680ada 100644 --- a/core/src/main/scala/mill/eval/Evaluator.scala +++ b/core/src/main/scala/mill/eval/Evaluator.scala @@ -5,7 +5,7 @@ import java.net.URLClassLoader import ammonite.ops._ import ammonite.runtime.SpecialClassLoader import mill.define.{Graph, Target, Task, Worker} -import mill.discover.Mirror +import mill.discover.{Discovered, Mirror} import mill.discover.Mirror.LabelledTarget import mill.discover.Mirror.Segment.{Cross, Label} import mill.util @@ -19,6 +19,7 @@ class Evaluator(workspacePath: Path, sel: List[Mirror.Segment] = List(), classLoaderSig: Seq[(Path, Long)] = Evaluator.classLoaderSig){ val workerCache = mutable.Map.empty[Ctx.Loader[_], Any] + def evaluate(goals: OSet[Task[_]]): Evaluator.Results = { mkdir(workspacePath) diff --git a/core/src/main/scala/mill/main/ReplApplyHandler.scala b/core/src/main/scala/mill/main/ReplApplyHandler.scala index b2b688df..bb55c03f 100644 --- a/core/src/main/scala/mill/main/ReplApplyHandler.scala +++ b/core/src/main/scala/mill/main/ReplApplyHandler.scala @@ -1,12 +1,10 @@ package mill.main -import ammonite.ops.pwd -import mill.Main.discoverMirror + import mill.define.Applicative.ApplyHandler import mill.define.Task -import mill.discover.Discovered import mill.eval.Evaluator -import mill.util.{OSet, PrintLogger} +import mill.util.{OSet} class ReplApplyHandler(evaluator: Evaluator) extends ApplyHandler[Task] { // Evaluate classLoaderSig only once in the REPL to avoid busting caches |