diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/mill/define/Cross.scala | 45 | ||||
-rw-r--r-- | core/src/main/scala/mill/discover/Discovered.scala | 6 | ||||
-rw-r--r-- | core/src/main/scala/mill/main/ReplApplyHandler.scala | 5 | ||||
-rw-r--r-- | core/src/main/scala/mill/package.scala | 2 | ||||
-rw-r--r-- | core/src/test/scala/mill/define/CrossTests.scala | 138 | ||||
-rw-r--r-- | core/src/test/scala/mill/discover/CrossModuleTests.scala | 11 | ||||
-rw-r--r-- | core/src/test/scala/mill/util/TestGraphs.scala | 26 |
7 files changed, 33 insertions, 200 deletions
diff --git a/core/src/main/scala/mill/define/Cross.scala b/core/src/main/scala/mill/define/Cross.scala index 4b0e1b8f..72424bde 100644 --- a/core/src/main/scala/mill/define/Cross.scala +++ b/core/src/main/scala/mill/define/Cross.scala @@ -1,35 +1,9 @@ package mill.define import language.experimental.macros -import scala.reflect.ClassTag -import scala.reflect.macros.{Context, blackbox} +import scala.reflect.macros.blackbox -case class Cross[+T](items: List[(List[Any], T)])(implicit val e: sourcecode.Enclosing, val l: sourcecode.Line){ - def flatMap[V](f: T => Cross[V]): Cross[V] = new Cross( - items.flatMap{ - case (l, v) => f(v).items.map{case (l2, v2) => (l2 ::: l, v2)} - } - ) - def map[V](f: T => V): Cross[V] = new Cross(items.map{case (l, v) => (l, f(v))}) - def withFilter(f: T => Boolean): Cross[T] = new Cross(items.filter(t => f(t._2))) - def applyOpt(input: Any*): Option[T] = { - val inputList = input.toList - items.find(_._1 == inputList).map(_._2) - } - def apply(input: Any*): T = { - applyOpt(input:_*).getOrElse( - throw new Exception( - "Unknown set of cross values: " + input + - " not in known values\n" + items.map(_._1).mkString("\n") - ) - ) - } -} object Cross{ - def apply[T](t: T*) = new Cross(t.map(i => List(i) -> i).toList) -} - -object CrossModule{ def autoCast[A](x: Any): A = x.asInstanceOf[A] abstract class Implicit[T]{ def make(v: Any, ctx: Module.Ctx): T @@ -47,7 +21,7 @@ object CrossModule{ val tree = primaryConstructorArgs match{ case List(arg) => q""" - new mill.define.CrossModule.Implicit[$tpe]{ + new mill.define.Cross.Implicit[$tpe]{ def make(v: Any, ctx0: mill.define.Module.Ctx) = new $tpe(v.asInstanceOf[${arg.info}]){ override def ctx = ctx0 } @@ -59,7 +33,7 @@ object CrossModule{ q"v.asInstanceOf[scala.Product].productElement($n).asInstanceOf[${a.info}]" } q""" - new mill.define.CrossModule.Implicit[$tpe]{ + new mill.define.Cross.Implicit[$tpe]{ def make(v: Any, ctx0: mill.define.Module.Ctx) = new $tpe(..$argTupleValues){ override def ctx = ctx0 } @@ -72,11 +46,10 @@ object CrossModule{ } } } -class CrossModule[T](cases: Any*) - (implicit ci: CrossModule.Implicit[T], - ctx: Module.Ctx) -extends Cross[T]({ - for(c <- cases.toList) yield{ +class Cross[T](cases: Any*) + (implicit ci: Cross.Implicit[T], + val ctx: Module.Ctx){ + val items = for(c <- cases.toList) yield{ val crossValues = ci.crossValues(c) val sub = ci.make( c, @@ -87,4 +60,6 @@ extends Cross[T]({ ) (crossValues.reverse, sub) } -})
\ No newline at end of file + val itemMap = items.toMap + def apply(args: Any*) = itemMap(args.toList) +}
\ No newline at end of file diff --git a/core/src/main/scala/mill/discover/Discovered.scala b/core/src/main/scala/mill/discover/Discovered.scala index 74312f91..49d2db4a 100644 --- a/core/src/main/scala/mill/discover/Discovered.scala +++ b/core/src/main/scala/mill/discover/Discovered.scala @@ -103,10 +103,10 @@ object Discovered { } val crossChildren = - if (!(t <:< c.weakTypeOf[Cross[Module]])) q"None" + if (!(t <:< c.weakTypeOf[Cross[_]])) q"None" else { - val TypeRef(_, _, Seq(arg)) = t.baseType(typeOf[Cross[Module]].typeSymbol) + val TypeRef(_, _, Seq(arg)) = t.baseType(weakTypeOf[Cross[_]].typeSymbol) val innerMirror = rec(None :: segments, arg) q"Some(((c: mill.define.Cross[_]) => mill.discover.Discovered.tupleLeft(c.items), $innerMirror))" } @@ -114,7 +114,7 @@ object Discovered { m <- t.members.toList if m.typeSignature.paramLists.isEmpty && m.isPublic if (m.typeSignature.finalResultType <:< c.weakTypeOf[Module]) || - (m.typeSignature.finalResultType <:< c.weakTypeOf[Cross[Module]]) + (m.typeSignature.finalResultType <:< c.weakTypeOf[Cross[_]]) } yield { val name = m.name.toString.trim diff --git a/core/src/main/scala/mill/main/ReplApplyHandler.scala b/core/src/main/scala/mill/main/ReplApplyHandler.scala index 77926d45..0bcf8e6e 100644 --- a/core/src/main/scala/mill/main/ReplApplyHandler.scala +++ b/core/src/main/scala/mill/main/ReplApplyHandler.scala @@ -2,8 +2,7 @@ package mill.main import mill.define.Applicative.ApplyHandler -import mill.define.{Cross, Target, Task} -import mill.define.Segment +import mill.define._ import mill.discover.{Discovered, Mirror} import mill.eval.Evaluator import mill.util.OSet @@ -43,7 +42,7 @@ class ReplApplyHandler(pprinter0: pprint.PPrinter, evaluator: Evaluator[_]) exte val millHandlers: PartialFunction[Any, pprint.Tree] = { case c: Cross[_] => pprint.Tree.Lazy( ctx => - Iterator(c.e.value, ":", c.l.value.toString, ctx.applyPrefixColor("\nChildren:").toString) ++ + Iterator(c.ctx.enclosing , ":", c.ctx.lineNum.toString, ctx.applyPrefixColor("\nChildren:").toString) ++ c.items.iterator.map(x => "\n (" + x._1.map(pprint.PPrinter.BlackWhite.apply(_)).mkString(", ") + ")" ) diff --git a/core/src/main/scala/mill/package.scala b/core/src/main/scala/mill/package.scala index b578ed3b..3463b63f 100644 --- a/core/src/main/scala/mill/package.scala +++ b/core/src/main/scala/mill/package.scala @@ -7,5 +7,5 @@ package object mill extends JsonFormatters{ type PathRef = mill.eval.PathRef type Module = define.Module val Module = define.Module - type CrossModule[T] = define.CrossModule[T] + type Cross[T] = define.Cross[T] } diff --git a/core/src/test/scala/mill/define/CrossTests.scala b/core/src/test/scala/mill/define/CrossTests.scala deleted file mode 100644 index 5c0f5c08..00000000 --- a/core/src/test/scala/mill/define/CrossTests.scala +++ /dev/null @@ -1,138 +0,0 @@ -package mill.define - -import utest._ - -object CrossTests extends TestSuite{ - - val tests = Tests{ - - def assertEquals[T](value: Cross[T], value1: Cross[T]) = { - assert(value == value1) - } - 'single - assertEquals( - for(a <- Cross(1, 2, 3)) yield a.toString, - Cross(List((List(1), "1"), (List(2), "2"), (List(3), "3"))) - ) - - 'double - assertEquals( - for{ - a <- Cross(1, 2, 3) - b <- Cross("A", "B", "C") - } yield b * a, - Cross( - List( - (List("A", 1), "A"), - (List("B", 1), "B"), - (List("C", 1), "C"), - (List("A", 2), "AA"), - (List("B", 2), "BB"), - (List("C", 2), "CC"), - (List("A", 3), "AAA"), - (List("B", 3), "BBB"), - (List("C", 3), "CCC") - ) - ) - ) - - - 'triple - assertEquals( - for{ - a <- Cross(1, 2) - b <- Cross("A", "B") - c <- Cross(true, false) - } yield b * a + c, - Cross( - List( - (List(true, "A", 1), "Atrue"), - (List(false, "A", 1), "Afalse"), - (List(true, "B", 1), "Btrue"), - (List(false, "B", 1), "Bfalse"), - (List(true, "A", 2), "AAtrue"), - (List(false, "A", 2), "AAfalse"), - (List(true, "B", 2), "BBtrue"), - (List(false, "B", 2), "BBfalse") - ) - ) - ) - - - 'filter - assertEquals( - for{ - a <- Cross(1, 2, 3) - b <- Cross("A", "B", "C") - if !(a == 2 && b == "B") - } yield b * a, - Cross( - List( - (List("A", 1), "A"), - (List("B", 1), "B"), - (List("C", 1), "C"), - (List("A", 2), "AA"), - (List("C", 2), "CC"), - (List("A", 3), "AAA"), - (List("B", 3), "BBB"), - (List("C", 3), "CCC") - ) - ) - ) - - - 'middleFilter- assertEquals( - for{ - a <- Cross(1, 2, 3) - if a != 2 - b <- Cross("A", "B", "C") - } yield b * a, - Cross( - List( - (List("A", 1), "A"), - (List("B", 1), "B"), - (List("C", 1), "C"), - (List("A", 3), "AAA"), - (List("B", 3), "BBB"), - (List("C", 3), "CCC") - ) - ) - ) - - - 'nestedComprehension1 - assertEquals( - for{ - (a, b) <- for(a <- Cross(1, 2); b <- Cross("A", "B")) yield (a, b) - c <- Cross(true, false) - } yield b * a + c, - Cross( - List( - (List(true, "A", 1), "Atrue"), - (List(false, "A", 1), "Afalse"), - (List(true, "B", 1), "Btrue"), - (List(false, "B", 1), "Bfalse"), - (List(true, "A", 2), "AAtrue"), - (List(false, "A", 2), "AAfalse"), - (List(true, "B", 2), "BBtrue"), - (List(false, "B", 2), "BBfalse") - ) - ) - ) - - 'nestedComprehension2 - assertEquals( - for{ - a <- Cross(1, 2) - (b, c) <- for(b <- Cross("A", "B"); c <- Cross(true, false)) yield (b, c) - } yield b * a + c, - Cross( - List( - (List(true, "A", 1), "Atrue"), - (List(false, "A", 1), "Afalse"), - (List(true, "B", 1), "Btrue"), - (List(false, "B", 1), "Bfalse"), - (List(true, "A", 2), "AAtrue"), - (List(false, "A", 2), "AAfalse"), - (List(true, "B", 2), "BBtrue"), - (List(false, "B", 2), "BBfalse") - ) - ) - ) - - } -} diff --git a/core/src/test/scala/mill/discover/CrossModuleTests.scala b/core/src/test/scala/mill/discover/CrossModuleTests.scala index ff3b52b1..9848b9f4 100644 --- a/core/src/test/scala/mill/discover/CrossModuleTests.scala +++ b/core/src/test/scala/mill/discover/CrossModuleTests.scala @@ -1,20 +1,17 @@ package mill.discover -import mill.{Module, T} -import mill.define.Cross import mill.define.Segment import mill.define.Segment.Label -import mill.util.TestUtil.test import utest._ import mill.util.TestGraphs._ import mill.util.TestUtil -object CrossModuleTests extends TestSuite{ +object CrossTests extends TestSuite{ val tests = Tests{ 'cross - { object outer extends TestUtil.BaseModule { - object crossed extends mill.CrossModule[CrossedModule]("2.10.6", "2.11.8", "2.12.4") + object crossed extends mill.Cross[CrossedModule]("2.10.6", "2.11.8", "2.12.4") class CrossedModule(n: String) extends mill.Module{ def scalaVersion = n } @@ -42,8 +39,8 @@ object CrossModuleTests extends TestSuite{ scalaVersion <- Seq("2.10.6", "2.11.8", "2.12.4") if !(platform == "native0.3" && scalaVersion == "2.10.6") } yield (platform, scalaVersion) - object crossed extends mill.CrossModule[CrossModule](crossMatrix:_*) - case class CrossModule(platform: String, scalaVersion: String) extends mill.Module{ + object crossed extends mill.Cross[Cross](crossMatrix:_*) + case class Cross(platform: String, scalaVersion: String) extends mill.Module{ def suffix = Seq(scalaVersion, platform).filter(_.nonEmpty).map("_"+_).mkString } } diff --git a/core/src/test/scala/mill/util/TestGraphs.scala b/core/src/test/scala/mill/util/TestGraphs.scala index 3ddae02d..f2c67583 100644 --- a/core/src/test/scala/mill/util/TestGraphs.scala +++ b/core/src/test/scala/mill/util/TestGraphs.scala @@ -1,6 +1,6 @@ package mill.util import TestUtil.test -import mill.define.CrossModule +import mill.define.Cross import mill.{Module, T} /** @@ -192,8 +192,8 @@ object TestGraphs{ object singleCross extends TestUtil.BaseModule { - object cross extends mill.CrossModule[CrossModule]("210", "211", "212") - class CrossModule(scalaVersion: String) extends Module{ + object cross extends mill.Cross[Cross]("210", "211", "212") + class Cross(scalaVersion: String) extends Module{ def suffix = T{ scalaVersion } } } @@ -203,27 +203,27 @@ object TestGraphs{ platform <- Seq("jvm", "js", "native") if !(platform == "native" && scalaVersion != "212") } yield (scalaVersion, platform) - object cross extends mill.CrossModule[CrossModule](crossMatrix:_*) - class CrossModule(scalaVersion: String, platform: String) extends Module{ + object cross extends mill.Cross[Cross](crossMatrix:_*) + class Cross(scalaVersion: String, platform: String) extends Module{ def suffix = T{ scalaVersion + "_" + platform } } } object indirectNestedCrosses extends TestUtil.BaseModule { - object cross extends mill.CrossModule[CrossModule]("210", "211", "212") - class CrossModule(scalaVersion: String) extends mill.Module{ - object cross2 extends mill.CrossModule[CrossModule]("jvm", "js", "native") - class CrossModule(platform: String) extends mill.Module{ + object cross extends mill.Cross[Cross]("210", "211", "212") + class Cross(scalaVersion: String) extends mill.Module{ + object cross2 extends mill.Cross[Cross]("jvm", "js", "native") + class Cross(platform: String) extends mill.Module{ def suffix = T{ scalaVersion + "_" + platform } } } } object nestedCrosses extends TestUtil.BaseModule { - object cross extends mill.CrossModule[CrossModule]("210", "211", "212") - class CrossModule(scalaVersion: String) extends mill.Module{ - object cross2 extends mill.CrossModule[CrossModule]("jvm", "js", "native") - class CrossModule(platform: String) extends mill.Module{ + object cross extends mill.Cross[Cross]("210", "211", "212") + class Cross(scalaVersion: String) extends mill.Module{ + object cross2 extends mill.Cross[Cross]("jvm", "js", "native") + class Cross(platform: String) extends mill.Module{ def suffix = T{ scalaVersion + "_" + platform } } } |