Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a mix task from within a mix task?

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.

like image 614
Fletcher Moore Avatar asked Jan 15 '17 00:01

Fletcher Moore


People also ask

What does mix Run do?

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.


3 Answers

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
like image 61
Aleksei Matiushkin Avatar answered Oct 24 '22 01:10

Aleksei Matiushkin


Mix.Task.run("edeliver build release") works

like image 3
Alexander Meshkov Avatar answered Oct 24 '22 01:10

Alexander Meshkov


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.

like image 1
khusnetdinov Avatar answered Oct 24 '22 01:10

khusnetdinov