Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding clean as a default task #253

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion main/src/mill/main/MainModule.scala
Original file line number Diff line number Diff line change
@@ -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) = {
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea why you are wrapping this in a Try; you just need to return a simple Result object

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ammonite functions call things which can throw exceptions. Is it acceptable to let those exceptions be thrown?

if (ammonite.ops.ls(ammonite.ops.pwd).contains(ammonite.ops.pwd / BuildFile)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be exists(pwd / "build.sc")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are doing validation on ammonite.ops.pwd instead of the paths you are going to delete

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

targets match {
case Nil =>
remove(List(ammonite.ops.pwd / OutDir))
case _ =>
remove(targets
.map(_.replace(".", "/"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use resolveDestPaths (https://github.com/lihaoyi/mill/blob/master/core/src/mill/eval/Evaluator.scala#L342) to figure out what targets to clean, to ensure it stays in sync with any future changes

.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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the filter; rm already just ignores a path if there' nothing to remove

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice.

Result.Success("Targets cleaned")
}
}
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down