From 17a4696f80df84c0f5bf87db5e17e618c27dce5d Mon Sep 17 00:00:00 2001 From: Mark Soule Date: Tue, 20 Mar 2018 22:33:57 -0500 Subject: [PATCH] Adding clean as a default task --- main/src/mill/main/MainModule.scala | 38 ++++++++++++++++++++++++++++- readme.md | 11 +++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/main/src/mill/main/MainModule.scala b/main/src/mill/main/MainModule.scala index 4b0a4354a09..272fe8a2a11 100644 --- a/main/src/mill/main/MainModule.scala +++ b/main/src/mill/main/MainModule.scala @@ -1,10 +1,13 @@ 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 + +import scala.util.{Failure, Success, Try} object MainModule{ def resolveTasks[T](evaluator: Evaluator[Any], targets: Seq[String], multiSelect: Boolean) (f: List[NamedTask[Any]] => T) = { @@ -31,6 +34,9 @@ trait MainModule extends mill.Module{ implicit def millScoptTasksReads[T] = new mill.main.Tasks.Scopt[T]() implicit def millScoptEvaluatorReads[T] = new mill.main.EvaluatorScopt[T]() + private val OutDir: String = "out" + private val BuildFile: String = "build.sc" + /** * Resolves a mill query string and prints out the tasks it resolves to. */ @@ -173,4 +179,34 @@ trait MainModule extends mill.Module{ } } } + + /** + * Deletes the given targets from the out directory. Providing no targets will clean everything. + */ + def clean(targets: String*) = mill.T.command { + val result = Try { + if (ammonite.ops.ls(ammonite.ops.pwd).contains(ammonite.ops.pwd / BuildFile)) { + targets match { + case Nil => + remove(List(ammonite.ops.pwd / OutDir)) + case _ => + remove(targets + .map(_.replace(".", "/")) + .map(target => Path(target, ammonite.ops.pwd / OutDir)) + .toList) + } + } else { + Result.Failure(s"Cannot find $BuildFile") + } + } + result match { + case Success(r) => r + case Failure(error) => Result.Exception(error, new Result.OuterStack(error.getStackTrace)) + } + } + + private def remove(paths: List[Path]): Result[String] = { + paths.filter(ammonite.ops.exists).foreach(ammonite.ops.rm) + Result.Success("Targets cleaned") + } } diff --git a/readme.md b/readme.md index a1dacc2ced1..aa0f5539575 100644 --- a/readme.md +++ b/readme.md @@ -64,6 +64,17 @@ You can get Mill to show the JSON-structured output for a particular `Target` or Output will be generated into a the `./out` folder. +You can clean the project using `clean`: + +```bash +# Clean entire project. +mill clean +# Clean a single target. +mill clean main +# Clean multiple targets. +mill clean main core +``` + If you are repeatedly testing Mill manually by running it against the `build.sc` file in the repository root, you can skip the assembly process and directly run it via: