Skip to content

Commit

Permalink
Read inputs from .formatter.exs (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNeck authored Nov 10, 2024
1 parent a8a1c2b commit 4171691
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .dialyzer_ignore.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
~r|lib/mix/tasks/recode.ex:.*:no_return|,
~r|lib/recode/runner/impl.ex:223:apply|,
~r|lib/recode/runner/impl.ex:228:call|
~r|lib/recode/runner/impl.ex:235:apply|,
~r|lib/recode/runner/impl.ex:240:call|
]
7 changes: 5 additions & 2 deletions examples/my_code/.recode.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
color: true,
# Can also be set/reset with `--verbose`/`--no-verbose`.
verbose: false,
# Inputs can be a path, glob expression or list of paths and glob expressions.
# With the atom :formatter the inputs from .formatter.exs are
# used. also allowed in the list mentioned above.
# Can be overwritten by calling `mix recode "lib/**/*.ex"`.
inputs: ["{mix,.formatter}.exs", "{apps,config,lib,test,priv}/**/*.{ex,exs}"],
inputs: :formatter,
formatters: [Recode.CLIFormatter],
tasks: [
# Tasks could be added by a tuple of the tasks module name and an options
Expand All @@ -35,4 +38,4 @@
{Recode.Task.UnnecessaryIfUnless, []},
{Recode.Task.UnusedVariable, [active: false]}
]
]
]
2 changes: 1 addition & 1 deletion examples/my_code/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"glob_ex": {:hex, :glob_ex, "0.1.11", "cb50d3f1ef53f6ca04d6252c7fde09fd7a1cf63387714fe96f340a1349e62c93", [:mix], [], "hexpm", "342729363056e3145e61766b416769984c329e4378f1d558b63e341020525de4"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"json_formatter": {:hex, :json_formatter, "0.2.0", "e36ec571f2420c5afa4f744169eb5b2e7ed2ae0cadaf3cd39ef43e1d11fef29a", [:mix], [{:jason, "~> 1.3", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "c8b8c354420b88a3edd0f5a23d4ca8a6ca6090091df6965894631eb20fc7df31"},
"rewrite": {:hex, :rewrite, "1.0.0", "fddda21eb62c2b9dbd6cf35b39984d02761a63c25de2947ceab7ffe2729fa2e5", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}, {:text_diff, "~> 0.1", [hex: :text_diff, repo: "hexpm", optional: false]}], "hexpm", "701975d9d0c21b1f40a82881e489aca6b53893f095f318978c6d2899299a514b"},
"rewrite": {:hex, :rewrite, "1.1.0", "69f869fd6460cde30ae90b09895e42d1840cbdb903802650a28a6959c4777971", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}, {:text_diff, "~> 0.1", [hex: :text_diff, repo: "hexpm", optional: false]}], "hexpm", "adcdd3623a055703c8ecfe4dd24f695ff9d9e1ec6b09c023080999808ff4096e"},
"sourceror": {:hex, :sourceror, "1.7.1", "599d78f4cc2be7d55c9c4fd0a8d772fd0478e3a50e726697c20d13d02aa056d4", [:mix], [], "hexpm", "cd6f268fe29fa00afbc535e215158680a0662b357dc784646d7dff28ac65a0fc"},
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
"text_diff": {:hex, :text_diff, "0.1.0", "1caf3175e11a53a9a139bc9339bd607c47b9e376b073d4571c031913317fecaa", [:mix], [], "hexpm", "d1ffaaecab338e49357b6daa82e435f877e0649041ace7755583a0ea3362dbd7"},
Expand Down
17 changes: 5 additions & 12 deletions lib/recode/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Recode.Config do
dry: false,
color: true,
verbose: false,
inputs: ["{mix,.formatter}.exs", "{apps,config,lib,test}/**/*.{ex,exs}"],
inputs: :formatter,
formatters: [Recode.CLIFormatter],
tasks: [
{Recode.Task.AliasExpansion, []},
Expand Down Expand Up @@ -72,6 +72,9 @@ defmodule Recode.Config do
color: <%= @config[:color] %>,
# Can also be set/reset with `--verbose`/`--no-verbose`.
verbose: <%= @config[:verbose] %>,
# Inputs can be a path, glob expression or list of paths and glob expressions.
# With the atom :formatter the inputs from .formatter.exs are
# used. also allowed in the list mentioned above.
# Can be overwritten by calling `mix recode "lib/**/*.ex"`.
inputs: <%= inspect @config[:inputs] %>,
formatters: <%= inspect @config[:formatters] %>,
Expand Down Expand Up @@ -146,11 +149,7 @@ defmodule Recode.Config do
def read(path \\ @config_filename) when is_binary(path) do
case File.exists?(path) do
true ->
config =
path
|> Code.eval_file()
|> elem(0)
|> update_inputs()
config = path |> Code.eval_file() |> elem(0)

{:ok, config}

Expand Down Expand Up @@ -185,10 +184,4 @@ defmodule Recode.Config do
defp validate_tasks(config) do
if Keyword.has_key?(config, :tasks), do: :ok, else: {:error, :no_tasks}
end

defp update_inputs(config) do
Keyword.update(config, :inputs, [], fn inputs ->
inputs |> List.wrap() |> Enum.map(fn input -> GlobEx.compile!(input) end)
end)
end
end
47 changes: 35 additions & 12 deletions lib/recode/runner/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ defmodule Recode.Runner.Impl do

tasks = tasks(config)

dot_formatter = dot_formatter()

config =
config
|> update_config()
|> update_config(dot_formatter)
|> start_event_manager()

project =
config
|> project()
|> project(dot_formatter)
|> notify(:prepared, config, time(start_recode))
|> Rewrite.format!(by: Recode.Task.Format)

Expand All @@ -46,24 +48,30 @@ defmodule Recode.Runner.Impl do
@impl true
def run(content, config, path \\ "source.ex") do
tasks = tasks(config)
config = update_config(config)
formatter_opts = Keyword.get(config, :formatter_opts, [])

dot_formatter =
DotFormatter.from_formatter_opts(formatter_opts, remove_plugins: [Recode.FormatterPlugin])

config = update_config(config, dot_formatter)

source =
content
|> Source.Ex.from_string(path: path)
|> Source.format!(by: Recode.Task.Format, dot_formatter: dot_formatter)

%Source{filetype: %Source.Ex{}} = source

tasks
|> update_opts(config)
|> correctors_first()
|> Enum.reduce(source, fn {module, opts}, source ->
case exclude?(module, source, config) do
true -> source
false -> module.run(source, Keyword.put(opts, :dot_formatter, dot_formatter))
true ->
source

false ->
module.run(source, Keyword.put(opts, :dot_formatter, dot_formatter))
end
end)
|> Source.get(:content)
Expand Down Expand Up @@ -171,12 +179,16 @@ defmodule Recode.Runner.Impl do
)
end

defp exclude?(task, source, config) do
# For now, all sources without a filetype Source.Ex are direclty excluded.
# For the Source.Ex sources we check whether the source is excluded.
defp exclude?(task, %Source{filetype: %Source.Ex{}} = source, config) do
config
|> config(task, :exclude)
|> Enum.any?(fn glob -> GlobEx.match?(glob, source.path) end)
end

defp exclude?(_task, _source, _config), do: true

defp get_cli_opts(config, key, default) do
config
|> Keyword.get(:cli_opts, [])
Expand All @@ -201,8 +213,8 @@ defmodule Recode.Runner.Impl do

defp event_manager(config), do: Keyword.fetch!(config, :event_manager)

defp project(config) do
inputs = config |> Keyword.fetch!(:inputs) |> List.wrap()
defp project(config, dot_formatter) do
inputs = Keyword.fetch!(config, :inputs)

if inputs == ["-"] do
stdin = IO.stream(:stdio, :line) |> Enum.to_list() |> IO.iodata_to_binary()
Expand All @@ -214,7 +226,7 @@ defmodule Recode.Runner.Impl do
else
Rewrite.new!(inputs,
filetypes: [Source.Ex, {Source, owner: Recode}],
dot_formatter: dot_formatter()
dot_formatter: dot_formatter
)
end
end
Expand Down Expand Up @@ -296,18 +308,29 @@ defmodule Recode.Runner.Impl do
Enum.concat(Map.get(groups, true, []), Map.get(groups, false, []))
end

defp update_config(config) do
Keyword.update!(config, :tasks, fn tasks -> update_config(tasks, :exclude) end)
defp update_config(config, dot_formatter) do
config
|> Keyword.update!(:tasks, fn tasks -> update_tasks(tasks) end)
|> Keyword.update!(:inputs, fn inputs -> update_inputs(inputs, dot_formatter) end)
end

defp update_config(tasks, :exclude) do
defp update_tasks(tasks) do
Enum.map(tasks, fn {task, config} ->
config = Keyword.update(config, :exclude, [], &compile_globs/1)

{task, config}
end)
end

defp update_inputs(inputs, dot_formatter) do
inputs
|> List.wrap()
|> Enum.flat_map(fn
:formatter -> if dot_formatter.path, do: DotFormatter.inputs(dot_formatter), else: []
input -> [input]
end)
end

defp compile_globs(exclude) do
exclude
|> List.wrap()
Expand Down
42 changes: 42 additions & 0 deletions test/recode/runner/impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,48 @@ defmodule Recode.Runner.ImplTest do
end
end
end

test "reads inputs from .formatter.exs", context do
in_tmp context do
config = config(inputs: :formatter, dry: false, tasks: [{SinglePipe, []}])

File.write!(".formatter.exs", ~s|[inputs: "lib/**/*.ex", subdirectories: ["bar"]]|)
File.mkdir!("lib")
File.write!("lib/foo.ex", " foo = :foo")
File.mkdir!("bar")
File.write!("bar/.formatter.exs", ~s|[inputs: "**/*.ex"]|)
File.write!("bar/foo.ex", " foo = :foo")

capture_io(fn ->
assert {:ok, 0} = Runner.run(config)
end)

code = "foo = :foo\n"
assert File.read!("bar/foo.ex") == code
assert File.read!("lib/foo.ex") == code
end
end

test "reads inputs from .formatter.exs and glob expression", context do
in_tmp context do
config =
config(inputs: [:formatter, "bar/**/*.ex"], dry: false, tasks: [{SinglePipe, []}])

File.write!(".formatter.exs", ~s|[inputs: "lib/**/*.ex"]|)
File.mkdir!("lib")
File.write!("lib/foo.ex", " :foo |> foo")
File.mkdir!("bar")
File.write!("bar/foo.ex", " :foo |> foo")

capture_io(fn ->
assert {:ok, 0} = Runner.run(config)
end)

code = "foo(:foo)\n"
assert File.read!("lib/foo.ex") == code
assert File.read!("bar/foo.ex") == code
end
end
end

describe "run/3" do
Expand Down

0 comments on commit 4171691

Please sign in to comment.