mill
mill copied to clipboard
Add ability to alias Modules (specially external modules)
In the spirit of issue 259, wouldn't it be nice to give Mill the capability to alias commands and/or (external?) modules? So if someone working creating an External Module with a long package name can therefore call an easy to remember alias than the whole package name plus slash plus target.
As a workaround, something like this can be added to the ./mill
wrapper script:
if [ "$1" = "bloop" ] ; then
exec $MILL_EXEC_PATH mill.contrib.Bloop/install
elif [ "$1" = "idea" ] ; then
exec $MILL_EXEC_PATH mill.scalalib.GenIdea/idea
else
exec $MILL_EXEC_PATH "$@"
fi
A bit better than global shell alias.
I have an implementation I add to all my build.sc
files allowing me to define an alias list that even print a help... I defined the alias runner as the run
command but could be set to anything else by changing the def
method.
// In your build.sc
...
// -----------------------------------------------------------------------------
// Command Aliases
// -----------------------------------------------------------------------------
// Alias commands are run like `./mill run [alias]`
// Define the alias as a map element containing the alias name and a Seq with the tasks to be executed
val aliases: Map[String, Seq[String]] = Map(
"lint" -> Seq("mill.scalalib.scalafmt.ScalafmtModule/reformatAll __.sources", "__.fix"),
"fmt" -> Seq("mill.scalalib.scalafmt.ScalafmtModule/reformatAll __.sources"),
"checkfmt" -> Seq("mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll __.sources"),
"deps" -> Seq("mill.scalalib.Dependency/showUpdates"),
"testall" -> Seq("__.test"),
"pub" -> Seq("io.kipp.mill.ci.release.ReleaseModule/publishAll"),
)
// Alias Runner
def run(ev: eval.Evaluator, alias: String = "") = T.command {
aliases.get(alias) match {
case Some(t) =>
mill.main.MainModule.evaluateTasks(
ev,
t.flatMap(x => Seq(x, "+")).flatMap(_.split("\\s+")).init,
mill.define.SelectMode.Single,
)(identity)
case None =>
Console.err.println("Use './mill run [alias]'."); Console.out.println("Available aliases:")
aliases.foreach(x => Console.out.println(s"${x._1.padTo(15, ' ')} - Commands: (${x._2.mkString(", ")})"));
sys.exit(1)
}
}
For example:
❯ ./mill run
[1/1] run
Use './mill run [alias]'.
Available aliases:
testall - Commands: (__.test)
pub - Commands: (io.kipp.mill.ci.release.ReleaseModule/publishAll)
deps - Commands: (mill.scalalib.Dependency/showUpdates)
fmt - Commands: (mill.scalalib.scalafmt.ScalafmtModule/reformatAll __.sources)
lint - Commands: (mill.scalalib.scalafmt.ScalafmtModule/reformatAll __.sources, __.fix)
checkfmt - Commands: (mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll __.sources)
❯ ./mill run fmt
[1/1] run > [7/7] mill.scalalib.scalafmt.ScalafmtModule.reformatAll
Formatting 2 Scala sources
parsed config (v3.7.3): /Users/cdepaula/repos/scala/mill-docker-nativeimage/.scalafmt.conf
I could submit a PR adding something like this to the core Mill if desired.