Skip to content

Commit

Permalink
Comments and typos
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Jun 15, 2022
1 parent 5b51659 commit bafa887
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/ex_factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule ExFactor do
By identifying a Module, function name, and arity, it will identify all non-test usages
and extract them to a new Module.
If the Module exists, it add the function to the end of the file and change all calls to the
If the Module exists, it adds the function to the end of the file and change all calls to the
new module's name.
"""

Expand Down
12 changes: 10 additions & 2 deletions lib/ex_factor/changer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ defmodule ExFactor.Changer do
end

defp change_alias({state, contents_list}, target_string) do
elem = Enum.find(contents_list, &(str_match?(&1, target_string, "alias")))
elem = Enum.find(contents_list, &str_match?(&1, target_string, "alias"))

if elem do
{[:alias_exists | state], contents_list}
else
Expand All @@ -169,15 +170,21 @@ defmodule ExFactor.Changer do
cond do
:alias_changed in state ->
{state, contents_list}

:alias_added in state ->
{state, contents_list}

:alias_exists in state ->
{state, contents_list}

:changed in state ->
index = Enum.find_index(contents_list, fn el -> str_match?(el, target_string, "alias") end)
index =
Enum.find_index(contents_list, fn el -> str_match?(el, target_string, "alias") end)

index = index || 2
contents_list = List.insert_at(contents_list, index - 1, "alias #{target_string}")
{set_state(state, :alias_added), contents_list}

true ->
{state, contents_list}
end
Expand All @@ -197,6 +204,7 @@ defmodule ExFactor.Changer do
Enum.find_index(contents_list, fn el -> str_match?(el, source_alias_alt, "import") end)

new_state = set_state(state, :import_added)

if index do
{new_state, List.insert_at(contents_list, index + 1, "import #{target_string}")}
else
Expand Down
97 changes: 92 additions & 5 deletions lib/mix/tasks/ex_factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,49 @@ defmodule Mix.Tasks.ExFactor do

defp cli_output(map, opts) do
verbose = Keyword.get(opts, :verbose, false)
# dry_run = Keyword.get(opts, :dryrun, false)

format_entry(Map.get(map, :additions), "Additions", :light_cyan_background, verbose)
format_entry(Map.get(map, :changes), "Changes", :light_green_background, verbose)
format_entry(Map.get(map, :removals), "Removals", :light_red_background, verbose)
message(false)
end

defp format_entry(entry, title, color, verbose) do
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, title])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, "================================================================================"])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"])]))

IO.puts(
IO.ANSI.format([
color,
IO.ANSI.format([
:black,
"================================================================================"
])
])
)

IO.puts(
IO.ANSI.format([
color,
IO.ANSI.format([
:black,
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"
])
])
)

handle_entry(entry, color, verbose)
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, "================================================================================"])]))

IO.puts(
IO.ANSI.format([
color,
IO.ANSI.format([
:black,
"================================================================================"
])
])
)

IO.puts("")
end

Expand All @@ -89,14 +120,70 @@ defmodule Mix.Tasks.ExFactor do
end

defp handle_entry(entry, color, true) do
handle_entry(entry, color , false)
handle_entry(entry, color, false)
IO.puts(" File contents: \n#{entry.file_contents}")
IO.puts(" State: #{inspect(entry.state)}")
end
defp handle_entry(entry, color , false) do

defp handle_entry(entry, color, false) do
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, " Module: #{entry.module}"])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, " Path: #{entry.path}"])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, " Message: #{entry.message}"])]))
end

@message """
`ExFactor` (by design), does not change test files.
There are two important and practical reason for this.
Reason1:
The test failures provide a safety net to help ensure that the ExFactor-ed functions
continue to behave as expected.
Reason2:
Because .exs files are evaluated at runtime the introspection provided by the compiler
is not available.
In future revisions, we hope to address this issue, but for now, refactoring test files
remains your responsibility.
"""
defp message(true), do: ""

defp message(false) do
IO.puts(
IO.ANSI.format([
:magenta_background,
IO.ANSI.format([
:bright,
"⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠"
])
])
)

IO.puts(IO.ANSI.format([:magenta_background, IO.ANSI.format([:bright, "IMPORTANT NOTE:"])]))

IO.puts(
IO.ANSI.format([
:magenta_background,
IO.ANSI.format([
:bright,
"✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖"
])
])
)

IO.puts(
IO.ANSI.format([
:magenta_background,
IO.ANSI.format([
:bright,
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
])
])
)

IO.puts(IO.ANSI.format([:magenta_background, IO.ANSI.format([:bright, ""])]))
IO.puts(IO.ANSI.format([:magenta_background, IO.ANSI.format([:bright, @message])]))
end
end
7 changes: 5 additions & 2 deletions test/ex_factor/changer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ defmodule ExFactor.ChangerTest do
end)
end


test "handle alias exists with :as" do
content = """
defmodule ExFactor.Tmp.SourceMod do
Expand Down Expand Up @@ -379,7 +378,11 @@ defmodule ExFactor.ChangerTest do

refute caller =~ "alias ExFactor.Tmp.TargetModule"
refute caller =~ "TargetModule.refactor1(arg_a)"
assert Enum.find(change_map.state, fn el -> String.match?(Atom.to_string(el), ~r/alias_/) end)

assert Enum.find(change_map.state, fn el ->
String.match?(Atom.to_string(el), ~r/alias_/)
end)

# change_map.state == [:dry_run, :alias_changed, :changed]
assert change_map.message == "--dry_run changes to make"
end
Expand Down
2 changes: 1 addition & 1 deletion test/support/support.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ExFactor.Support do
@moduledoc """
Support moduel for `ExFactor` testing.
Support module for `ExFactor` testing.
"""

# use alias as: to verify the caller is found.
Expand Down

0 comments on commit bafa887

Please sign in to comment.