faigy langsam
faigy langsam

Reputation: 2176

Get application name in umbrella project

I have an umbrella project (my_app) like this:

├── README.md ├── apps │ ├── app_one │ | └── mix.exs │ ├── app_two | | └── mix.exs │ └── ... ├── config └── mix.exs

I want to get the name of the currently running app,
for example: app_one , app_two.
when i use:
Mix.Project.get.project[:app]
i always get the main project name my_app .
How can i do that?

Upvotes: 5

Views: 3757

Answers (2)

justapilgrim
justapilgrim

Reputation: 6872

The alternative to Erlang's :application.get_application/1 is Application.get_application/1. This returns nil if the module is not listed in any application spec.

iex> Application.get_application MyApp.ExistingModule
:my_app

iex> Application.get_application MyApp.NonExistingModule
nil

Upvotes: 1

Dogbert
Dogbert

Reputation: 222238

You can get the application to which the a module belongs to using :application.get_application/1. If you pass __MODULE__ as the first argument, you'll get the application the current module belongs to.

$ cat apps/a/lib/a.ex
defmodule A do
  def hello do
    :application.get_application(__MODULE__)
  end
end
$ cat apps/b/lib/b.ex
defmodule B do
  def hello do
    :application.get_application(__MODULE__)
  end
end
$ iex -S mix
iex(1)> A.hello
{:ok, :a}
iex(2)> B.hello
{:ok, :b}

Upvotes: 10

Related Questions