6

How can you run a shell script as a mix alias?

I've tried the following with no luck:

defp aliases() do
  [
    "test": [ "./scripts/test.sh" ]
  ]
end

defp aliases() do
  [
    "test": [ "scripts/test.sh" ]
  ]
end

Each returns with a variation of:

** (Mix) The task "./scripts/test" could not be found

anthonator
  • 4,915
  • 7
  • 36
  • 50

1 Answers1

9

You can use invoke the Mix.Tasks.Cmd task for this:

"test": ["cmd ./scripts/test.sh"]
$ cat a.sh
#!/bin/bash
echo foo
$ cat mix.exs | grep test
      "test": ["cmd ./a.sh", "cmd echo bar"]
$ mix test
foo
bar
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • 1
    This looks like the right answer. The only issue is that when used in an umbrella app it's recursive. Meaning it will run the command within each child app. If the script is meant to do something project-wide this may not be workable. Unfortunately, I don't see an alternative command. – anthonator Apr 03 '18 at 19:02
  • ⚡️‍♀️‍♂️ Because of the `cmd` functionality in umbrella projects, seems like it's just easier reference a local function `"test": [&test/1]` and: `defp test(_argv), do: Mix.shell().cmd("./scripts/test.sh")` – jhwls Sep 02 '23 at 00:56