View Source Mix.Generator (Mix v1.15.0)

Conveniences for working with paths and generating content.

Link to this section Summary

Functions

Copies source to target.

Evaluates and copy templates at source to target.

Creates a directory if one does not exist yet.

Creates a file with the given contents.

Embeds a template given by contents into the current module.

Embeds a text given by contents into the current module.

Prompts the user to overwrite the file if it exists.

Prompts the user to overwrite the file if it exists.

Link to this section Functions

Link to this function

copy_file(source, target, options \\ [])

View Source (since 1.9.0)
@spec copy_file(Path.t(), Path.t(), keyword()) :: boolean()

Copies source to target.

If target already exists and the contents are not the same, it asks for user confirmation.

options

Options

  • :force - forces copying without a shell prompt
  • :quiet - does not log command output

examples

Examples

iex> Mix.Generator.copy_file("source/gitignore", ".gitignore")
* creating .gitignore
true
Link to this function

copy_template(source, target, assigns, options \\ [])

View Source (since 1.9.0)
@spec copy_template(Path.t(), Path.t(), keyword(), keyword()) :: boolean()

Evaluates and copy templates at source to target.

The template in source is evaluated with the given assigns.

If target already exists and the contents are not the same, it asks for user confirmation.

options

Options

  • :force - forces copying without a shell prompt
  • :quiet - does not log command output

examples

Examples

iex> assigns = [project_path: "/Users/joe/newproject"]
iex> Mix.Generator.copy_template("source/gitignore", ".gitignore", assigns)
* creating .gitignore
true
Link to this function

create_directory(path, options \\ [])

View Source
@spec create_directory(
  Path.t(),
  keyword()
) :: true

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.

options

Options

  • :quiet - does not log command output

examples

Examples

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

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

View Source
@spec create_file(Path.t(), iodata(), keyword()) :: boolean()

Creates a file with the given contents.

If the file already exists and the contents are not the same, it asks for user confirmation.

options

Options

  • :force - forces creation without a shell prompt
  • :quiet - does not log command output

examples

Examples

iex> Mix.Generator.create_file(".gitignore", "_build\ndeps\n")
* creating .gitignore
true
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

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

Examples

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

overwrite?(path)

View Source (since 1.9.0)
@spec overwrite?(Path.t()) :: boolean()

Prompts the user to overwrite the file if it exists.

Returns false if the file exists and the user forbade to override it. Returns true otherwise.

Link to this function

overwrite?(path, contents)

View Source (since 1.9.0)
@spec overwrite?(Path.t(), iodata()) :: boolean()

Prompts the user to overwrite the file if it exists.

The contents are compared to avoid asking the user to override if the contents did not change. Returns false if the file exists and the content is the same or the user forbade to override it. Returns true otherwise.