Mix v1.0.5 Mix.Task behaviour

A simple module that provides conveniences for creating, loading and manipulating tasks.

A Mix task can be defined by simply using Mix.Task in a module starting with Mix.Tasks. and defining the run/1 function:

defmodule Mix.Tasks.Hello do
  use Mix.Task

  def run(_) do
    Mix.shell.info "hello"
  end
end

The run/1 function will receive all arguments passed to the command line.

Attributes

There are a couple attributes available in Mix tasks to configure them in Mix:

  • @shortdoc - makes the task public with a short description that appears

               on `mix help`
  • @recursive - run the task recursively in umbrella projects

Summary

Functions

Checks if exists an alias with the given task name

Returns all loaded task modules

Clears all invoked tasks, allowing them to be reinvoked

Receives a task name and returns {:ok, module} if a task is found

Receives a task name and retrieves the task module

Loads all tasks in all code paths

Loads all tasks in the given paths

Gets the moduledoc for the given task module

Checks if the task should be run recursively for all sub-apps in umbrella projects

Reenables a given task so it can be executed again down the stack

Runs a task with the given args

Gets the shortdoc for the given task module

Returns true if given module is a task

Returns the task name for the given module

Callbacks

A task needs to implement run which receives a list of command line args

Types

task_module :: atom
task_name :: String.t | atom

Functions

alias?(task)

Specs

alias?(task_name) :: boolean

Checks if exists an alias with the given task name.

all_modules()

Specs

all_modules :: [task_module]

Returns all loaded task modules.

Modules that are not yet loaded won’t show up. Check load_all/0 if you want to preload all tasks.

clear()

Specs

clear :: :ok

Clears all invoked tasks, allowing them to be reinvoked.

This operation is not recursive.

get(task)

Specs

get(task_name) :: task_module | nil

Receives a task name and returns {:ok, module} if a task is found.

Otherwise returns {:error, :invalid} in case the module exists but it isn’t a task or {:error, :not_found}.

get!(task)

Specs

get!(task_name) :: task_module | no_return

Receives a task name and retrieves the task module.

Exceptions

load_all()

Specs

load_all :: [task_module]

Loads all tasks in all code paths.

load_tasks(dirs)

Specs

load_tasks([List.Chars.t]) :: [task_module]

Loads all tasks in the given paths.

moduledoc(module)

Specs

moduledoc(task_module) :: String.t | nil

Gets the moduledoc for the given task module.

Returns the moduledoc or nil.

recursive(module)

Specs

recursive(task_module) :: boolean

Checks if the task should be run recursively for all sub-apps in umbrella projects.

Returns true or false.

reenable(task)

Specs

reenable(task_name) :: :ok

Reenables a given task so it can be executed again down the stack.

Both alias and the regular stack are reenabled when this function is called.

If an umbrella project reenables a task, it is reenabled for all children projects.

run(task, args \\ [])

Specs

run(task_name, [any]) :: any

Runs a task with the given args.

If the task was not yet invoked, it runs the task and returns the result.

If there is an alias with the same name, the alias will be invoked instead of a task.

If the task or alias were already invoked, it does not run them again and simply aborts with :noop.

It may raise an exception if the an alias or a task can’t be found or the task is invalid. Check get!/1 for more information.

shortdoc(module)

Specs

shortdoc(task_module) :: String.t | nil

Gets the shortdoc for the given task module.

Returns the shortdoc or nil.

task?(module)

Specs

task?(task_module) :: boolean

Returns true if given module is a task.

task_name(module)

Specs

task_name(task_module) :: task_name

Returns the task name for the given module.

Callbacks

run(list)

Specs

run([binary]) :: any

A task needs to implement run which receives a list of command line args.