fceruti
How to get application name at compile time?
I need to access the application name under which a module was defined, in other words, I need the name defined in the module’s parent mix.exs.
def __on_definition__(env, _kind, func_name, args, _guards, _body) do
app_name = Application.get_application(env.module)
Application.get_application/1 doesn’t always work, as the application may or may not be available when recompiling.
Is there a non-hacky way of getting this info? I’m tempted to look at the file directory looking for a mix.exs and parsing it.
Marked As Solved
josevalim
Mix.Project.config(), but only invoke it at compile time, as Mix isn’t available inside releases. Or ask the user to pass it as parameter (as Phoenix/Ecto do).
Also Liked
LostKobrakai
No. Because those macros are not necessarily used just by the top level mix application (if there even is one).
fceruti
Yup, I’m gonna just do this, and change it later if a better solution arises.
The problem is that this is library code, that is supposed to look into your project, but also, other libraries, so it’s a no-no, to need to adhere to that standard.
For now goes like this:
app_name = find_app_name(env.file)
...
def find_app_name(module_file) when is_binary(module_file) do
module_file |> Path.dirname() |> find_app_name_in_dir()
end
defp find_app_name_in_dir(dir) do
{:ok, files} = File.ls(dir)
if "mix.exs" in files do
mix_file = Path.join(dir, "mix.exs")
{:ok, content} = File.read(mix_file)
case Regex.named_captures(~r/app: :(?<app_name>[a-z_]+),*\n/, content) do
%{"app_name" => app_name} -> String.to_existing_atom(app_name)
_ -> raise("Failed to read an app name from #{mix_file}")
end
else
dir |> Path.dirname() |> find_app_name_in_dir()
end
end







