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

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

Closed
caeus opened this issue Apr 16, 2018 · 3 comments · Fixed by #3760
Closed

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

caeus opened this issue Apr 16, 2018 · 3 comments · Fixed by #3760
Milestone

Comments

@caeus
Copy link

caeus commented Apr 16, 2018

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
Copy link
Contributor

uosis commented Sep 8, 2019

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
Copy link
Contributor

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.

@carlosedp
Copy link
Contributor

carlosedp commented Oct 17, 2024

Nice that this is "built-in" and documented but just a heads-up that my plugin(https://github.com/carlosedp/mill-aliases) allows one to define an alias that calls a sequence of other modules or commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants