Where does the `priv` directory belong in mix umbrella projects
Asked Answered
L

2

7

As I understand it any artifact that is needed in a release should be put in a priv directory. In the case of an umbrella project should there be a single priv directory at the umbrella level or one for each app?

Also how do you look up the pathname for the priv directory?

Lotty answered 20/11, 2016 at 12:28 Comment(0)
G
10

priv directory is part of an OTP application layout. Since an umbrella in itself is not an OTP application it doesn't have the priv directory - the actual applications do, so each one gets it's own priv.

You can access files in the priv directory using:

Application.app_dir(:app_name, "priv/path/to/file")
Goblet answered 20/11, 2016 at 13:8 Comment(2)
Is there a way to look up the app dir without knowing the name of the app you are in. for example a macro might want to look up the app dir of the module it is being included intoLotty
@otp_app Mix.Project.config[:app]Corporal
V
1

If you haven't defined an :app_name, as it was in my case, I've managed to make this work:

defmodule MyModule do

   @otp_app Mix.Project.config[:app]

   def my_func do
       Application.app_dir(@otp_app, "priv/path/to/file")
   end
end

Then I could run my tests (mix test) in both root level of my umbrella project and in my app that was using the priv directory.

Update:

I've had issue because I had an escript that I wanted to use data from priv, so I ended up reading the file at compile time and then my problem was gone:

  @otp_app Mix.Project.config()[:app]
  @file_contents File.stream!(Application.app_dir(@otp_app, "priv/path/tofile"))

and in my method:

def read_file do
    @file_contents
    |> do_something
    # ...
end
Vela answered 10/9, 2019 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.