diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-01-13 22:18:05 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-01-13 22:18:05 -0800 |
commit | 7589250f87dd70e52cec923dcda5d554a4c7bbcb (patch) | |
tree | c88d42484ac82ee52790adb7fcde070baba19190 /core/src/main/scala | |
parent | 38ca4a5005dbaee94e674c41bb7262d603eec73c (diff) | |
download | mill-7589250f87dd70e52cec923dcda5d554a4c7bbcb.tar.gz mill-7589250f87dd70e52cec923dcda5d554a4c7bbcb.tar.bz2 mill-7589250f87dd70e52cec923dcda5d554a4c7bbcb.zip |
Remove targets generation in Mirror, in favor of just using java.reflect
Diffstat (limited to 'core/src/main/scala')
-rw-r--r-- | core/src/main/scala/mill/define/Module.scala | 9 | ||||
-rw-r--r-- | core/src/main/scala/mill/define/Task.scala | 1 | ||||
-rw-r--r-- | core/src/main/scala/mill/discover/Discovered.scala | 23 | ||||
-rw-r--r-- | core/src/main/scala/mill/discover/Mirror.scala | 9 | ||||
-rw-r--r-- | core/src/main/scala/mill/main/ReplApplyHandler.scala | 11 | ||||
-rw-r--r-- | core/src/main/scala/mill/main/Resolve.scala | 7 | ||||
-rw-r--r-- | core/src/main/scala/mill/util/AggWrapper.scala (renamed from core/src/main/scala/mill/util/OSet.scala) | 2 |
7 files changed, 23 insertions, 39 deletions
diff --git a/core/src/main/scala/mill/define/Module.scala b/core/src/main/scala/mill/define/Module.scala index c9240fa0..064741c4 100644 --- a/core/src/main/scala/mill/define/Module.scala +++ b/core/src/main/scala/mill/define/Module.scala @@ -4,6 +4,7 @@ import ammonite.main.Router.Overrides import ammonite.ops.Path import scala.annotation.implicitNotFound +import scala.reflect.ClassTag sealed trait Segment object Segment{ @@ -55,6 +56,14 @@ class Module(implicit ctx0: Module.Ctx) extends mill.moduledefs.Cacher{ implicit def millModuleSegments: Segments = { ctx.segments0 ++ Seq(ctx.segment) } + def reflect[T: ClassTag] = { + this + .getClass + .getMethods + .filter(_.getParameterCount == 0) + .filter(implicitly[ClassTag[T]].runtimeClass isAssignableFrom _.getReturnType) + .map(_.invoke(this).asInstanceOf[T]) + } } object Module{ @implicitNotFound("Modules, Targets and Commands can only be defined within a mill Module") diff --git a/core/src/main/scala/mill/define/Task.scala b/core/src/main/scala/mill/define/Task.scala index eac64cd5..2a670d21 100644 --- a/core/src/main/scala/mill/define/Task.scala +++ b/core/src/main/scala/mill/define/Task.scala @@ -42,6 +42,7 @@ abstract class Task[+T] extends Task.Ops[T] with Applyable[Task, T]{ trait NamedTask[+T] extends Task[T]{ def ctx: Module.Ctx + def label = ctx.segment match{case Segment.Label(v) => v} } trait Target[+T] extends NamedTask[T]{ override def asTarget = Some(this) diff --git a/core/src/main/scala/mill/discover/Discovered.scala b/core/src/main/scala/mill/discover/Discovered.scala index 98d4ebb9..6296645a 100644 --- a/core/src/main/scala/mill/discover/Discovered.scala +++ b/core/src/main/scala/mill/discover/Discovered.scala @@ -3,7 +3,6 @@ package mill.discover import mill.define._ import ammonite.main.Router import ammonite.main.Router.EntryPoint -import mill.discover.Mirror.TargetPoint import mill.util.Ctx.Loader import mill.util.Strict.Agg @@ -30,8 +29,7 @@ object Discovered { val targets = Agg.from( Mirror.traverseNode(base, mirror){ (mirror, segmentsRev, resolvedNode) => - for(target <- mirror.targets) - yield target.asInstanceOf[TargetPoint[Any, Any]].run(resolvedNode) + resolvedNode.asInstanceOf[mill.Module].reflect[Target[_]] } ) @@ -69,24 +67,6 @@ object Discovered { val r = new Router(c) - val targets = for { - m <- t.members.toList - if m.isMethod && - m.typeSignature.paramLists.isEmpty && - m.typeSignature.resultType <:< c.weakTypeOf[Target[_]] && - !m.name.toString.contains(' ') && - m.isPublic - } yield { - val x = Ident(TermName(c.freshName())) - val t = q"""mill.discover.Mirror.TargetPoint( - ${m.name.toString}, - ($x: ${m.typeSignature.resultType}) => $x.${m.name.toTermName} - )""" - - c.internal.setPos(t, m.pos) - t - } - val crossChildren = if (!(t <:< c.weakTypeOf[Cross[_]])) q"None" else { @@ -128,7 +108,6 @@ object Discovered { q"""mill.discover.Mirror[$baseType, $t]( $hierarchySelector, $commands, - $targets, $childHierarchies, $crossChildren )""" diff --git a/core/src/main/scala/mill/discover/Mirror.scala b/core/src/main/scala/mill/discover/Mirror.scala index bb6e44ad..ffa86225 100644 --- a/core/src/main/scala/mill/discover/Mirror.scala +++ b/core/src/main/scala/mill/discover/Mirror.scala @@ -15,13 +15,11 @@ import scala.language.experimental.macros */ case class Mirror[-T, V](node: (T, List[List[Any]]) => V, commands: Seq[EntryPoint[V]], - targets: Seq[Mirror.TargetPoint[V, _]], children: List[(String, Mirror[T, _])], crossChildren: Option[(V => List[List[Any]], Mirror[T, _])]) object Mirror{ - def traverseNode[T, V, R](t: T, hierarchy: Mirror[T, V]) (f: (Mirror[T, _], => Segments, => Any) => Seq[R]): Seq[R] = { traverse(t, hierarchy){ (mirror, segmentsRev) => @@ -48,11 +46,4 @@ object Mirror{ } rec(Nil, hierarchy) } - - - /** - * Represents metadata about a particular target, before the target is - * materialized for a concrete build - */ - case class TargetPoint[T, V](label: String, run: T => Target[V]) } diff --git a/core/src/main/scala/mill/main/ReplApplyHandler.scala b/core/src/main/scala/mill/main/ReplApplyHandler.scala index c239745a..718ca358 100644 --- a/core/src/main/scala/mill/main/ReplApplyHandler.scala +++ b/core/src/main/scala/mill/main/ReplApplyHandler.scala @@ -83,10 +83,13 @@ class ReplApplyHandler(pprinter0: pprint.PPrinter, evaluator: Evaluator[_]) exte c.argSignatures.map(s => s.name + ": " + s.typeString).mkString(", ") + ")()" }) ++ - (if (mirror.targets.isEmpty) Nil - else ctx.applyPrefixColor("\nTargets:").toString +: mirror.targets.sortBy(_.label).map(t => - "\n ." + t.label + "()" - )) + (if (m.reflect[Target[_]].isEmpty) Nil + else { + Seq(ctx.applyPrefixColor("\nTargets:").toString) ++ + m.reflect[Target[_]].sortBy(_.label).map(t => + "\n ." + t.label + "()" + ) + }) ) case t: mill.define.Target[_] if evaluator.mapping.targets.contains(t) => diff --git a/core/src/main/scala/mill/main/Resolve.scala b/core/src/main/scala/mill/main/Resolve.scala index 35b07a26..1567f26a 100644 --- a/core/src/main/scala/mill/main/Resolve.scala +++ b/core/src/main/scala/mill/main/Resolve.scala @@ -1,6 +1,6 @@ package mill.main -import mill.define.{Segment, Segments, Task} +import mill.define.{Segment, Segments, Target, Task} import mill.define.Task.TaskModule import mill.discover.Mirror import ammonite.main.Router @@ -17,9 +17,10 @@ object Resolve { case Segment.Cross(_) :: Nil => Left("Selector cannot start with a [cross] segment") case Segment.Label(last) :: Nil => def target = - hierarchy.targets + hierarchy.node(obj, remainingCrossSelectors).asInstanceOf[mill.Module] + .reflect[Target[_]] .find(_.label == last) - .map(x => Right(x.run(hierarchy.node(obj, remainingCrossSelectors)))) + .map(Right(_)) def invokeCommand[V](mirror: Mirror[T, V], name: String) = for{ cmd <- mirror.commands.find(_.name == name) diff --git a/core/src/main/scala/mill/util/OSet.scala b/core/src/main/scala/mill/util/AggWrapper.scala index d0f41777..c2994a9a 100644 --- a/core/src/main/scala/mill/util/OSet.scala +++ b/core/src/main/scala/mill/util/AggWrapper.scala @@ -35,7 +35,7 @@ sealed class AggWrapper(strictUniqueness: Boolean){ ) def apply[V](items: V*) = from(items) - def from[V](items: TraversableOnce[V]): Agg[V] = { + implicit def from[V](items: TraversableOnce[V]): Agg[V] = { val set = new Agg.Mutable[V]() items.foreach(set.append) set |