fceruti

fceruti

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.

Showing Posts 1 to 7

sodapopcan

sodapopcan

I don’t have an answer, but I’d like to double-down on your question about looking at files during compilation as I am currently tempted to do the same. Is this a truly terrible idea? And if so, it is wrong because it’s too hacky/surprising/confusing, or is it wrong because there are gotchas?

cmo

cmo

You could put it in a file called APPLICATION and read that into the mix file as the application name and wherever else. This is sometimes done for defining the version. Could be considered hacky tho :melting_face:

fceruti

fceruti OP

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


josevalim

josevalim

Creator of Elixir

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).

jarlah

jarlah

I just have to ask, but wouldnt this enable most macros to avoid requiring the app option? Is it «safe» to fallback to the app property defined in mix config inside a library macro? Which obviously messes with our heads because its hard to understand without knowing how it works, why it works.. its a «compile runtime» call actually. In the application using the macro.?

LostKobrakai

LostKobrakai

No. Because those macros are not necessarily used just by the top level mix application (if there even is one).

fceruti

fceruti OP

So there was a waaaay less hacky way of doing it :smile:

Thanks!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews