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
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
Use Application.app_dir to build the path within a certain app.
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








