LoginSignup
7

More than 5 years have passed since last update.

`mix new`で作られるファイル探索

Last updated at Posted at 2013-12-03

Elixirのビルドツールmixにより作成できる,Elixirプロジェクトの雛形構成を見ていきます.

mixとは

Elixirプロジェクトの作成や依存解決,コンパイル,テストを行うビルドツール.
Clojureのビルドツールleingenを参考に作られている.(そのコントリビュータの1人はelixirの開発にも関わっている)

leingenさん
leingen

ファイル構成

プロジェクト作成コマンドのmix newでは以下のようなファイルが作成される.

$ mix new nyaa
* creating README.md
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/nyaa.ex
* creating lib/nyaa
* creating lib/nyaa/supervisor.ex
* creating test
* creating test/test_helper.exs
* creating test/nyaa_test.exs

Your mix project was created successfully.
You can use mix to compile it, test it, and more:

    cd nyaa
    mix compile
    mix test

Run `mix help` for more information.

$ cd ./nyaa
$ tree
.
├── README.md
├── lib
│   ├── nyaa
│   │   └── supervisor.ex
│   └── nyaa.ex
├── mix.exs
└── test
    ├── nyaa_test.exs
    └── test_helper.exs

3 directories, 6 files

lib/nyaa.ex

アプリケーションの起動/停止の振舞いを記述するファイル.停止(stop/1)はuse Application.Behaviourすればデフォルトのものを利用できるので,start/2のみ実装すればよい

lib/nyaa.ex
defmodule Nyaa do
  use Application.Behaviour

  # See http://elixir-lang.org/docs/stable/Application.Behaviour.html
  # for more information on OTP Applications
  def start(_type, _args) do
    Nyaa.Supervisor.start_link
  end
end

lib/nyaa/supervisor.ex

上で呼んでいるNyaa.Supervisor.start_linkを定義している.
:supervisor.start_link(__MODULE__, [])は,カレントモジュールをsupervisorとして定義するということ([]はinitの引数として渡される)

supervise(children, strategy: :one_for_one):one_for_oneは監視しているプロセス(child)が落ちたらそのプロセスだけ再起動するという意味.他にも監視プロセス全てを再起動する:one_for_allなどがある.オプション一覧は http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html を参照.

lib/nyaa/supervisor.ex
defmodule Nyaa.Supervisor do
  use Supervisor.Behaviour

  def start_link do
    :supervisor.start_link(__MODULE__, [])
  end

  def init([]) do
    children = [
      # Define workers and child supervisors to be supervised
      # worker(Nyaa.Worker, [])
    ]

    # See http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html
    # for other strategies and supported options
    supervise(children, strategy: :one_for_one)
  end
end

mix.exs

アプリケーションの設定ファイル.依存ライブラリやなどを記述する

test/nyaa_test.exs, test/test_helper.exs

名前の通りテストの雛形.rspecのような書き方

test/nyaa_test.exs
defmodule NyaaTest do
  use ExUnit.Case

  test "the truth" do
    assert(true)
  end
end

次の一歩

この時点ではsupervisorしかいないので,workerをNyaa.Workerとして作成していく.

参考リンク

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
7