diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-11-12 22:27:39 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-11-12 22:27:39 -0800 |
commit | ec4486c3700c421311d190c36e01faf2fd011e4e (patch) | |
tree | 04c7ac1ccb7874d3e73a7c48ce57e7a3b03b3f74 /core/src | |
parent | 05a501369c8483f9743e5cecf5e5ba0949d47ddc (diff) | |
download | mill-ec4486c3700c421311d190c36e01faf2fd011e4e.tar.gz mill-ec4486c3700c421311d190c36e01faf2fd011e4e.tar.bz2 mill-ec4486c3700c421311d190c36e01faf2fd011e4e.zip |
Add namespace hierarchy discovery to the `Discovered` macro, in preparation for IntelliJ support
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/main/scala/mill/discover/Discovered.scala | 35 | ||||
-rw-r--r-- | core/src/test/scala/mill/GraphTests.scala | 20 | ||||
-rw-r--r-- | core/src/test/scala/mill/JavaCompileJarTests.scala | 1 | ||||
-rw-r--r-- | core/src/test/scala/mill/TestUtil.scala | 1 |
4 files changed, 44 insertions, 13 deletions
diff --git a/core/src/main/scala/mill/discover/Discovered.scala b/core/src/main/scala/mill/discover/Discovered.scala index 3c89fbc2..3595f21f 100644 --- a/core/src/main/scala/mill/discover/Discovered.scala +++ b/core/src/main/scala/mill/discover/Discovered.scala @@ -8,10 +8,13 @@ import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context sealed trait Info[T, V] class Discovered[T](val targets: Seq[LabelInfo[T, _]], - val mains: Seq[CommandInfo[T, _]]){ + val mains: Seq[CommandInfo[T, _]], + val hierarchy: Hierarchy[T]){ def apply(t: T) = targets.map{case LabelInfo(a, f, b) => (a, f, b(t)) } } +case class Hierarchy[T](path: Seq[String], node: T => Any, children: List[Hierarchy[T]]) + case class Labelled[T](target: Task[T], format: Format[T], segments: Seq[String]) @@ -55,7 +58,8 @@ object Discovered { def applyImpl[T: c.WeakTypeTag](c: Context): c.Expr[Discovered[T]] = { import c.universe._ val tpe = c.weakTypeTag[T].tpe - def rec(segments: List[String], t: c.Type): (Seq[(Seq[String], Tree)], Seq[Seq[String]]) = { + def rec(segments: List[String], + t: c.Type): (Seq[(Seq[String], Tree)], Seq[Seq[String]], Tree) = { val r = new Router(c) val selfMains = @@ -75,19 +79,31 @@ object Discovered { if (m.typeSignature.resultType <:< c.weakTypeOf[Target[_]]) Seq(extendedSegments) else Nil - val (mains, children) = rec(extendedSegments, m.typeSignature) + val (mains, children, hierarchy) = rec(extendedSegments, m.typeSignature) + + val nonEmpty = mains.nonEmpty || children.nonEmpty + (mains, self ++ children, if (nonEmpty) Some(hierarchy) else None) + } - (mains, self ++ children) + val (mains, targets, childHierarchyOpts) = items.unzip3 + val childHierarchies = childHierarchyOpts.flatMap(_.toSeq).toList + val hierarchySelector = { + val base = q"${TermName(c.freshName())}" + val ident = segments.reverse.foldLeft[Tree](base) { (prefix, name) => + q"$prefix.${TermName(name)}" + } + q"($base: $tpe) => $ident" } - val (mains, targets) = items.unzip - Tuple2( + + Tuple3( selfMains ++ mains.flatten, - targets.flatten + targets.flatten, + q"mill.discover.Hierarchy[$tpe](${segments.reverse}, $hierarchySelector, $childHierarchies)" ) } - val (entryPoints, reversedPaths) = rec(Nil, tpe) + val (entryPoints, reversedPaths, hierarchy) = rec(Nil, tpe) val result = for(reversedPath <- reversedPaths.toList) yield { val base = q"${TermName(c.freshName())}" @@ -113,7 +129,8 @@ object Discovered { c.Expr[Discovered[T]](q""" new _root_.mill.discover.Discovered( $result, - ${nested.toList} + ${nested.toList}, + $hierarchy ) """) } diff --git a/core/src/test/scala/mill/GraphTests.scala b/core/src/test/scala/mill/GraphTests.scala index fe21a6de..a3eeb62f 100644 --- a/core/src/test/scala/mill/GraphTests.scala +++ b/core/src/test/scala/mill/GraphTests.scala @@ -3,7 +3,7 @@ package mill import utest._ import TestUtil.test import mill.define.Task -import mill.discover.Discovered +import mill.discover.{Discovered, Hierarchy} import mill.eval.Evaluator import mill.util.OSet @@ -31,13 +31,27 @@ object GraphTests extends TestSuite{ val classInstance = new CanNest } - val discovered = Discovered[outer.type].apply(outer).map(x => (x._1, x._3)) + val discovered = Discovered[outer.type] + + val mapped = discovered.apply(outer).map(x => (x._1, x._3)) + def flatten(h: Hierarchy[outer.type]): Seq[Any] = { + h.node(outer) :: h.children.flatMap(flatten) + } + val flattenedHierarchy = flatten(discovered.hierarchy) + + val expectedHierarchy = Seq( + outer, + outer.classInstance, + outer.nested, + ) + assert(flattenedHierarchy == expectedHierarchy) + val expected = Seq( (List("classInstance", "single"), outer.classInstance.single), (List("nested", "single"), outer.nested.single), (List("single"), outer.single) ) - assert(discovered == expected) + assert(mapped == expected) } 'failConsistencyChecks - { diff --git a/core/src/test/scala/mill/JavaCompileJarTests.scala b/core/src/test/scala/mill/JavaCompileJarTests.scala index 87fb55f0..39d3a93a 100644 --- a/core/src/test/scala/mill/JavaCompileJarTests.scala +++ b/core/src/test/scala/mill/JavaCompileJarTests.scala @@ -51,6 +51,7 @@ object JavaCompileJarTests extends TestSuite{ import Build._ val mapping = Discovered.mapping(Build) + def eval[T](t: Task[T]): (T, Int) = { val evaluator = new Evaluator(workspacePath, mapping) val evaluated = evaluator.evaluate(OSet(t)) diff --git a/core/src/test/scala/mill/TestUtil.scala b/core/src/test/scala/mill/TestUtil.scala index 59cda559..dfe0f3a0 100644 --- a/core/src/test/scala/mill/TestUtil.scala +++ b/core/src/test/scala/mill/TestUtil.scala @@ -34,5 +34,4 @@ object TestUtil { } } } - } |