Skip to content

Commit

Permalink
move to callers module
Browse files Browse the repository at this point in the history
  • Loading branch information
ckochx committed Sep 8, 2021
1 parent a9ca141 commit 8bc9c52
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 87 deletions.
25 changes: 0 additions & 25 deletions lib/ex_factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,4 @@ defmodule ExFactor do
@moduledoc """
Documentation for `ExFactor`.
"""

alias ExFactor.Parser

def all_fns(input), do: Parser.all_functions(input)

@doc """
use `mix xref` list all the callers of a given module.
"""
# @spec callers(module()) :: list(map())
def callers(mod) do
System.cmd("mix", ["xref", "callers", "#{mod}"], env: [{"MIX_ENV", "test"}])
|> elem(0)
|> String.trim()
|> String.split("\n")
|> mangle_list()
end

defp mangle_list(["Compiling" <> _ | tail]), do: mangle_list(tail)

defp mangle_list(list) do
Enum.map(list, fn string ->
[path, type] = String.split(string, " ")
%{filepath: path, dependency_type: type}
end)
end
end
30 changes: 30 additions & 0 deletions lib/ex_factor/callers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule ExFactor.Callers do
@moduledoc """
Documentation for `ExFactor.Callers`.
"""

alias ExFactor.Parser

def all_fns(input), do: Parser.all_functions(input)

@doc """
use `mix xref` list all the callers of a given module.
"""
# @spec callers(module()) :: list(map())
def callers(mod) do
System.cmd("mix", ["xref", "callers", "#{mod}"], env: [{"MIX_ENV", "test"}])
|> elem(0)
|> String.trim()
|> String.split("\n")
|> mangle_list()
end

defp mangle_list(["Compiling" <> _ | tail]), do: mangle_list(tail)

defp mangle_list(list) do
Enum.map(list, fn string ->
[path, type] = String.split(string, " ")
%{filepath: path, dependency_type: type}
end)
end
end
3 changes: 2 additions & 1 deletion lib/ex_factor/evaluater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ defmodule ExFactor.Evaluater do
Documentation for `ExFactor.Evaluater`.
"""

alias ExFactor.Callers
alias ExFactor.Parser

def modules_to_refactor(module, func, arity) do
module
|> ExFactor.callers()
|> Callers.callers()
|> Enum.map(fn %{filepath: filepath} ->
filepath
|> Parser.public_functions()
Expand Down
19 changes: 1 addition & 18 deletions lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,11 @@ defmodule ExFactor.Extractor do
source_function = Keyword.get(opts, :source_function)
arity = Keyword.get(opts, :arity)
target_function = Keyword.get(opts, :target_function, source_function)

target_path = Keyword.get(opts, :target_path, path(target_module))
|> IO.inspect(label: "target_path")
source_path = Keyword.get(opts, :source_path, path(source_module))
# Macro.underscore(source_module)

# source_path = Macro.underscore(source_module) <> ".ex"
# target_path = Macro.underscore(target_module) <> ".ex"

# Path.join([Mix.Project.app_path(), target_path])
# |> IO.inspect(label: "")

File.exists?(source_path) |> IO.inspect(label: "exists")

{_ast, functions} = Parser.public_functions(source_path)

# ast |> IO.inspect(label: "")
map = Enum.find(functions, &(&1.name == source_function && &1.arity == arity))
# |> IO.inspect(label: "source function")

# map.ast
# |> Macro.to_string()
# |> IO.inspect(label: "source AST")

case File.exists?(target_path) do
true ->
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_factor/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule ExFactor.Parser do
filepath
|> File.read!()
|> Code.string_to_quoted()
|> IO.inspect(label: "all funxs")
# |> IO.inspect(label: "all funxs")
|> public_functions()
end

Expand Down
14 changes: 14 additions & 0 deletions test/ex_factor/callers_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ExFactor.CallersTest do
use ExUnit.Case
alias ExFactor.Callers

describe "callers/1" do
test "it should report callers of a module" do
[one, _two, _three, four] = Callers.callers(ExFactor.Parser)

assert one.dependency_type == "(runtime)"
assert one.filepath == "lib/ex_factor/callers.ex"
assert four.filepath == "test/support/support.ex"
end
end
end
6 changes: 2 additions & 4 deletions test/ex_factor/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ defmodule ExFactor.ExtractorTest do
content = """
defmodule ExFactorSampleModule do
@somedoc "This is somedoc"
# no aliases
# a comment and no aliases
_docp = "here's an arbitrary module underscore"
def pub1(arg1) do
:ok
end
end
"""

File.write("test/support/source_module.ex", content)

# target_path = Macro.underscore(target_module)
target_path = "test/support/target_module.ex"
File.rm(target_path)

Expand All @@ -46,7 +45,6 @@ defmodule ExFactor.ExtractorTest do
content = """
defmodule ExFactor.SourceModule do
@somedoc "This is somedoc"
# no aliases
def pub1(arg1) do
:ok
end
Expand Down
59 changes: 31 additions & 28 deletions test/ex_factor/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,37 @@ defmodule ExFactor.ParserTest do
assert f1.name == :pub2
end

# test "ast references private fns and external fns called by a public fn" do
# content = """
# defmodule ExFactorOtherModule do
# def other_pub1(arg1), do: arg1
# end
# """
# File.write("test/support/other_module.ex", content)

# {ast, fns} = """
# defmodule ExFactorSampleModule do
# def pub1(arg1) do
# arg1
# |> ExFactorOtherModule.other_pub1()
# |> priv2()
# end

# defp priv2(arg2) do
# :private
# end
# end
# """
# |> Code.string_to_quoted()
# |> Parser.public_functions()
# |> IO.inspect(label: "")
# # assert f2.name == :pub1
# # assert f1.name == :pub2
# File.rm("test/support/other_module.ex")
# end
test "ast references private fns and external fns called by a public fn" do
content = """
defmodule ExFactorOtherModule do
def other_pub1(arg1), do: arg1
end
"""
File.write("test/support/other_module.ex", content)

{ast, fns} = """
defmodule ExFactorSampleModule do
def pub1(arg1) do
inter = ExFactorOtherModule.other_pub1(arg1)
priv2(inter)
end
defp priv2(arg2) do
Map.get(args2, :key)
end
end
"""
|> Code.string_to_quoted()
|> Parser.public_functions()
|> IO.inspect(label: "")

m = List.first(fns)
Macro.decompose_call(m.ast)
|> IO.inspect(label: "decompose_call")
# assert f2.name == :pub1
# assert f1.name == :pub2
File.rm("test/support/other_module.ex")
end
end

describe "private_functions/1" do
Expand Down
18 changes: 9 additions & 9 deletions test/ex_factor_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule ExFactorTest do
use ExUnit.Case
# use ExUnit.Case

describe "callers/1" do
test "it should report callers of a module" do
[one, _three, two] = ExFactor.callers(ExFactor.Parser)
# describe "callers/1" do
# test "it should report callers of a module" do
# [one, _three, two] = ExFactor.callers(ExFactor.Parser)

assert one.dependency_type == "(runtime)"
assert one.filepath == "lib/ex_factor.ex"
assert two.filepath == "test/support/support.ex"
end
end
# assert one.dependency_type == "(runtime)"
# assert one.filepath == "lib/ex_factor.ex"
# assert two.filepath == "test/support/support.ex"
# end
# end
end
3 changes: 2 additions & 1 deletion test/support/support.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ defmodule ExFactor.Support do

# use alias as: to verify the caller is found.
alias ExFactor.Parser, as: P
alias ExFactor.Callers

def callers(mod), do: ExFactor.callers(mod)
def callers(mod), do: Callers.callers(mod)
def all_functions(input), do: P.all_functions(input)
end

0 comments on commit 8bc9c52

Please sign in to comment.