Mix v1.8.1 Mix.Generator View Source

Conveniences for working with paths and generating content.

All of these functions are verbose, in the sense they log the action to be performed via Mix.shell/0.

Link to this section Summary

Functions

Creates a directory if one does not exist yet.

Creates a file with the given contents. If the file already exists, asks for user confirmation.

Embeds a template given by contents into the current module.

Embeds a text given by contents into the current module.

Link to this section Functions

Specs

create_directory(Path.t()) :: any()

Creates a directory if one does not exist yet.

This function does nothing if the given directory already exists; in this case, it still logs the directory creation.

Examples

iex> Mix.Generator.create_directory("path/to/dir")
* creating path/to/dir
:ok
Link to this function

create_file(path, contents, opts \\ [])

View Source

Specs

create_file(Path.t(), iodata(), keyword()) :: any()

Creates a file with the given contents. If the file already exists, asks for user confirmation.

Options

  • :force - forces installation without a shell prompt.

Examples

iex> Mix.Generator.create_file(".gitignore", "_build\ndeps\n")
* creating .gitignore
:ok
Link to this macro

embed_template(name, contents)

View Source (macro)

Embeds a template given by contents into the current module.

It will define a private function with the name followed by _template that expects assigns as arguments.

This function must be invoked passing a keyword list. Each key in the keyword list can be accessed in the template using the @ macro.

For more information, check EEx.SmartEngine.

Examples

defmodule Mix.Tasks.MyTask do
  require Mix.Generator
  Mix.Generator.embed_template(:log, "Log: <%= @log %>")
end
Link to this macro

embed_text(name, contents)

View Source (macro)

Embeds a text given by contents into the current module.

It will define a private function with the name followed by _text that expects no arguments.

Examples

defmodule Mix.Tasks.MyTask do
  require Mix.Generator
  Mix.Generator.embed_text(:error, "There was an error!")
end