Skip to content

Commit

Permalink
fix: handle moduleattributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Timmer committed May 15, 2024
1 parent 9afea6f commit 989182a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
41 changes: 40 additions & 1 deletion lib/mix_edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,27 @@ defmodule MixEdit do

@doc "add dependencies to the dependency list"
def add_deps(current_deps, additional_dependencies) do
{deps_keyword, _} = Code.eval_quoted(current_deps)
attributes =
current_deps
|> collect_module_attributes()
|> Enum.map(fn name ->
quote do
Module.put_attribute(__MODULE__, unquote(name), true)
end
end)

random_module_name = Base.encode32(:rand.bytes(16), padding: false)

wrapped_current_deps =
quote do
defmodule unquote(Module.concat([MixEdit, TmpModule, random_module_name])) do
unquote(attributes)
unquote(current_deps)
end
end

{{:module, _, _, deps_keyword}, _} =
Code.eval_quoted(wrapped_current_deps, [], file: "generated_module_by_mix_edit.ex")

dependencies =
Enum.filter(
Expand Down Expand Up @@ -244,6 +264,25 @@ defmodule MixEdit do
end)
end

defp collect_module_attributes(current_deps) do
Enum.reduce(current_deps, [], fn item, acc ->
case item do
{_, _context,
[
_,
_,
[
{_, {:@, _, [{module_attribute, _, _}]}}
]
]} ->
[module_attribute | acc]

_ ->
acc
end
end)
end

defp get_dep_name_from_quoted(quoted) do
quoted
|> Code.eval_quoted()
Expand Down
6 changes: 4 additions & 2 deletions test/mix_add_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ defmodule MixEditTest do
defmodule MoreTags.MixProject do
use Mix.Project
@dev_envs [:dev, :test]
def project do
[
version: "0.1.0",
Expand All @@ -250,7 +252,7 @@ defmodule MixEditTest do
{:jason, "~> 1.0"},
{:credo, "~> 1.7.1", only: [:dev]},
{:dialyxir, "~> 1.4", only: [:dev], runtime: false},
{:excoveralls, "~> 0.17", only: [:dev, :test]},
{:excoveralls, "~> 0.17", only: @dev_envs},
]
end
end
Expand All @@ -260,7 +262,7 @@ defmodule MixEditTest do
assert mix_project_sort(mix_project, testing: [version: ">= 0.0.0"]) =~
Enum.join([
"deps do [{:credo, \"~> 1.7.1\", only: [:dev]}, {:dialyxir, \"~> 1.4\", only: [:dev], runtime: false}, ",
"{:excoveralls, \"~> 0.17\", only: [:dev, :test]}, {:jason, \"~> 1.0\"}, {:testing, \">= 0.0.0\"}] end"
"{:excoveralls, \"~> 0.17\", only: @dev_envs}, {:jason, \"~> 1.0\"}, {:testing, \">= 0.0.0\"}] end"
])
end
end
Expand Down

0 comments on commit 989182a

Please sign in to comment.