LoginSignup
13
7

More than 5 years have passed since last update.

Elixirでコマンド実行可能なスクリプトを書きGistで配布する

Last updated at Posted at 2017-01-20

ErlangVM には,Erlang をコマンド実行可能にする escript という機能がある.

ErlangVM ファミリーの Elixir でも利用可能だ.
さらに Elixir1.4 から escript を Git やパッケージマネージャーの Hex.pm からインストールできるようになった..

GistへElixirのプロジェクトを置き,最小限の自己完結した確認可能なコードとして利用すると組み合せると

mix escript.install git https://gist.github.com/niku/9be74fbcdb5d2b9e36b4259fe85e08a8

という 1 行で実行可能なコマンドを Gist からインストールできるようになる.
今回は my_app というコマンドを実行すると "Hello from MyApp!" と表示する escript を Gist に準備した

/Users/niku% mix escript
No escripts currently installed.

/Users/niku% mix escript.install git https://gist.github.com/niku/9be74fbcdb5d2b9e36b4259fe85e08a8
* Getting new package (https://gist.github.com/niku/9be74fbcdb5d2b9e36b4259fe85e08a8)
remote: Counting objects: 5, done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 5
Compiling 1 file (.ex)
Generated my_app app
Generated escript my_app with MIX_ENV=prod
Are you sure you want to install escript "my_app"? [Yn]
* creating /Users/niku/.mix/escripts/my_app

/Users/niku% .mix/escripts/my_app
Hello from MyApp!

PATH を通す (optional)

~/.mix/escripts を PATH に含めておくと,単に my_app と打つだけで実行できる.

/Users/niku% my_app
Hello from MyApp!
13
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
7