From 82bf163c130d78631b1615ea3604996108cd066c Mon Sep 17 00:00:00 2001 From: Christian Koch Date: Mon, 27 Sep 2021 21:55:22 -0500 Subject: [PATCH] update CLI output --- lib/ex_factor.ex | 3 ++- lib/ex_factor/callers.ex | 7 ------- lib/ex_factor/extractor.ex | 1 + lib/mix/tasks/ex_factor.ex | 38 +++++++++++++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/ex_factor.ex b/lib/ex_factor.ex index 78c2e73..5608fed 100644 --- a/lib/ex_factor.ex +++ b/lib/ex_factor.ex @@ -43,12 +43,13 @@ defmodule ExFactor do defp format(%{path: nil} = struct), do: struct - defp format(%{additions: adds, changes: changes, removals: removals}) do + defp format(%{additions: adds, changes: changes, removals: removals} = output) do %{ additions: format(adds), changes: format(changes), removals: format(removals) } + output end defp format(list) when is_list(list) do diff --git a/lib/ex_factor/callers.ex b/lib/ex_factor/callers.ex index e0814af..e4c38f2 100644 --- a/lib/ex_factor/callers.ex +++ b/lib/ex_factor/callers.ex @@ -19,15 +19,8 @@ defmodule ExFactor.Callers do end def callers(mod, func, arity) do - # Code.compiler_options(parser_options: [columns: true]) - # Mix.Task.rerun("compile.elixir", ["--force", "--tracer", "ExFactor.CallerTracer"]) - Mix.Tasks.Xref.calls([]) |> Enum.filter(fn x -> - # {:ok, _} = Import2Alias.Server.start_link(elem(x.callee, 0)) - # entries = Import2Alias.Server.entries() - # |> IO.inspect(label: "") - x.callee == {cast(mod), cast(func), arity} end) end diff --git a/lib/ex_factor/extractor.ex b/lib/ex_factor/extractor.ex index 188a3e5..7d36519 100644 --- a/lib/ex_factor/extractor.ex +++ b/lib/ex_factor/extractor.ex @@ -19,6 +19,7 @@ defmodule ExFactor.Extractor do Optional keys: - :source_path Specify an alternate (non-standard) path for the source module - :target_path Specify an alternate (non-standard) path for the destination module + - :dry_run Don't write any updates """ def emplace(opts) do diff --git a/lib/mix/tasks/ex_factor.ex b/lib/mix/tasks/ex_factor.ex index b56daa4..6df5211 100644 --- a/lib/mix/tasks/ex_factor.ex +++ b/lib/mix/tasks/ex_factor.ex @@ -22,10 +22,11 @@ defmodule Mix.Tasks.ExFactor do - `:arity` the arity of function to move. - `:target` is the fully-qualified destination for the removed function. If the moduel does not exist, it will be created. - Optional command line args: --source_path, --target_path, --dryrun + Optional command line args: --source_path, --target_path, --dryrun, --verbose - `: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. + - `:verbose` (Default false) include the :state and :file_contents key values. Example Usage: ``` @@ -42,6 +43,7 @@ defmodule Mix.Tasks.ExFactor do strict: [ arity: :integer, dryrun: :boolean, + verbose: :boolean, function: :string, key: :string, module: :string, @@ -54,6 +56,7 @@ defmodule Mix.Tasks.ExFactor do parsed_opts |> options() |> ExFactor.refactor() + |> cli_output(parsed_opts) end defp options(opts) do @@ -63,4 +66,37 @@ defmodule Mix.Tasks.ExFactor do |> Keyword.put(:target_module, Keyword.fetch!(opts, :target)) |> Keyword.put(:dry_run, Keyword.get(opts, :dryrun, false)) end + + defp cli_output(map, opts) do + verbose = Keyword.get(opts, :verbose, 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) + 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"])])) + handle_entry(entry, color, verbose) + IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, "================================================================================"])])) + IO.puts("") + end + + defp handle_entry(entries, color, verbose) when is_list(entries) do + Enum.map(entries, &handle_entry(&1, color, verbose)) + end + + defp handle_entry(entry, color, true) do + 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 + 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 end