diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/mill/Main.scala | 81 | ||||
-rw-r--r-- | core/src/main/scala/mill/discover/Discovered.scala | 25 |
2 files changed, 57 insertions, 49 deletions
diff --git a/core/src/main/scala/mill/Main.scala b/core/src/main/scala/mill/Main.scala index 0d316530..3112ec27 100644 --- a/core/src/main/scala/mill/Main.scala +++ b/core/src/main/scala/mill/Main.scala @@ -3,18 +3,55 @@ package mill import ammonite.ops._ import ammonite.util.{Name, Res} import mill.define.Task -import mill.discover.{Discovered, NestedEntry} +import mill.discover.{CommandInfo, Discovered, Info, LabelInfo} import mill.eval.Evaluator import mill.util.OSet import play.api.libs.json.Format object Main { + def apply[T: Discovered](args: Seq[String], obj: T) = { + val Seq(selectorString, rest @_*) = args + val selector = selectorString.split('.') + val discovered = implicitly[Discovered[T]] + val consistencyErrors = Discovered.consistencyCheck(obj, discovered) + pprint.log(consistencyErrors) + if (consistencyErrors.nonEmpty) println("Failed Discovered.consistencyCheck: " + consistencyErrors) + else { + val mapping = Discovered.mapping(obj)(discovered) + val workspacePath = pwd / 'out + val evaluator = new Evaluator(workspacePath, mapping) + val mainRoutes = discovered.mains.map(x => (x.path :+ x.entryPoint.name, x: Info[T, _])) + val targetRoutes = discovered.targets.map(x => (x.path, x: Info[T, _])) + val routeList: Seq[(Seq[String], Info[T, _])] = mainRoutes ++ targetRoutes + val allRoutes = routeList.toMap[Seq[String], Info[_, _]] + allRoutes.get(selector) match{ + case Some(nestedEntryPoint: CommandInfo[T, _]) => + nestedEntryPoint.invoke( + obj, + ammonite.main.Scripts.groupArgs(rest.toList) + ) match{ + case error: mill.discover.Router.Result.Error => + println("Failed to evaluate main method: " + error) + case mill.discover.Router.Result.Success(target) => + println("Found target! " + target) + val evaluated = evaluator.evaluate(OSet(target)) + pprint.log(evaluated) + } + + case Some(labelled: LabelInfo[T, _]) => + val target = labelled.run(obj) + val evaluated = evaluator.evaluate(OSet(target)) + pprint.log(evaluated) + case None => println("Unknown selector: " + selector) + } + } + + } def main(args: Array[String]): Unit = { - val List(buildFile, selector0, rest @_*) = args.toList - pprint.log((buildFile, selector0, rest)) - val selector = selector0.split('.').toList + val List(buildFile, rest @_*) = args.toList + ammonite.Main().instantiateInterpreter() match{ case Left(problems) => pprint.log(problems) case Right(interp) => @@ -22,42 +59,8 @@ object Main { if (!result.isSuccess) println(result) else{ - val (obj, discovered) = result.asInstanceOf[Res.Success[(Any, Discovered[Any])]].s - val consistencyErrors = Discovered.consistencyCheck(obj, discovered) - pprint.log(consistencyErrors) - if (consistencyErrors.nonEmpty) println("Failed Discovered.consistencyCheck: " + consistencyErrors) - else { - val mapping = Discovered.mapping(obj)(discovered) - val workspacePath = pwd / 'out - val evaluator = new Evaluator(workspacePath, mapping) - val mainRoutes = discovered.mains.map(x => (x.path :+ x.entryPoint.name, Left(x))) - val targetRoutes = discovered.targets.map(x => x._1 -> Right(x)) - val allRoutes = (mainRoutes ++ targetRoutes).toMap[ - Seq[String], - Either[NestedEntry[Any, _], (Seq[String], Format[_], Any => Task[_])] - ] - allRoutes.get(selector) match{ - case Some(Left(nestedEntryPoint)) => - nestedEntryPoint.invoke( - obj, - ammonite.main.Scripts.groupArgs(rest.toList) - ) match{ - case error: mill.discover.Router.Result.Error => - println("Failed to evaluate main method: " + error) - case mill.discover.Router.Result.Success(target) => - println("Found target! " + target) - val evaluated = evaluator.evaluate(OSet(target)) - pprint.log(evaluated) - } - - case None => println("Unknown selector: " + selector) - case Some(Right((_, _, targetFunc))) => - val target = targetFunc(obj) - val evaluated = evaluator.evaluate(OSet(target)) - pprint.log(evaluated) - } - } + apply(rest, obj)(discovered) } } diff --git a/core/src/main/scala/mill/discover/Discovered.scala b/core/src/main/scala/mill/discover/Discovered.scala index 0a17dcba..3c89fbc2 100644 --- a/core/src/main/scala/mill/discover/Discovered.scala +++ b/core/src/main/scala/mill/discover/Discovered.scala @@ -6,36 +6,41 @@ import play.api.libs.json.Format import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context - -class Discovered[T](val targets: Seq[(Seq[String], Format[_], T => Task[_])], - val mains: Seq[NestedEntry[T, _]]){ - def apply(t: T) = targets.map{case (a, f, b) => (a, f, b(t)) } +sealed trait Info[T, V] +class Discovered[T](val targets: Seq[LabelInfo[T, _]], + val mains: Seq[CommandInfo[T, _]]){ + def apply(t: T) = targets.map{case LabelInfo(a, f, b) => (a, f, b(t)) } } case class Labelled[T](target: Task[T], format: Format[T], segments: Seq[String]) +case class LabelInfo[T, V](path: Seq[String], + format: Format[V], + run: T => Task[V]) extends Info[T, V] -case class NestedEntry[T, V](path: Seq[String], resolve: T => V, entryPoint: EntryPoint[V]){ +case class CommandInfo[T, V](path: Seq[String], + resolve: T => V, + entryPoint: EntryPoint[V]) extends Info[T, V]{ def invoke(target: T, groupedArgs: Seq[(String, Option[String])]): Result[Task[Any]] = { entryPoint.invoke(resolve(target),groupedArgs) } } -object NestedEntry{ +object CommandInfo{ def make[T, V](path: Seq[String], resolve: T => V) - (entryPoint: EntryPoint[V]) = NestedEntry(path, resolve, entryPoint) + (entryPoint: EntryPoint[V]) = CommandInfo(path, resolve, entryPoint) } object Discovered { def consistencyCheck[T](base: T, d: Discovered[T]) = { val inconsistent = for{ - (path, formatter, targetGen) <- d.targets + LabelInfo(path, formatter, targetGen) <- d.targets if targetGen(base) ne targetGen(base) } yield path inconsistent } def makeTuple[T, V](path: Seq[String], func: T => Task[V])(implicit f: Format[V]) = { - (path, f, func) + LabelInfo(path, f, func) } @@ -102,7 +107,7 @@ object Discovered { val select = segments.foldLeft[Tree](Ident(arg)) { (prefix, name) => q"$prefix.${TermName(name)}" } - q"mill.discover.NestedEntry.make(Seq(..$segments), ($arg: $tpe) => $select)($entry)" + q"mill.discover.CommandInfo.make(Seq(..$segments), ($arg: $tpe) => $select)($entry)" } c.Expr[Discovered[T]](q""" |