Skip to content

Commit

Permalink
Get the CLI task to work
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 20, 2021
1 parent 0c4704a commit 0751c11
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
15 changes: 11 additions & 4 deletions lib/ex_factor/changer.ex
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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],
Expand All @@ -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)
Expand All @@ -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
Expand Down
16 changes: 7 additions & 9 deletions lib/ex_factor/extractor.ex
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions 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
8 changes: 3 additions & 5 deletions lib/mix/tasks/ex_factor/refactor.ex
Expand Up @@ -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.
"""

Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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
16 changes: 10 additions & 6 deletions test/ex_factor/changer_test.exs
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0751c11

Please sign in to comment.