From 989182a306caa3b8adeb1d9605246df403d8cc04 Mon Sep 17 00:00:00 2001 From: Thomas Timmer Date: Wed, 15 May 2024 21:03:12 +0200 Subject: [PATCH] fix: handle moduleattributes --- lib/mix_edit.ex | 41 ++++++++++++++++++++++++++++++++++++++++- test/mix_add_test.exs | 6 ++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/mix_edit.ex b/lib/mix_edit.ex index cb20596..054d66c 100644 --- a/lib/mix_edit.ex +++ b/lib/mix_edit.ex @@ -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( @@ -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() diff --git a/test/mix_add_test.exs b/test/mix_add_test.exs index 581bc53..eaca1c1 100644 --- a/test/mix_add_test.exs +++ b/test/mix_add_test.exs @@ -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", @@ -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 @@ -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