diff --git a/README.md b/README.md index fb5997a..88ec4aa 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,10 @@ to a new file/module. At this time, ExFactor cannot change the function arity. - [] update test file refs by CLI option - [] format changes - [] CLI output, list files changed and created. - - [] dry-run option + - [X] dry-run option - [] ElixirLS integration for VSCode? - [] Write the module code to rename usages of the refactored function + - [] guthub actions, run test suite ## Installation diff --git a/lib/ex_factor.ex b/lib/ex_factor.ex index 09a1f9f..93fe7a7 100644 --- a/lib/ex_factor.ex +++ b/lib/ex_factor.ex @@ -10,9 +10,14 @@ defmodule ExFactor do """ alias ExFactor.Extractor + alias ExFactor.Remover @doc """ Call Extractor module emaplce/1 """ - def refactor(opts), do: Extractor.emplace(opts) + def refactor(opts) do + emplace = Extractor.emplace(opts) + remove = Remover.remove(opts) + {emplace, remove} + end end diff --git a/lib/ex_factor/extractor.ex b/lib/ex_factor/extractor.ex index 2b3af05..97e1764 100644 --- a/lib/ex_factor/extractor.ex +++ b/lib/ex_factor/extractor.ex @@ -33,7 +33,7 @@ defmodule ExFactor.Extractor do to_extract = block_contents |> Neighbors.walk(source_function, arity) |> Enum.map(&(Macro.to_string(&1))) - |> IO.inspect(label: "to string") + # |> IO.inspect(label: "to string") string_fns = Enum.join(to_extract, "\n") diff --git a/lib/ex_factor/remover.ex b/lib/ex_factor/remover.ex index ea47595..4e543bc 100644 --- a/lib/ex_factor/remover.ex +++ b/lib/ex_factor/remover.ex @@ -10,8 +10,9 @@ defmodule ExFactor.Remover do """ def remove(opts) do source_function = Keyword.fetch!(opts, :source_function) + source_module = Keyword.get(opts, :source_module) arity = Keyword.fetch!(opts, :arity) - source_path = Keyword.fetch!(opts, :source_path) + source_path = Keyword.get(opts, :source_path, path(source_module)) dry_run = Keyword.get(opts, :dry_run, false) # |> IO.inspect(label: "REMOVE source_path") @@ -60,4 +61,8 @@ defmodule ExFactor.Remover do defp write_file(path, contents, _) do File.write(path, contents, [:write]) end + + defp path(module) do + Path.join(["lib", Macro.underscore(module) <> ".ex"]) + end end diff --git a/lib/mix/tasks/ex_factor/refactor.ex b/lib/mix/tasks/ex_factor/refactor.ex index 57e44cf..11e827f 100644 --- a/lib/mix/tasks/ex_factor/refactor.ex +++ b/lib/mix/tasks/ex_factor/refactor.ex @@ -42,8 +42,8 @@ defmodule Mix.Tasks.ExFactor.Refactor do ] ) - parsed_opts - |> IO.inspect(label: "PARSED ARGS") + # parsed_opts + # |> IO.inspect(label: "PARSED ARGS") # Mix.Task.run("app.start") parsed_opts diff --git a/test/ex_factor/parser_test.exs b/test/ex_factor/parser_test.exs index 3e4f8b9..27f97cb 100644 --- a/test/ex_factor/parser_test.exs +++ b/test/ex_factor/parser_test.exs @@ -454,7 +454,7 @@ defmodule ExFactor.ParserTest do assert List.first(list) == "defmodule ExFactorSampleModule do" assert Enum.at(list, -2) == "end" - assert {:defmodule, [do: [line: 1], end: [line: 8], line: 1], _} = ast + assert {:defmodule, [do: [line: 1], end: [line: 9], line: 1], _} = ast end test "raises an exception for an invalid file path" do diff --git a/test/ex_factor_test.exs b/test/ex_factor_test.exs index c4ebb08..a28976e 100644 --- a/test/ex_factor_test.exs +++ b/test/ex_factor_test.exs @@ -1,13 +1,21 @@ defmodule ExFactorTest do use ExUnit.Case + setup_all do + File.mkdir_p("lib/ex_factor/tmp") + + on_exit(fn -> + File.rm_rf("lib/ex_factor/tmp") + end) + end + describe "refactor/1" do test "it refactors the functions into a new module, specified in opts" do - File.rm("lib/ex_factor/source_module.ex") - File.rm("lib/ex_factor/target_module.ex") + File.rm("lib/ex_factor/tmp/source_module.ex") + File.rm("lib/ex_factor/tmp/target_module.ex") content = """ - defmodule ExFactor.SourceModule do + defmodule ExFactor.Tmp.SourceModule do @somedoc "This is somedoc" @doc "this is some documentation for refactor1/1" def refactor1(arg1) do @@ -19,10 +27,10 @@ defmodule ExFactorTest do end """ - File.write("lib/ex_factor/source_module.ex", content) + File.write("lib/ex_factor/tmp/source_module.ex", content) content = """ - defmodule ExFactor.TargetModule do + defmodule ExFactor.Tmp.TargetModule do @somedoc "This is somedoc TargetModule" @doc "some docs" def pub_exists(arg_exists) do @@ -34,30 +42,29 @@ defmodule ExFactorTest do end """ - File.write("lib/ex_factor/target_module.ex", content) + File.write("lib/ex_factor/tmp/target_module.ex", content) opts = [ - target_module: ExFactor.TargetModule, - source_module: ExFactor.SourceModule, + target_module: ExFactor.Tmp.TargetModule, + source_module: ExFactor.Tmp.SourceModule, source_function: :refactor1, arity: 1 ] - ExFactor.refactor(opts) + results = ExFactor.refactor(opts) - file = File.read!("lib/ex_factor/target_module.ex") + file = File.read!("lib/ex_factor/tmp/target_module.ex") assert file =~ "def(refactor1(arg1)) do" assert file =~ "def(refactor1([])) do" assert file =~ " @doc \"some docs\"" assert file =~ "def pub_exists(arg_exists) do" - file = File.read!("lib/ex_factor/source_module.ex") - |> IO.inspect(label: "") + file = File.read!("lib/ex_factor/tmp/source_module.ex") + # |> IO.inspect(label: "") refute file =~ "def refactor1(arg1) do" refute file =~ "def refactor1([]) do" - # File.rm("lib/ex_factor/source_module.ex") - # File.rm("lib/ex_factor/target_module.ex") + results |> IO.inspect(label: "ExFactorTest results") end end end