summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorGuillaume Galy <guilgaly@users.noreply.github.com>2018-05-16 06:19:47 +0200
committerNikolay Tatarinov <5min4eq.unity@gmail.com>2018-05-16 07:19:47 +0300
commit730a40d0324c14adfeb3837604c4a65c8e1941b2 (patch)
tree66587b74d1ee5abba93cfa563dcab3e953d3104c /main
parent032ae8b5138863c1523cf34a612ba1b19870713b (diff)
downloadmill-730a40d0324c14adfeb3837604c4a65c8e1941b2.tar.gz
mill-730a40d0324c14adfeb3837604c4a65c8e1941b2.tar.bz2
mill-730a40d0324c14adfeb3837604c4a65c8e1941b2.zip
[WIP] Fixes #227; add `mill clean` (#315)
* Adding clean as a default task * [WIP] Improve 'clean' paths resolution * Improve clean targets resolution mechanism * fix error on clean all * update "clean all" to keep all 'out/mill-*' paths * fix cross module resolution in clean task * Add documentation for "clean" task
Diffstat (limited to 'main')
-rw-r--r--main/src/mill/main/MainModule.scala42
-rw-r--r--main/src/mill/main/Resolve.scala70
2 files changed, 111 insertions, 1 deletions
diff --git a/main/src/mill/main/MainModule.scala b/main/src/mill/main/MainModule.scala
index 32281407..4fed5ec5 100644
--- a/main/src/mill/main/MainModule.scala
+++ b/main/src/mill/main/MainModule.scala
@@ -1,10 +1,12 @@
package mill.main
+import ammonite.ops.Path
import mill.define.{NamedTask, Task}
import mill.eval.{Evaluator, Result}
-import mill.util.{EitherOps, ParseArgs, PrintLogger, Watched}
+import mill.util.{PrintLogger, Watched}
import pprint.{Renderer, Truncated}
import upickle.Js
+
object MainModule{
def resolveTasks[T](evaluator: Evaluator[Any], targets: Seq[String], multiSelect: Boolean)
(f: List[NamedTask[Any]] => T) = {
@@ -35,6 +37,9 @@ trait MainModule extends mill.Module{
println(res)
res
}
+
+ private val OutDir: String = "out"
+
/**
* Resolves a mill query string and prints out the tasks it resolves to.
*/
@@ -177,4 +182,39 @@ trait MainModule extends mill.Module{
}
}
}
+
+ /**
+ * Deletes the given targets from the out directory. Providing no targets
+ * will clean everything.
+ */
+ def clean(evaluator: Evaluator[Any], targets: String*) = mill.T.command {
+ val rootDir = ammonite.ops.pwd / OutDir
+
+ val KeepPattern = "(mill-.+)".r.anchored
+
+ def keepPath(path: Path) = path.segments.lastOption match {
+ case Some(KeepPattern(_)) => true
+ case _ => false
+ }
+
+ val pathsToRemove =
+ if (targets.isEmpty)
+ Right(ammonite.ops.ls(rootDir).filterNot(keepPath))
+ else
+ RunScript.resolveTasks(
+ mill.main.ResolveSegments, evaluator, targets, multiSelect = true
+ ).map(
+ _.map { segments =>
+ Evaluator.resolveDestPaths(rootDir, segments).out
+ })
+
+ pathsToRemove match {
+ case Left(err) =>
+ Result.Failure(err)
+ case Right(paths) =>
+ paths.foreach(ammonite.ops.rm)
+ Result.Success(())
+ }
+ }
+
}
diff --git a/main/src/mill/main/Resolve.scala b/main/src/mill/main/Resolve.scala
index ccb5ec04..4b0ae27f 100644
--- a/main/src/mill/main/Resolve.scala
+++ b/main/src/mill/main/Resolve.scala
@@ -86,6 +86,76 @@ object ResolveMetadata extends Resolve[String]{
}
}
+object ResolveSegments extends Resolve[Segments] {
+
+ override def endResolveCross(obj: Module,
+ revSelectorsSoFar: List[Segment],
+ last: List[String],
+ discover: Discover[_],
+ rest: Seq[String]): Either[String, Seq[Segments]] = {
+ obj match{
+ case c: Cross[Module] =>
+ last match{
+ case List("__") => Right(c.items.map(_._2.millModuleSegments))
+ case items =>
+ c.items
+ .filter(_._1.length == items.length)
+ .filter(_._1.zip(last).forall{case (a, b) => b == "_" || a.toString == b})
+ .map(_._2.millModuleSegments) match {
+ case Nil =>
+ Resolve.errorMsgCross(
+ c.items.map(_._1.map(_.toString)),
+ last,
+ revSelectorsSoFar
+ )
+ case res => Right(res)
+ }
+ }
+ case _ =>
+ Left(
+ Resolve.unableToResolve(Segment.Cross(last), revSelectorsSoFar) +
+ Resolve.hintListLabel(revSelectorsSoFar)
+ )
+ }
+ }
+
+ def endResolveLabel(obj: Module,
+ revSelectorsSoFar: List[Segment],
+ last: String,
+ discover: Discover[_],
+ rest: Seq[String]): Either[String, Seq[Segments]] = {
+ val target =
+ obj
+ .millInternal
+ .reflect[Target[_]]
+ .find(_.label == last)
+ .map(t => Right(t.ctx.segments))
+
+ val command =
+ Resolve
+ .invokeCommand(obj, last, discover, rest)
+ .headOption
+ .map(_.map(_.ctx.segments))
+
+ val module =
+ obj.millInternal
+ .reflectNestedObjects[Module]
+ .find(_.millOuterCtx.segment == Segment.Label(last))
+ .map(m => Right(m.millModuleSegments))
+
+ command orElse target orElse module match {
+ case None =>
+ Resolve.errorMsgLabel(
+ singleModuleMeta(obj, discover, revSelectorsSoFar.isEmpty),
+ last,
+ revSelectorsSoFar
+ )
+
+ case Some(either) => either.right.map(Seq(_))
+ }
+ }
+}
+
object ResolveTasks extends Resolve[NamedTask[Any]]{