From a9ca1414c04b813b14d0d4298e837575c8ed0adb Mon Sep 17 00:00:00 2001 From: Christian Koch Date: Tue, 7 Sep 2021 19:59:22 -0500 Subject: [PATCH] add default values --- lib/ex_factor/extractor.ex | 18 +++++++++++++----- test/ex_factor/extractor_test.exs | 22 +++++++++++----------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/ex_factor/extractor.ex b/lib/ex_factor/extractor.ex index c849775..98e4ef8 100644 --- a/lib/ex_factor/extractor.ex +++ b/lib/ex_factor/extractor.ex @@ -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) @@ -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 diff --git a/test/ex_factor/extractor_test.exs b/test/ex_factor/extractor_test.exs index 2d5ae3a..933a3c3 100644 --- a/test/ex_factor/extractor_test.exs +++ b/test/ex_factor/extractor_test.exs @@ -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 @@ -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