View Source Igniter.Project.Application (igniter v0.4.8)

Codemods and tools for working with Application modules.

Summary

Functions

Adds a new child to the children list in the application file

Returns the name of the application module.

app_name() deprecated

Returns the name of the current application.

Returns the name of the application.

Returns the path of the application's priv directory.

Functions

add_new_child(igniter, to_supervise, opts \\ [])

@spec add_new_child(
  Igniter.t(),
  module() | {module(), {:code, term()}} | {module(), term()},
  opts :: Keyword.t()
) :: Igniter.t()

Adds a new child to the children list in the application file

To pass quoted code as the options, use the following format:

{module, {:code, quoted_code}}

i.e

{MyApp.Supervisor, {:code, quote do
  Application.fetch_env!(:app, :config)
end}}

Options

  • :after - A list of other modules that this supervisor should appear after, or a function that takes a module and returns true if this module should be placed after it.
  • :opts_updater - A function that takes the current options (second element of the child tuple), and returns a new value. If the existing value of the module is not a tuple, the value passed into your function will be []. Your function must return {:ok, zipper} or {:error | :warning, "error_or_warning"}.
  • :force? - If true, forces adding a new child, even if an existing child uses the same child module. Defaults to false.

Ordering

We will put the new child as the earliest item in the list that we can, skipping any modules in the after option.

Examples

Given an application start/2 that looks like this:

def start(_type, _args) do
  children = [
    ChildOne,
    {ChildTwo, opt: 1}
  ]

  Supervisor.start_link(children, strategy: :one_for_one)
end

Add a new child that isn't currently present:

Igniter.Project.Application.add_new_child(igniter, NewChild)
# =>
children = [
  NewChild,
  ChildOne,
  {ChildTwo, opt: 1}
]

Add a new child after some existing ones:

Igniter.Project.Application.add_new_child(igniter, NewChild, after: [ChildOne, ChildTwo])
# =>
children = [
  ChildOne,
  {ChildTwo, opt: 1},
  NewChild
]

If the given child module is already present, add_new_child/3 is a no-op by default:

Igniter.Project.Application.add_new_child(igniter, {ChildOne, opt: 1})
# =>
children = [
  ChildOne,
  {ChildTwo, opt: 1}
]

You can explicitly handle module conflicts by passing an :opts_updater:

Igniter.Project.Application.add_new_child(igniter, {ChildOne, opt: 1},
  opts_updater: fn opts ->
    {:ok, Sourceror.Zipper.replace(opts, [opt: 1])}
  end
)
# =>
children = [
  {ChildOne, opt: 1},
  {ChildTwo, opt: 1}
]

Using force?: true, you can force a child to be added, even if the module conflicts with an existing one:

Igniter.Project.Application.add_new_child(igniter, {ChildOne, opt: 1}, force?: true)
# =>
children = [
  {ChildOne, opt: 1},
  ChildOne,
  {ChildTwo, opt: 1}
]

app_module(igniter)

Returns the name of the application module.

app_name()

This function is deprecated. Use `app_name/1` instead..
@spec app_name() :: atom()

Returns the name of the current application.

app_name(igniter)

@spec app_name(Igniter.t()) :: atom()

Returns the name of the application.

create_app(igniter, application)

create_application_file(igniter, application)

priv_dir(igniter, subpath \\ [])

@spec priv_dir(Igniter.t(), [String.t()]) :: String.t()

Returns the path of the application's priv directory.

skip_after(zipper, opts)