Command line: elixir vs mix? How to write a script that has access to all an app's modules?

This is perhaps a simple question, but I think it gets at some of the fundamental underpinnings of mix and iex.

I’m working on an app where it makes sense to have separate .exs files where users can do some simple (but sometimes lengthy) setup before calling a function or two inside the app, e.g.

MyApp.do_something(["list", "of", "stuff", "maybe", "100s", "of", "lines", "..."])

This command works fine if running from inside iex -S mix. But if I save a command like that inside of foo.exs, it can’t magically find the MyApp module. How can I “tell” a script how to find all of its modules? I could write a Mix task, but it’s important that the users here are given a clean slate of having their own .exs file, so how does one tell a mix task to “include” and evaluate an .exs file? (This is more or less what mix test does, no?)

2 Likes

You can run scripts in the context of your mix project with mix run -r foo.exs. See mix help run for more information.

9 Likes