diff --git a/lib/ex_factor/changer.ex b/lib/ex_factor/changer.ex index b4bdf01..6c7c350 100644 --- a/lib/ex_factor/changer.ex +++ b/lib/ex_factor/changer.ex @@ -32,7 +32,7 @@ defmodule ExFactor.Changer do # modified values source_string = Util.module_to_string(source_module) - source_modules = Module.split(source_module) + source_modules = String.split(source_module, ".") source_alias = Enum.at(source_modules, -1) target_alias = preferred_alias(list, target_module) source_alias_alt = find_alias_as(list, source_module) @@ -42,13 +42,17 @@ defmodule ExFactor.Changer do String.match?(elem, ~r/#{source_string}\.#{source_function}/) -> elem = String.replace(elem, source_module, target_module) {:changed, [elem | acc]} + String.match?(elem, ~r/#{source_alias}\.#{source_function}/) -> elem = String.replace(elem, source_alias, target_alias) {:changed, [elem | acc]} + String.match?(elem, ~r/#{source_alias_alt}\.#{source_function}/) -> elem = String.replace(elem, source_alias_alt, target_alias) {:changed, [elem | acc]} - true -> {state, [elem | acc]} + + true -> + {state, [elem | acc]} end end) |> maybe_add_alias(opts) @@ -57,6 +61,7 @@ defmodule ExFactor.Changer do defp find_alias_as(list, module) do aalias = Enum.find(list, "", fn el -> el =~ "alias #{Util.module_to_string(module)}" end) + if String.match?(aalias, ~r/, as: /) do aalias |> String.split("as:", trim: true) @@ -67,7 +72,7 @@ defmodule ExFactor.Changer do end defp preferred_alias(list, target_module) do - target_modules = Module.split(target_module) + target_modules = String.split(target_module, ".") target_alias = Enum.at(target_modules, -1) target_alias_alt = find_alias_as(list, target_module) @@ -99,6 +104,7 @@ defmodule ExFactor.Changer do defp write_file({state, contents_list}, _, target_path, _dry_run) do contents = list_to_string(contents_list) File.write(target_path, contents, [:write]) + %{ path: target_path, state: [state], @@ -114,6 +120,7 @@ defmodule ExFactor.Changer do end defp maybe_add_alias({:unchanged, contents_list}, _), do: {:unchanged, contents_list} + defp maybe_add_alias({state, contents_list}, opts) do source_module = Keyword.fetch!(opts, :source_module) source_string = Util.module_to_string(source_module) @@ -125,7 +132,7 @@ defmodule ExFactor.Changer do else contents_list |> Enum.reduce([], fn elem, acc -> - if (elem =~ "alias #{source_string}") do + if elem =~ "alias #{source_string}" do new_alias = String.replace(elem, source_string, target_string) [new_alias | [elem | acc]] else diff --git a/lib/ex_factor/extractor.ex b/lib/ex_factor/extractor.ex index f575e08..93c8e13 100644 --- a/lib/ex_factor/extractor.ex +++ b/lib/ex_factor/extractor.ex @@ -54,16 +54,14 @@ defmodule ExFactor.Extractor do |> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end) _ -> - contents = - quote generated: true do - defmodule unquote(target_module) do - @moduledoc false - unquote(Macro.unescape_string(string_fns)) - end + quote generated: true do + defmodule unquote(target_module) do + @moduledoc false + unquote(Macro.unescape_string(string_fns)) end - |> Macro.to_string() - - write_file(target_path, contents, source_module, dry_run) + end + |> Macro.to_string() + |> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end) end end diff --git a/lib/ex_factor/util.ex b/lib/ex_factor/util.ex index aca3aff..e3ecc52 100644 --- a/lib/ex_factor/util.ex +++ b/lib/ex_factor/util.ex @@ -1,9 +1,11 @@ defmodule ExFactor.Util do @moduledoc false - def module_to_string(module) do - module - |> Module.split() - |> Enum.join(".") + # TODO: we probably don't even need this function + # use this as a dead code finder + def module_to_string(module) when is_atom(module) do + to_string(module) end + + def module_to_string(module), do: module end diff --git a/lib/mix/tasks/ex_factor/refactor.ex b/lib/mix/tasks/ex_factor/refactor.ex index 5e1c13b..88d80ce 100644 --- a/lib/mix/tasks/ex_factor/refactor.ex +++ b/lib/mix/tasks/ex_factor/refactor.ex @@ -23,6 +23,7 @@ defmodule Mix.Tasks.ExFactor.Refactor do Optional command line args: --source_path, --target_path - `:target_path` Specify an alternate (non-standard) path for the source file. - `:source_path` Specify an alternate (non-standard) path for the destination file. + - `:dryrun` Don't write any updates, only return the built results. """ @@ -33,7 +34,7 @@ defmodule Mix.Tasks.ExFactor.Refactor do OptionParser.parse(argv, strict: [ arity: :integer, - dry_run: :boolean, + dryrun: :boolean, function: :string, key: :string, module: :string, @@ -43,10 +44,6 @@ defmodule Mix.Tasks.ExFactor.Refactor do ] ) - # parsed_opts - # |> IO.inspect(label: "PARSED ARGS") - - # Mix.Task.run("app.start") parsed_opts |> options() |> ExFactor.refactor() @@ -57,5 +54,6 @@ defmodule Mix.Tasks.ExFactor.Refactor do |> Keyword.put(:source_function, Keyword.fetch!(opts, :function)) |> Keyword.put(:source_module, Keyword.fetch!(opts, :module)) |> Keyword.put(:target_module, Keyword.fetch!(opts, :target)) + |> Keyword.put(:dry_run, Keyword.get(opts, :dryrun, false)) end end diff --git a/test/ex_factor/changer_test.exs b/test/ex_factor/changer_test.exs index 4a3da22..b3ab3b7 100644 --- a/test/ex_factor/changer_test.exs +++ b/test/ex_factor/changer_test.exs @@ -95,9 +95,11 @@ defmodule ExFactor.ChangerTest do caller_list = String.split(caller, "\n") assert caller =~ "alias ExFactor.Tmp.TargetModule" assert caller =~ "TargetModule.refactor1(arg_a)" - assert 1 == Enum.count(caller_list, fn el -> - el =~ "alias ExFactor.Tmp.TargetModule" - end) + + assert 1 == + Enum.count(caller_list, fn el -> + el =~ "alias ExFactor.Tmp.TargetModule" + end) end test "handle alias exists with :as" do @@ -143,9 +145,11 @@ defmodule ExFactor.ChangerTest do caller_list = String.split(caller, "\n") assert caller =~ "alias ExFactor.Tmp.TargetModule" assert caller =~ "TM.refactor1(arg_a)" - assert 1 == Enum.count(caller_list, fn el -> - el =~ "alias ExFactor.Tmp.TargetModule" - end) + + assert 1 == + Enum.count(caller_list, fn el -> + el =~ "alias ExFactor.Tmp.TargetModule" + end) end test "it finds all the callers of a module by an alias, function, and arity, and updates the calls to the new module " do