Mix v1.0.5 Mix.Utils

Utilities used throughout Mix and tasks.

Summary

Functions

Converts the given string to CamelCase format

Take a command name and attempts to load a module with the command name converted to a module name in the given at scope

Takes a command and converts it to the module name format

Copies content from either a URL or a local filesystem path to target path

Extract files from a list of paths

Extract all stale sources compared to the given targets

Returns the date the given path was last modified

Get the mix home

Get all paths defined in the MIX_PATH env variable

Takes a module and converts it to a command

Prompts the user to overwrite the file if it exists. Returns the user input

Opens and reads content from either a URL or a local filesystem path and returns the contents as a binary

Returns true if any of the sources are stale compared to the given targets

Symlink directory source to target or copy it recursively in case symlink fails

Converts the given atom or binary to underscore format

Functions

camelize(arg1)

Converts the given string to CamelCase format.

Examples

iex> Mix.Utils.camelize "foo_bar"
"FooBar"
command_to_module(command, at \\ Elixir)

Take a command name and attempts to load a module with the command name converted to a module name in the given at scope.

Returns {:module, module} in case a module exists and is loaded, {:error, reason} otherwise.

Examples

iex> Mix.Utils.command_to_module("compile", Mix.Tasks)
{:module, Mix.Tasks.Compile}
command_to_module_name(s)

Takes a command and converts it to the module name format.

Examples

iex> Mix.Utils.command_to_module_name("compile.elixir")
"Compile.Elixir"
copy_path!(source, target, opts \\ [])

Copies content from either a URL or a local filesystem path to target path.

Used by tasks like archive.install and local.rebar that support installation either from a URL or a local file.

Raises if the given path is not a URL, nor a file or if the file or URL are invalid.

Options

  • :shell - Forces the use of wget or curl to fetch the file if the given path is a URL.

  • :force - Forces overwriting target file without a shell prompt.
extract_files(paths, exts_or_pattern)

Extract files from a list of paths.

exts_or_pattern may be a list of extensions or a Path.wildcard/1 pattern.

If the path in paths is a file, it is included in the return result. If it is a directory, it is searched recursively for files with the given extensions or matching the given patterns.

extract_stale(sources, targets)

Extract all stale sources compared to the given targets.

last_modified(path)

Returns the date the given path was last modified.

If the path does not exist, it returns the unix epoch (1970-01-01 00:00:00).

mix_home()

Get the mix home.

It defaults to ~/.mix unless the MIX_HOME environment variable is set.

Developers should only store entries in the MIX_HOME directory which are guaranteed to work across multiple Elixir versions, as it is not recommended to swap the MIX_HOME directory as configuration and other important data may be stored there.

mix_paths()

Get all paths defined in the MIX_PATH env variable.

MIX_PATH may contain multiple paths. If on Windows, those paths should be separated by ;, if on unix systems, use :.

module_name_to_command(module, nesting \\ 0)

Takes a module and converts it to a command.

The nesting argument can be given in order to remove the nesting of a module.

Examples

iex> Mix.Utils.module_name_to_command(Mix.Tasks.Compile, 2)
"compile"

iex> Mix.Utils.module_name_to_command("Mix.Tasks.Compile.Elixir", 2)
"compile.elixir"
overwriting?(path)

Prompts the user to overwrite the file if it exists. Returns the user input.

read_path!(path, opts \\ [])

Opens and reads content from either a URL or a local filesystem path and returns the contents as a binary.

Raises if the given path is not a URL, nor a file or if the file or URL are invalid.

Options

  • :shell - Forces the use of wget or curl to fetch the file if the given path is a URL.
stale?(sources, targets)

Returns true if any of the sources are stale compared to the given targets.

underscore(atom)

Converts the given atom or binary to underscore format.

If an atom is given, it is assumed to be an Elixir module, so it is converted to a binary and then processed.

Examples

iex> Mix.Utils.underscore "FooBar"
"foo_bar"

iex> Mix.Utils.underscore "Foo.Bar"
"foo/bar"

iex> Mix.Utils.underscore Foo.Bar
"foo/bar"

In general, underscore can be thought of as the reverse of camelize, however, in some cases formatting may be lost:

Mix.Utils.underscore "SAPExample"  #=> "sap_example"
Mix.Utils.camelize   "sap_example" #=> "SapExample"