Getting a list of "tagged" modules

I think the best option is to use @before_compile hook for tagging. Check the example below:

defmodule Tagger do
  defmacro __before_compile__(_env) do
    quote do
      def __tagged, do: :ok
    end
  end

  def list_tagged_modules do
    {:ok, modules} = :application.get_key(Application.get_application(__MODULE__), :modules)
    modules |> Enum.filter(fn m -> m.__info__(:functions) |> Enum.any?(&match?({:__tagged, 0}, &1)) end)
  end
end

defmodule Mod1 do
  @before_compile Tagger
end

defmodule Mod2 do
end

And this is how you use it:

iex(1)> Tagger.list_tagged_modules
[Mod1]

So effectively “@before_compile Tagger” does the tagging.

1 Like