zoukankan      html  css  js  c++  java
  • elixir mix 简介

    概述

    mix 是 elixir 工程的构建工具,利用 mix,可以快速方便的创建 elixir 工程,写单元测试,管理 elixir 包的依赖管理等等。
    我觉得刚开始学习 elixir 的时候,先简单看看 mix 的用法,也方便学习中写些 elixir 的小例子。

    本文环境基于:

    $ elixir -v
    Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
    
    Elixir 1.2.1
    
    $ mix -v
    Mix 1.2.1
    

    使用 mix 管理 elixir 工程

    mix 支持很多子命令,可以通过 mix help 来查看。
    下面只介绍一些常用的子命令,使得可以在学习 elixir 的过程中,做些简单的代码实验即可。

    创建工程

    学习 elixir 的过程中,难免会写些代码来试验语言的特性。
    虽然写个 exs 的脚本也行,但是将所有的试验代码通过一个工程来管理,不仅方便以后查看,也方便试验的时候复用代码。

    所以,首先要看看如何使用 mix 来创建一个 elixir 工程。

    $ mix new sample
    * creating README.md
    * creating .gitignore
    * creating mix.exs
    * creating config
    * creating config/config.exs
    * creating lib
    * creating lib/sample.ex
    * creating test
    * creating test/test_helper.exs
    * creating test/sample_test.exs
    
    Your Mix project was created successfully.
    You can use "mix" to compile it, test it, and more:
    
        cd sample
        mix test
    
    Run "mix help" for more commands.
    

    通过 new 子命令,即可以创建工程,工程的目录结构如下:

     .
    ├── README.md               # 默认的工程说明文件,可以根据工程添加自己的内容
    ├── config                  # 工程中使用的各个package的配置,具体说明可以看 config.exs 中的注释说明
    │   └── config.exs
    ├── lib                     # 真正实现功能或者试验的代码放在这个文件夹中,默认生成一个空的示例
    │   └── sample.ex
    ├── mix.exs                 # 整个工程的配置文件,里面包含工程名称,版本,依赖关系等等。mix 工具就是依靠此文件来编译工程的
    └── test                    # 单元测试代码放在此文件夹中,默认生成一个空的测试文件
        ├── sample_test.exs     # 文件名称对应 lib 中的代码文件
        └── test_helper.exs     # 默认只有一行启动测试的代码,可以根据需要扩充(比如加一些共通的测试前处理,后处理等)
    

    编译,运行工程

    工程创建了之后,就可以开始写 elixir 的代码,体验 elixir 的特别之处了。
    为了试验 mix 的编译,首先在 lib/sample.ex 中加入一段简单的代码:

    defmodule Sample do
      def hello do
        IO.puts "hello Elixir"
      end
    end
    

    然后编译工程:

    $ mix compile
    Compiled lib/sample.ex
    Generated sample app
    Consolidated List.Chars
    Consolidated String.Chars
    Consolidated Collectable
    Consolidated Enumerable
    Consolidated IEx.Info
    Consolidated Inspect
    
    $ ls -l
    total 16
    -rw-r--r--  1 xxxxxx  staff  399  2 17 22:08 README.md
    drwxr-xr-x  3 xxxxxx  staff  102  2 17 22:30 _build
    drwxr-xr-x  3 xxxxxx  staff  102  2 17 22:08 config
    drwxr-xr-x  3 xxxxxx  staff  102  2 17 22:29 lib
    -rw-r--r--  1 xxxxxx  staff  665  2 17 22:08 mix.exs
    drwxr-xr-x  4 xxxxxx  staff  136  2 17 22:08 test
    

    编译的结果就在 _build 文件夹中。

    运行工程也很简单:

    $ iex -S mix
    Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
    
    Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> Sample.hello
    hello Elixir
    :ok
    

    单元测试

    单元测试是确保代码质量的重要手段,mix 创建的工程也规定了单元测试的规则。
    针对上面的简单代码,我们可以在 test 目录中对应的测试文件 samle_test.exs 中添加测试代码:

    defmodule SampleTest do
      use ExUnit.Case
      doctest Sample
    
      test "case01" do
        assert :ok == Sample.hello
      end
    
      test "case02" do
        assert :ng == Sample.hello
      end
    end
    

    运行单元测试非常简单:

    $ mix test
    hello Elixir
    .hello Elixir
    
    
      1) test case02 (SampleTest)
         test/sample_test.exs:9
         Assertion with == failed
         code: :ng == Sample.hello()
         lhs:  :ng
         rhs:  :ok
         stacktrace:
           test/sample_test.exs:10
    
    
    
    Finished in 0.07 seconds (0.07s on load, 0.00s on tests)
    2 tests, 1 failure
    
    Randomized with seed 537919
    

    从输出的内容中可以看出,case01 成功,case02 失败,与预期的是一致的。

    编译二进制文件

    上面的示例中,是通过 iex -S mix 来运行工程的,这种方式一般用于运行一个库。
    我们也可以将工程编译成一个二进制文件,可以分发到所有安装 OTP 环境的系统中运行。
    这时,我们需要一个 main 函数。
    修改 lib/sample.ex 中的代码如下:

    defmodule Sample do
      def main(args \ []) do
        IO.puts "hello "
        IO.puts args
      end
    end
    

    为了编译二进制,设置 mix.exs 如下:

    def project do
      [app: :sample,
       version: "0.0.1",
       elixir: "~> 1.2",
       escript: escript,                            # 追加 escript 配置
       build_embedded: Mix.env == :prod,
       start_permanent: Mix.env == :prod,
       deps: deps]
    end
    
    def escript do                                  # 追加 escript 函数,其中设置 main_module
      [main_module: Sample]
    end
    

    编译二进制:

    $ mix escript.build
    Compiled lib/sample.ex
    Generated sample app
    Consolidated List.Chars
    Consolidated Collectable
    Consolidated String.Chars
    Consolidated Enumerable
    Consolidated IEx.Info
    Consolidated Inspect
    Generated escript sample with MIX_ENV=dev
    
    $ ls -l
    total 4824
    -rw-r--r--  1 xxxxxx  staff      399  2 17 22:08 README.md
    drwxr-xr-x  4 xxxxxx  staff      136  2 17 23:08 _build
    drwxr-xr-x  3 xxxxxx  staff      102  2 17 22:08 config
    drwxr-xr-x  3 xxxxxx  staff      102  2 17 23:31 lib
    -rw-r--r--  1 xxxxxx  staff      738  2 17 23:28 mix.exs
    -rwxr-xr-x  1 xxxxxx  staff  2460249  2 17 23:28 sample          # 此为二进制文件
    drwxr-xr-x  4 xxxxxx  staff      136  2 17 23:09 test
    
    $ ./sample
    hello
    
    $ ./sample "Elixir"
    hello
    Elixir
    
  • 相关阅读:
    局部 与 整体 修正 逐渐逼近
    en-zh(社会问题)social problems
    单调性 [1 + 1 / (n)]^n
    en-zh(科学技术)science and technology
    mysql函数之截取字符串
    看数据库的文件大小 MySQL Binlog日志的生成和清理规则
    Brouwer不动点
    布尔巴基学派
    量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html
    COMSOL
  • 原文地址:https://www.cnblogs.com/wang_yb/p/5196884.html
Copyright © 2011-2022 走看看