diff --git a/lib/igniter.ex b/lib/igniter.ex index 96c5dc8..a3b22e9 100644 --- a/lib/igniter.ex +++ b/lib/igniter.ex @@ -102,7 +102,13 @@ defmodule Igniter do @doc "Returns a new igniter" @spec new() :: t() def new do - %__MODULE__{rewrite: Rewrite.new(hooks: [Rewrite.Hook.DotFormatterUpdater])} + %__MODULE__{ + rewrite: + Rewrite.new( + hooks: [Igniter.Rewrite.DotFormatterUpdater], + dot_formatter: Rewrite.DotFormatter.read!(ignore_unknown_deps: true) + ) + } |> include_existing_elixir_file(".igniter.exs", required?: false) |> parse_igniter_config() end diff --git a/lib/igniter/rewrite/dot_formatter_updater.ex b/lib/igniter/rewrite/dot_formatter_updater.ex new file mode 100644 index 0000000..c634ee7 --- /dev/null +++ b/lib/igniter/rewrite/dot_formatter_updater.ex @@ -0,0 +1,33 @@ +defmodule Igniter.Rewrite.DotFormatterUpdater do + @moduledoc false + + alias Rewrite.DotFormatter + + @behaviour Rewrite.Hook + + @formatter ".formatter.exs" + + @impl true + def handle(:new, project) do + {:ok, %{project | dot_formatter: dot_formatter(project)}} + end + + def handle({action, files}, project) when action in [:added, :updated] do + if dot_formatter?(files) do + {:ok, %{project | dot_formatter: dot_formatter(project)}} + else + :ok + end + end + + defp dot_formatter(project) do + case DotFormatter.read(project, ignore_unknown_deps: true) do + {:ok, dot_formatter} -> dot_formatter + {:error, _error} -> DotFormatter.default() + end + end + + defp dot_formatter?(@formatter), do: true + defp dot_formatter?(files) when is_list(files), do: Enum.member?(files, @formatter) + defp dot_formatter?(_files), do: false +end diff --git a/lib/igniter/test.ex b/lib/igniter/test.ex index 02c8693..9a591b2 100644 --- a/lib/igniter/test.ex +++ b/lib/igniter/test.ex @@ -334,7 +334,7 @@ defmodule Igniter.Test do |> Map.put( :rewrite, Rewrite.new( - hooks: [Rewrite.Hook.DotFormatterUpdater], + hooks: [Igniter.Rewrite.DotFormatterUpdater], dot_formatter: igniter.rewrite.dot_formatter ) )