Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 42 additions & 12 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -504,21 +504,51 @@ defmodule Igniter do
"""
@spec update_elixir_file(t(), Path.t(), zipper_updater()) :: Igniter.t()
def update_elixir_file(igniter, path, func) do
if Rewrite.has_source?(igniter.rewrite, path) do
igniter
|> apply_func_with_zipper(path, func)
|> format(path)
else
if exists?(igniter, path) do
source = read_ex_source!(igniter, path)

%{igniter | rewrite: Rewrite.put!(igniter.rewrite, source)}
|> format(path)
case use_or_read_elixir_file(igniter, path) do
{:exists, igniter} ->
igniter
|> apply_func_with_zipper(path, func)
|> format(path)
else

{:missing, igniter} ->
add_issue(igniter, "Required #{path} but it did not exist")
end
end
end

@doc """
Updates the source code of the given elixir file if it exists
"""
@spec update_existing_elixir_file(t(), Path.t(), zipper_updater(), keyword) :: Igniter.t()
def update_existing_elixir_file(igniter, path, func, opts \\ []) do
Comment thread
LostKobrakai marked this conversation as resolved.
Outdated
required? = Keyword.get(opts, :required?, false)

case use_or_read_elixir_file(igniter, path) do
{:exists, igniter} ->
igniter
|> apply_func_with_zipper(path, func)
|> format(path)

{:missing, igniter} ->
if required? do
add_issue(igniter, "Required #{path} but it did not exist")
else
igniter
end
end
end

defp use_or_read_elixir_file(igniter, path) do
cond do
Rewrite.has_source?(igniter.rewrite, path) ->
{:exists, igniter}

exists?(igniter, path) ->
source = read_ex_source!(igniter, path)
igniter = %{igniter | rewrite: Rewrite.put!(igniter.rewrite, source)}
{:exists, format(igniter, path)}

true ->
{:missing, igniter}
end
end

Expand Down
60 changes: 40 additions & 20 deletions lib/igniter/project/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,7 @@ defmodule Igniter.Project.Config do
|> Igniter.include_or_create_file(config_file_path, file_contents)
|> ensure_default_configs_exist(file_path)
|> Igniter.update_elixir_file(config_file_path, fn zipper ->
case Zipper.find(zipper, fn
{:import, _, [Config]} ->
true

{:import, _, [{:__aliases__, _, [:Config]}]} ->
true

_ ->
false
end) do
case find_config(zipper) do
nil ->
{:warning,
bad_config_message(
Expand Down Expand Up @@ -272,16 +263,7 @@ defmodule Igniter.Project.Config do
|> ensure_default_configs_exist(file_name)
|> Igniter.include_or_create_file(file_path, file_contents)
|> Igniter.update_elixir_file(file_path, fn zipper ->
case Zipper.find(zipper, fn
{:import, _, [Config]} ->
true

{:import, _, [{:__aliases__, _, [:Config]}]} ->
true

_ ->
false
end) do
case find_config(zipper) do
nil ->
{:warning, bad_config_message(app_name, file_path, config_path, value, opts)}

Expand All @@ -294,6 +276,31 @@ defmodule Igniter.Project.Config do
end)
end

def remove_configuration(igniter, file_name, app_name) do
Comment thread
LostKobrakai marked this conversation as resolved.
Outdated
file_path = config_file_path(igniter, file_name)

Igniter.update_existing_elixir_file(igniter, file_path, fn zipper ->
case find_config(zipper) do
nil ->
igniter

_ ->
case Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:config,
[2, 3],
&Igniter.Code.Function.argument_equals?(&1, 0, app_name)
) do
:error ->
:error

{:ok, zipper} ->
Zipper.remove(zipper)
end
end
end)
end

defp config_file_path(igniter, file_name) do
case igniter |> Igniter.Project.Application.config_path() |> Path.split() do
[path] -> [path]
Expand Down Expand Up @@ -734,4 +741,17 @@ defmodule Igniter.Project.Config do
defp simple_atom(value) do
is_atom(value) and Regex.match?(~r/^[a-z_][a-zA-Z0-9_?!]*$/, to_string(value))
end

defp find_config(zipper) do
Zipper.find(zipper, fn
{:import, _, [Config]} ->
true

{:import, _, [{:__aliases__, _, [:Config]}]} ->
true

_ ->
false
end)
end
end
22 changes: 22 additions & 0 deletions test/igniter/project/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,26 @@ defmodule Igniter.Project.ConfigTest do
""")
end
end

describe "remove_configuration/3" do
test "it does not create the config file if it does not exist" do
test_project()
|> Igniter.Project.Config.remove_configuration("fake.exs", :fake)
|> refute_creates("config/fake.exs")
end

test "it removes the applications configuration if it exists" do
test_project()
|> Igniter.create_new_file("config/fake.exs", """
import Config

config :fake, buz: [:blat]
""")
|> apply_igniter!()
|> Igniter.Project.Config.remove_configuration("fake.exs", :fake)
|> assert_has_patch("config/fake.exs", """
3 - |config :fake, buz: [:blat]
""")
end
end
end