8000 Implement removal of configuration by LostKobrakai · Pull Request #309 · ash-project/igniter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement removal of configuration #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2025
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
40 changes: 29 additions & 11 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -501,24 +501,35 @@ defmodule Igniter do

@doc """
Updates the source code of the given elixir file

## Options

- `:required?` - Tracks an issue for the file missing. Defaults to `true`.

"""
@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
@spec update_elixir_file(t(), Path.t(), zipper_updater(), keyword) :: Igniter.t()
def update_elixir_file(igniter, path, func, opts \\ []) do
required? = Keyword.get(opts, :required?, true)

cond do
Rewrite.has_source?(igniter.rewrite, path) ->
igniter
|> apply_func_with_zipper(path, func)
|> format(path)

exists?(igniter, path) ->
source = read_ex_source!(igniter, path)

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

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

true ->
igniter
end
end

Expand Down Expand Up @@ -606,7 +617,14 @@ defmodule Igniter do
include_existing_file(igniter, path, Keyword.put(opts, :source_handler, Rewrite.Source.Ex))
end

@doc "Includes the given file in the project, expecting it to exist. Does nothing if its already been added."
@doc """
Includes the given file in the project, expecting it to exist. Does nothing if its already been added.

## Options

- `:required?` - Tracks an issue for the file missing. Defaults to `false`.

"""
@spec include_existing_file(t(), Path.t(), opts :: Keyword.t()) :: t()
def include_existing_file(igniter, path, opts \\ []) do
required? = Keyword.get(opts, :required?, false)
Expand Down
73 changes: 53 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,44 @@ defmodule Igniter.Project.Config do
end)
end

@doc """
Removes an applications config completely.
"""
@spec remove_application_configuration(Igniter.t(), Path.t(), atom()) :: Igniter.t()
def remove_application_configuration(igniter, file_name, app_name) do
file_path = config_file_path(igniter, file_name)

Igniter.update_elixir_file(
igniter,
file_path,
fn zipper ->
case find_config(zipper) do
nil -> igniter
_ -> recursively_remove_configurations(zipper, app_name)
end
end,
required?: false
)
end

defp recursively_remove_configurations(zipper, app_name) do
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 ->
zipper

{:ok, zipper} ->
zipper
|> Zipper.remove()
|> Zipper.top()
|> recursively_remove_configurations(app_name)
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 +754,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
38 changes: 38 additions & 0 deletions test/igniter/project/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,42 @@ defmodule Igniter.Project.ConfigTest do
""")
end
end

describe "remove_application_configuration/3" do
test "it does not create the config file if it does not exist" do
test_project()
|> Igniter.Project.Config.remove_application_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_application_configuration("fake.exs", :fake)
|> assert_has_patch("config/fake.exs", """
3 - |config :fake, buz: [:blat]
""")
end

test "it removes duplicate application configurations" do
test_project()
|> Igniter.create_new_file("config/fake.exs", """
import Config

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