Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 66 additions & 37 deletions lib/igniter/project/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -306,50 +306,79 @@ defmodule Igniter.Project.Application do
end
),
{:ok, zipper} <- Igniter.Code.Function.move_to_nth_argument(zipper, 1) do
if opts[:force?] do
insert_child(zipper, to_supervise, opts)
else
case Igniter.Code.List.move_to_list_item(zipper, fn item ->
case extract_child_module(item) do
{:ok, module} -> Igniter.Code.Common.nodes_equal?(module, to_supervise_module)
:error -> false
end
end) do
{:ok, zipper} ->
if updater = opts[:opts_updater] do
zipper =
if Igniter.Code.Tuple.tuple?(zipper) do
zipper
else
Zipper.replace(zipper, {zipper.node, []})
end

{:ok, zipper} = Igniter.Code.Tuple.tuple_elem(zipper, 1)
add_child_to_list(zipper, to_supervise, to_supervise_module, opts)
else
_ ->
with {:ok, zipper} <- Igniter.Code.Function.move_to_def(zipper, :start, 2),
{:ok, zipper} <-
Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:=,
[2],
fn call ->
Igniter.Code.Function.argument_matches_pattern?(
call,
0,
{:children, _, context} when is_atom(context)
) &&
Igniter.Code.Function.argument_matches_pattern?(
call,
1,
{:++, _, [{:__block__, _, [v]}, _]} when is_list(v)
)
end
),
{:ok, zipper} <- Igniter.Code.Function.move_to_nth_argument(zipper, 1),
{:ok, zipper} <- Igniter.Code.Function.move_to_nth_argument(zipper, 0) do
add_child_to_list(zipper, to_supervise, to_supervise_module, opts)
else
_ ->
to_supervise =
case to_supervise do
module when is_atom(module) -> inspect(module)
{module, opts} -> "{#{inspect(module)}, #{Macro.to_string(opts)}}"
end

{:warning,
"""
Could not find a `children = [...]` assignment in the `start` function of the `#{inspect(application)}` module.
Please ensure that #{to_supervise} is added started by the application `#{inspect(application)}` manually.
"""}
end
end
end)
end

updater.(zipper)
defp add_child_to_list(zipper, to_supervise, to_supervise_module, opts) do
if opts[:force?] do
insert_child(zipper, to_supervise, opts)
else
case Igniter.Code.List.move_to_list_item(zipper, fn item ->
case extract_child_module(item) do
{:ok, module} -> Igniter.Code.Common.nodes_equal?(module, to_supervise_module)
:error -> false
end
end) do
{:ok, zipper} ->
if updater = opts[:opts_updater] do
zipper =
if Igniter.Code.Tuple.tuple?(zipper) do
zipper
else
{:ok, zipper}
Zipper.replace(zipper, {zipper.node, []})
end

:error ->
insert_child(zipper, to_supervise, opts)
{:ok, zipper} = Igniter.Code.Tuple.tuple_elem(zipper, 1)

updater.(zipper)
else
{:ok, zipper}
end
end
else
_ ->
to_supervise =
case to_supervise do
module when is_atom(module) -> inspect(module)
{module, opts} -> "{#{inspect(module)}, #{Macro.to_string(opts)}}"
end

{:warning,
"""
Could not find a `children = [...]` assignment in the `start` function of the `#{inspect(application)}` module.
Please ensure that #{to_supervise} is added started by the application `#{inspect(application)}` manually.
"""}
:error ->
insert_child(zipper, to_supervise, opts)
end
end)
end
end

defp insert_child(zipper, child, opts) do
Expand Down
71 changes: 71 additions & 0 deletions test/igniter/project/application_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,77 @@ defmodule Igniter.Project.ApplicationTest do
+ | children = [{Foo, [name: Foo.One]}, {Foo, [name: Foo.Two]}]
""")
end

test "adds a child when children list uses ++ concatenation" do
test_project(
files: %{
"lib/test/application.ex" => """
defmodule Test.Application do
use Application

@impl true
def start(_type, _args) do
children =
[
] ++ target_children()

opts = [strategy: :one_for_one, name: Test.Supervisor]
Supervisor.start_link(children, opts)
end

if Mix.target() == :host do
defp target_children do
[]
end
else
defp target_children do
[]
end
end
end
"""
}
)
|> Igniter.Project.MixProject.update(:application, [:mod], fn _zipper ->
{:ok, {:code, "{Test.Application, []}"}}
end)
|> apply_igniter!()
|> Igniter.Project.Application.add_new_child(Foo)
|> assert_has_patch("lib/test/application.ex", """
7 7 | [
8 + | Foo
8 9 | ] ++ target_children()
""")
end

test "doesn't add a duplicate when children list uses ++ concatenation" do
test_project(
files: %{
"lib/test/application.ex" => """
defmodule Test.Application do
use Application

@impl true
def start(_type, _args) do
children =
[
Foo
] ++ target_children()

opts = [strategy: :one_for_one, name: Test.Supervisor]
Supervisor.start_link(children, opts)
end
end
"""
}
)
|> Igniter.Project.MixProject.update(:application, [:mod], fn _zipper ->
{:ok, {:code, "{Test.Application, []}"}}
end)
|> apply_igniter!()
|> Igniter.Project.Application.add_new_child(Foo)
|> assert_unchanged()
end
end

describe "app_name/1" do
Expand Down
Loading