elixir - 20 使用mix
访问量: 1470
参考https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html
mix 就是个自动化的工具,包含了创建项目,运行等。 跟rails, rake, bundle差不多。
$ mix new kv --module KV
$ mix new kv --module KV * creating README.md * creating .formatter.exs * creating .gitignore * creating mix.exs * creating config * creating config/config.exs * creating lib * creating lib/kv.ex * creating test * creating test/test_helper.exs * creating test/kv_test.exs Your Mix project was created successfully. You can use "mix" to compile it, test it, and more: cd kv mix test Run "mix help" for more commands.
直接运行测试:
$ cd kv $ mix test $ mix test Compiling 1 file (.ex) Generated kv app .. Finished in 0.05 seconds 1 doctest, 1 test, 0 failures Randomized with seed 685811同时,可以看到很多文件被生成了。
$ find . . ./test ./test/test_helper.exs ./test/kv_test.exs ./_build ./_build/test ./_build/test/lib ./_build/test/lib/kv ./_build/test/lib/kv/ebin ./_build/test/lib/kv/ebin/kv.app ./_build/test/lib/kv/ebin/Elixir.KV.beam ./_build/test/lib/kv/consolidated ./_build/test/lib/kv/consolidated/Elixir.IEx.Info.beam ./_build/test/lib/kv/consolidated/Elixir.String.Chars.beam ./_build/test/lib/kv/consolidated/Elixir.Inspect.beam ./_build/test/lib/kv/consolidated/Elixir.List.Chars.beam ./_build/test/lib/kv/consolidated/Elixir.Enumerable.beam ./_build/test/lib/kv/consolidated/Elixir.Collectable.beam ./_build/test/lib/kv/.mix ./_build/test/lib/kv/.mix/compile.elixir ./_build/test/lib/kv/.mix/compile.elixir_scm ./_build/test/lib/kv/.mix/compile.xref ./_build/test/lib/kv/.mix/.mix_test_failures ./_build/test/lib/kv/.mix/compile.protocols ./.formatter.exs ./.gitignore ./mix.exs ./README.md ./config ./config/config.exs ./lib ./lib/kv.ex可以看到,根目录下,生成了一个文件:mix.exs
defmodule KV.MixProject do use Mix.Project def project do [ app: :kv, version: "0.1.0", elixir: "~> 1.8", start_permanent: Mix.env() == :prod, deps: deps() ] end # Run "mix help compile.app" to learn about applications. def application do [ extra_applications: [:logger] ] end # Run "mix help deps" to learn about dependencies. defp deps do [ # {:dep_from_hexpm, "~> 0.3.0"}, # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} ] end endlib 目录下,生成了一个 kv.ex 文件
defmodule KV do def hello do :world end end
test 目录下,是相关的文件。
test/test_helper.exs 中,只有一行内容:ExUnit.start() 这个是废代码,先留着。
test/kv_test.exs 该文件是单元测试文件。 内容如下:
defmodule KVTest do use ExUnit.Case doctest KV test "greets the world" do assert KV.hello() == :world end end
编译
$ mix compile Compiling 1 file (.ex) Generated kv app $ MIX_ENV=prod mix compile Compiling 1 file (.ex) Generated kv app