Run your Elixir scripts with dependencies of your favorite Mix projects!
In Elixir scripts, you cannot use external libraries, such as Jason, by using elixir
command.
If you want to use functionalities of external Mix projects, you are forced to create new Mix projects to download dependencies.
You cannot even install Mix projects globally in your system.
By using erun
, new Mix projects become unnecessary for disposable scripts.
erun
resolves the problem simply by wrapping Mix projects in escript.
If erun
is installed with Mix dependencies you specified, you can run Elixir scripts anywhere you want.
For example, if you install erun
with Jason,
%{foo: "bar"}
|> Jason.encode!
|> IO.puts
the code above (foo.exs
) can be executed as
$ erun foo.exs
{"foo":"bar"}
git clone git@github.com:s417-lama/erun.git
defp deps do
[
{:super_package, "~> 1.0"},
...
]
end
mix deps.get
mix escript.install
(If needed) add escript path in .bashrc
.
echo 'export PATH=${HOME}/.mix/escripts:$PATH' >> ~/.bashrc
or if you are using asdf,
echo 'export PATH=$(asdf where elixir)/.mix/escripts:$PATH' >> ~/.bashrc
After that, restart a shell or run . ~/.bashrc
.
$ erun -e "IO.puts :hello"
hello
By calling Erun.args/0
, you can get commandline arguments.
If you save the code below
IO.inspect Erun.args
as get_args.exs
, the result would be:
$ erun get_args.exs foo bar 1
["foo", "bar", "1"]
Args are given as a list of string.
The implementation is so simple.
erun
simply calls Code.eval_string/1
or Code.eval_file/1
.