com-lihaoyi / mill

Mill is a fast JVM build tool that supports Java and Scala. 2-3x faster than Gradle and 5-10x faster than Maven for common workflows, Mill aims to make your project’s build process performant, maintainable, and flexible
https://mill-build.org/
MIT License
2.03k stars 322 forks source link

Add ability to alias Modules (specially external modules) #299

Open caeus opened 6 years ago

caeus commented 6 years ago

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.

uosis commented 5 years ago

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.

carlosedp commented 1 year ago

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.