fireproofsocks

fireproofsocks

Running one .exs file from another

How can you run a .exs file from another .exs file? My inspiration comes from the basic config.exs file which runs another .exs via import_config "#{Mix.env()}.exs". I’d like to do the same type of thing, but not with just config files.

Would something like Mix.Tasks.Run.run(["path/to/my.exs"]) work in an umbrella? How does Mix know which directory is the base directory?

Thanks for any pointers!

Marked As Solved

ericmj

ericmj

Elixir Core Team

You can’t call mix functions at runtime because they are not available in your release. You need to evaluate Mix.env() at compile time, module attributes can help you do that:

@env Mix.env()

def foo do
  Application.app_dir(:myapp, "priv/repo/#{@env}.exs")
  |> Code.require_file() 
end

Also Liked

josevalim

josevalim

Creator of Elixir
josevalim

josevalim

Creator of Elixir

Use Application.app_dir to build the path within a certain app.

joaoevangelista

joaoevangelista

You can use Application.app_dir/2 to fetch the file that lies inside the application folder like that:

 Application.app_dir(:myapp, ["priv/repo", "#{Mix.env()}.exs"])
 |> Code.require_file() 

The second argument will join the parts of the path and return a String

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

We're in Beta

About us Mission Statement