你的程式碼聞起來很臭嗎?不用擔心!大夫幫你...越治越臭

Mix(Elixir從零開始系列 07)(鼠年全馬鐵人挑戰 W08)

這是 w3HexSchool 鼠年全馬鐵人挑戰 Week 8 唷

這是 Elixir 從零開始 系列 07 唷

前言

在這篇文章之前,我們大部分都用 IEx 來操作,但是如果我們之後要進行大型的專案,更多的程式碼,更複雜的架構,單單只靠 IEx 是不夠的喔,因此我們需要專案管理工具!

Mix

Mix tool 的定義如下

Mix 是一個編譯工具,它提供建立、編譯與測試 Elixir 專案,它也可進行依賴套件的管理等

在這裡,我們將學會怎麼利用 Mix tool 建立新專案

Build a new project

我們到終端機底下測試一下 Elixir 環境是否正常

1
2
3
4
$ elixir -v
Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [hipe]

Elixir 1.10.1 (compiled with Erlang/OTP 22)

再來我們利用 mix new 新增一個專案,mix new 的用法如下

1
mix new PATH [--app APP] [--module MODULE] [--sup] [--umbrella]

我們可以用 --app 來幫 application 取名稱,更多詳細可以參考 這裡 唷,這裡我們新建立一個 my_app

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$mix new my_app

* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/my_app.ex
* creating test
* creating test/test_helper.exs
* creating test/my_app_test.exs

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

cd my_app
mix test

Run "mix help" for more commands.

建好之後呢,我們找到 lib/my_app.ex,在這裡我們可以新增我們的自己想要的函式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
defmodule MyApp do
@moduledoc """
Documentation for `MyApp`.
"""

@doc """
Hello world.

\## Examples

iex> MyApp.hello()
:world

"""
def hello do
:world
end
end

這邊預設了一個函式 hello,讓我們再多新增一些函式到 my_app.ex 裡面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def hello(:one) do
"hello world"
end

def hello(:two) do
"hello world hello world"
end

def hello(:three) do
"hello world hello world hello world"
end

def hello(_) do
"null"
end

好了之後呢,我們進到剛剛建好的專案資料夾內,我們可以使用兩個方法

  1. mix compile 單純做編譯
  2. 或直接 iex -S mix 編譯後進入 iex,這會載入剛建立好的應用程式
1
2
3
4
5
6
7
$ cd my_app
$ iex -S mix
Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [hipe]

Compiling 1 file (.ex)
Interactive Elixir (1.10.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

iex 內我們輸入我們剛剛建好的Module MyApp 後按下 tab

1
iex(1)> MyApp.

我們可以看到出現了 hello/0hello/1,這兩個都是我們剛剛建好的函式有沒有

1
MixProject    hello/0       hello/1

試著執行看看囉

1
2
3
4
iex(1)> MyApp.hello(:two)
"hello world hello world"
iex(2)> MyApp.hello(:three)
"hello world hello world hello world"

如果我們亂打一些參數進去 hello 裡面呢?可以看到我們用了之前學到的技巧 hello(_)_ 代表不在乎,也就是輸入什麼都可接受,然後被 pattern matching 到此函式內囉

1
2
3
4
5
6
iex(3)> MyApp.hello(:yoooo)
"null"
iex(4)> MyApp.hello(400)
"null"
iex(5)> MyApp.hello(3.14)
"null"

那可能會有人會問,那我修改函式之後怎麼重新編譯呢?有沒有什麼簡單的方法咧?

答案是有的,而且滿簡單的,使用 recompile 即可

我們更改了 my_app.ex 的內容,重新編譯後會回傳 :ok

1
2
3
iex(10)> recompile
Compiling 1 file (.ex)
:ok

我們沒有更改 my_app.ex 的內容,重新編譯後會回傳 :noop

1
2
iex(9)> recompile
:noop

Mix format

在我們利用 mix new 新增專案後,會一併產生檔案 .formatter.exs,這個檔案其實是給 mix format 使用的,mix format 是一個 task,它可以自動格式化你的程式碼,讓你的程式碼符合規則更漂亮一點,我們可以測試一下,修改原本的檔案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
defmodule MyApp do
def hello do
:world
end
def hello(:one) do
"hello world"
end
def hello(:two) do
"hello world hello world"
end
def hello(:three) do
"hello world hello world hello world"
end
def hello(_) do
"null"
end
end

再執行 mix format 看看,可以發現,程式碼又變整齊了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
defmodule MyApp do
def hello do
:world
end

def hello(:one) do
"hello world"
end

def hello(:two) do
"hello world hello world"
end

def hello(:three) do
"hello world hello world hello world"
end

def hello(_) do
"null"
end
end

這邊建議每次寫完程式都執行一下讓程式碼變得美美香香的喔

參考資料

  1. https://hexdocs.pm/mix/Mix.html#content
  2. https://hexdocs.pm/mix/Mix.Tasks.New.html
  3. https://hexdocs.pm/mix/Mix.Tasks.Format.html