I would like to run a mix task from within a custom mix task.
Something like
def run(_) do
Mix.Shell.cmd("mix edeliver build release")
#do other stuff
But I cannot figure out how to execute a shell command. If there is any easier way (besides just making a bash script), let me know.
mix run starts the current application dependencies and the application itself. The application will be compiled if it has not been compiled yet or it is outdated. In both cases, the command-line arguments for the script or expression are available in System.
Shell is the redundant link here. If you want to run edeliver
task, run Mix.Tasks.Edeliver#run
:
def run(_) do
Mix.Tasks.Edeliver.run(~w|build release|)
# do other stuff
Mix.Task.run("edeliver build release")
works
For executing shell comand you can use Loki. You can find functions for shell execution execute/1
.
And example how I used in Mix.Task for executing other mix tasks:
defmodule Mix.Tasks.Sesamex.Gen.Auth do
use Mix.Task
import Loki.Cmd
import Loki.Shell
@spec run(List.t) :: none()
def run([singular, plural]) do
execute("mix sesamex.gen.model #{singular} #{plural}")
execute("mix sesamex.gen.controllers #{singular}")
execute("mix sesamex.gen.views #{singular}")
execute("mix sesamex.gen.templates #{singular}")
execute("mix sesamex.gen.routes #{singular}")
# ...
end
end
Or just look how it execute command:
@spec execute(String.t, list(Keyword.t)) :: {Collectable.t, exit_status :: non_neg_integer}
def execute(string, opts) when is_bitstring(string) and is_list(opts) do
[command | args] = String.split(string)
say IO.ANSI.format [:green, " * execute ", :reset, string]
System.cmd(command, args, env: opts)
end
Hope it help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With