Yeesh.MixRunner (Yeesh v0.8.3)

View Source

Orchestrates running Mix tasks with I/O interception.

Spawns a Mix task in a separate process whose group leader is a Yeesh.IOServer. This transparently captures all IO.puts/IO.gets calls from the task without requiring any modifications to the task itself.

Return values

run/3 returns one of:

  • {:interactive, io_server, task_pid, output, prompt} -- the task called IO.gets and is waiting for input. Use Yeesh.IOServer.provide_input_and_wait/3 to feed lines.
  • {:completed, output} -- the task finished without requesting input.
  • {:error, reason} -- the task could not be started.

Example

case Yeesh.MixRunner.run("my_app.status", ["--verbose"]) do
  {:completed, output} ->
    IO.puts(output)

  {:interactive, io_server, _pid, output, _prompt} ->
    IO.puts(output)
    {next_output, status, _} = Yeesh.IOServer.provide_input_and_wait(io_server, "hello")
    IO.puts(next_output)
end

Summary

Types

Result from starting a Mix task.

Functions

Cleans up after a Mix task finishes.

Runs a Mix task with I/O interception.

Types

run_result()

@type run_result() ::
  {:interactive, pid(), pid(), String.t(), String.t()}
  | {:completed, String.t()}
  | {:error, term()}

Result from starting a Mix task.

Functions

cleanup(io_server, original_shell \\ Mix.Shell.IO)

@spec cleanup(pid(), module()) :: :ok

Cleans up after a Mix task finishes.

Stops the IOServer and restores the original Mix.shell. Called automatically for non-interactive tasks; must be called by the Yeesh.Executor when an interactive task completes.

run(task_name, args \\ [], opts \\ [])

@spec run(String.t(), [String.t()], keyword()) :: run_result()

Runs a Mix task with I/O interception.

The task is spawned in a new process with a custom group leader (Yeesh.IOServer). Mix.shell is temporarily set to Yeesh.MixShell so that Mix.shell().error/1 output is also captured.

Uses Mix.Task.rerun/2 to allow repeated execution of the same task within the VM.

Options

  • :timeout -- how long to wait for the task to produce initial output or request input (default: 30000ms)