Skip to content

Commit

Permalink
add default values
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 8, 2021
1 parent 27c13dc commit a9ca141
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
18 changes: 13 additions & 5 deletions lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ defmodule ExFactor.Extractor do
alias ExFactor.Parser

def emplace(files, opts) do
source_path = Keyword.get(opts, :source_path)
source_module = Keyword.get(opts, :source_module)
target_module = Keyword.get(opts, :target_module)
target_path = Keyword.get(opts, :target_path)
source_function = Keyword.get(opts, :source_function)
arity = Keyword.get(opts, :arity)
target_function = Keyword.get(opts, :target_function, source_function)

Macro.underscore(source_module)
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])

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

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

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

Expand Down Expand Up @@ -48,4 +52,8 @@ defmodule ExFactor.Extractor do
File.write(target_path, content)
end
end

defp path(module) do
Path.join(["lib", Macro.underscore(module) <> ".ex"])
end
end
22 changes: 11 additions & 11 deletions test/ex_factor/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule ExFactor.ExtractorTest do

test "write a new file with the function, infer some defaults" do
content = """
defmodule ExFactorSampleModule do
defmodule ExFactor.SourceModule do
@somedoc "This is somedoc"
# no aliases
def pub1(arg1) do
Expand All @@ -53,23 +53,23 @@ defmodule ExFactor.ExtractorTest do
end
"""

File.write("test/support/source_module.ex", content)
target_path = "test/support/target_module.ex"
File.write("lib/ex_factor/source_module.ex", content)

opts = [
target_path: target_path,
target_module: ExFactor.NewMod,
source_module: ExFactorSampleModule,
source_path: "test/support/source_module.ex",
target_module: ExFactor.TargetModule,
source_module: ExFactor.SourceModule,
source_function: :pub1,
arity: 1
]

path = Path.join([Mix.Project.app_path(), "lib", target_path <> ".ex"])
Extractor.emplace(["test/support/source_module.ex"], opts)
# path = Path.join([Mix.Project.app_path(), "lib", target_path <> ".ex"])
Extractor.emplace(["lib/ex_factor/source_module.ex"], opts)

file = File.read!(target_path) |> IO.inspect(label: "target_path")
file = File.read!("lib/ex_factor/target_module.ex")
assert file =~ "def(pub1(arg1))"
assert file =~ "defmodule(ExFactor.NewMod) do"
assert file =~ "defmodule(ExFactor.TargetModule) do"

File.rm("lib/ex_factor/source_module.ex")
File.rm("lib/ex_factor/target_module.ex")
end
end

0 comments on commit a9ca141

Please sign in to comment.