/erun

Run your Elixir scripts with dependencies of your favorite Mix projects

Primary LanguageElixir

Erun

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"}

Usage

1. clone

git clone git@github.com:s417-lama/erun.git

2. modify mix.exs to include dependencies you want

  defp deps do
    [
      {:super_package, "~> 1.0"},
      ...
    ]
  end

3. get deps

mix deps.get

4. compile and install

mix escript.install

5. add escript dir to PATH

(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.

6. try to run

$ erun -e "IO.puts :hello"
hello

Args

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.

Implementation

The implementation is so simple. erun simply calls Code.eval_string/1 or Code.eval_file/1.