Mix v1.1.1 Mix.Utils

Utilities used throughout Mix and tasks.

Summary

Functions

Converts the given string to CamelCase format

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

Takes 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

Extracts files from a list of paths

Extracts all stale sources compared to the given targets

Returns the date the given path was last modified

Gets the Mix home

Gets all paths defined in the MIX_PATH env variable

Takes a module and converts it to a command

Opens and reads content from either a URL or a local filesystem path and returns the contents as a {:ok, binary}, :badpath for invalid paths or {:local, message} for local errors and {:remote, message} for remote ones

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

Symlinks directory source to target or copies it recursively in case symlink fails

Converts the given atom or binary to underscore format

Functions

camelize(string)

Specs

camelize(String.t) :: String.t

Converts the given string to CamelCase format.

Examples

iex> Mix.Utils.camelize "foo_bar"
"FooBar"
can_write?(path)

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

command_to_module(command, at \\ Elixir)

Takes 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"
extract_files(paths, exts_or_pattern)

Extracts 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)

Extracts 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()

Gets 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()

Gets 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"
read_path(path, opts \\ [])

Specs

read_path(String.t, Keyword.t) ::
  {:ok, binary} |
  :badpath |
  {:remote, String.t} |
  {:local, String.t} |
  {:checksum, String.t}

Opens and reads content from either a URL or a local filesystem path and returns the contents as a {:ok, binary}, :badpath for invalid paths or {:local, message} for local errors and {:remote, message} for remote ones.

Options

  • :sha512 - checks against the given sha512 checksum. Returns {:checksum, message} in case it fails
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:

iex> Mix.Utils.underscore "SAPExample"
"sap_example"

iex> Mix.Utils.camelize "sap_example"
"SapExample"