From 708491342d8db05df6081c5e2edfe4037285f66e Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sat, 13 Jan 2018 23:33:36 -0800 Subject: `traverse` seems to work, still missing `Command` discovery --- core/src/main/scala/mill/define/Cross.scala | 12 ++++----- core/src/main/scala/mill/define/Module.scala | 38 ++++++++++++++++++++-------- core/src/test/scala/mill/util/TestUtil.scala | 6 +++-- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/core/src/main/scala/mill/define/Cross.scala b/core/src/main/scala/mill/define/Cross.scala index b40dc844..6120d5df 100644 --- a/core/src/main/scala/mill/define/Cross.scala +++ b/core/src/main/scala/mill/define/Cross.scala @@ -44,13 +44,13 @@ object Cross{ */ class Cross[T](cases: Any*) (implicit ci: Cross.Factory[T], - ctx: mill.define.Ctx) extends mill.define.Module()(ctx) { + ctx: mill.define.Ctx, + cmds: Module.Cmds) extends mill.define.Module()(ctx, cmds) { - override def traverse[T](f: (Module, Segments) => Seq[T]): Seq[T] = { - ??? -// f(this) ++ -// this.reflect[mill.Module].flatMap(f) ++ -// items.map(_._2).flatMap{case t: mill.Module => f(t)} + override def traverse[T](f: Module => Seq[T]): Seq[T] = { + f(this) ++ + this.reflect[Module].flatMap(f) ++ + items.map(_._2).flatMap{case t: Module => f(t)} } val items = for(c0 <- cases.toList) yield{ diff --git a/core/src/main/scala/mill/define/Module.scala b/core/src/main/scala/mill/define/Module.scala index f4a332a2..f2ebe1f3 100644 --- a/core/src/main/scala/mill/define/Module.scala +++ b/core/src/main/scala/mill/define/Module.scala @@ -5,33 +5,47 @@ import java.lang.reflect.Modifier import ammonite.main.Router.{EntryPoint, Overrides} import ammonite.ops.Path -import scala.annotation.implicitNotFound +import scala.language.experimental.macros import scala.reflect.ClassTag - +import scala.reflect.macros.blackbox +object Module{ + case class Cmds(value: Seq[EntryPoint[Module]]) + object Cmds{ + implicit def make: Cmds = macro makeImpl + implicit def makeImpl(c: blackbox.Context): c.Expr[Cmds] = { + import c.universe._ + reify(Cmds(Nil)) + } + } +} /** * `Module` is a class meant to be extended by `trait`s *only*, in order to * propagate the implicit parameters forward to the final concrete * instantiation site so they can capture the enclosing/line information of * the concrete instance. */ -class Module(implicit ctx0: mill.define.Ctx) extends mill.moduledefs.Cacher{ - def commands: Seq[EntryPoint[Module]] = ??? +class Module(implicit ctx0: mill.define.Ctx, cmds: Module.Cmds) extends mill.moduledefs.Cacher{ + def commands: Seq[EntryPoint[Module]] = cmds.value - def traverse[T](f: (Module, Segments) => Seq[T]): Seq[T] = { - ??? + def traverse[T](f: Module => Seq[T]): Seq[T] = { + def rec(m: Module): Seq[T] = { + f(m) ++ + this.reflect[Module].flatMap(f) + } + rec(this) } - lazy val segmentsToModules = traverse{(m, s) => Seq(s -> m)} + lazy val segmentsToModules = traverse{m => Seq(m.ctx.segments -> m)} .toMap lazy val modules = segmentsToModules.valuesIterator.toSet - lazy val segmentsToTargets = traverse{(m, s) => m.reflect[Target[_]]} + lazy val segmentsToTargets = traverse{_.reflect[Target[_]]} .map(t => (t.ctx.segments, t)) .toMap lazy val targets = segmentsToTargets.valuesIterator.toSet lazy val segmentsToCommands = traverse{ - (m, s) => m.commands.map(e => s ++ Seq(Segment.Label(e.name)) -> e) + m => m.commands.map(e => m.ctx.segments ++ Seq(Segment.Label(e.name)) -> e) }.toMap def ctx = ctx0 @@ -66,7 +80,9 @@ class BaseModule(basePath: Path) (implicit millModuleEnclosing0: sourcecode.Enclosing, millModuleLine0: sourcecode.Line, millName0: sourcecode.Name, - overrides0: Overrides) + overrides0: Overrides, + cmds: Module.Cmds) extends Module()( - mill.define.Ctx.make(implicitly, implicitly, implicitly, BasePath(basePath), Segments(), implicitly) + mill.define.Ctx.make(implicitly, implicitly, implicitly, BasePath(basePath), Segments(), implicitly), + cmds ) \ No newline at end of file diff --git a/core/src/test/scala/mill/util/TestUtil.scala b/core/src/test/scala/mill/util/TestUtil.scala index c0985c97..7a3644dd 100644 --- a/core/src/test/scala/mill/util/TestUtil.scala +++ b/core/src/test/scala/mill/util/TestUtil.scala @@ -11,7 +11,8 @@ object TestUtil { class BaseModule(implicit millModuleEnclosing0: sourcecode.Enclosing, millModuleLine0: sourcecode.Line, millName0: sourcecode.Name, - overrides: Overrides) + overrides: Overrides, + cmds: Module.Cmds) extends Module()( mill.define.Ctx.make( implicitly, @@ -20,7 +21,8 @@ object TestUtil { BasePath(ammonite.ops.pwd / millModuleEnclosing0.value), Segments(), implicitly - ) + ), + cmds ) object test{ -- cgit v1.2.3