|
1 | 1 | defmodule Mix do
|
2 |
| - @moduledoc """ |
3 |
| - Mix is a build tool that provides tasks for creating, compiling and |
4 |
| - testing Elixir projects. Mix is inspired by the Leiningen |
5 |
| - build tool for Clojure and was written by one of its contributors. |
| 2 | + @moduledoc ~S""" |
| 3 | + Mix is a build tool that provides tasks for creating, compiling, |
| 4 | + testing Elixir projects, as well as handle dependencies, and more. |
6 | 5 |
|
7 |
| - This module works as a facade for accessing the most common functionality |
8 |
| - in Elixir, such as the shell and the current project configuration. |
| 6 | + ## Mix.Project |
9 | 7 |
|
10 |
| - For getting started with Elixir, checkout out the guide available on |
11 |
| - [Elixir's website](http://elixir-lang.org). |
| 8 | + The foundation of Mix is a project. A project can be defined by |
| 9 | + by using the `Mix.Project` in a module, usually place in a file |
| 10 | + named `mix.exs`: |
| 11 | +
|
| 12 | + defmodule MyApp.Mixfile do |
| 13 | + def project do |
| 14 | + [app: :my_app, |
| 15 | + version: "1.0.0"] |
| 16 | + end |
| 17 | + end |
| 18 | +
|
| 19 | + The `project/0` function is where the project information is defined |
| 20 | + and it allows developers to configure many tasks. |
| 21 | +
|
| 22 | + After the project above is defined, there are many tasks one can |
| 23 | + run directly from the command line: |
| 24 | +
|
| 25 | + * `mix compile` - compiles the current project |
| 26 | + * `mix test` - runs tests for the given project |
| 27 | + * `mix run` - runs a particular command inside the project |
| 28 | +
|
| 29 | + Each task has its own options and sometimes specific configuration |
| 30 | + to be defined in the `project/0` function. You can use `mix help` |
| 31 | + to list all available tasks and `mix help NAME` to show help for |
| 32 | + a particular task. |
| 33 | +
|
| 34 | + The best way to get started with your first project is by calling |
| 35 | + `mix new my_project` from the command line. |
| 36 | +
|
| 37 | + ## Mix.Task |
| 38 | +
|
| 39 | + Tasks are what make Mix extensible. |
| 40 | +
|
| 41 | + Any project can extend Mix behaviour by adding their own tasks. For |
| 42 | + example, you can add the task below inside your project and it will |
| 43 | + be available to everyone that uses your project: |
| 44 | +
|
| 45 | + defmodule Mix.Tasks.Hello do |
| 46 | + use Mix.Task |
| 47 | +
|
| 48 | + def run(_) do |
| 49 | + Mix.shell.info "hello" |
| 50 | + end |
| 51 | + end |
| 52 | +
|
| 53 | + Now they can invoke it with `mix run hello`. |
| 54 | +
|
| 55 | + ## Dependencies |
| 56 | +
|
| 57 | + Another important feature in Mix is that it is able to manage your |
| 58 | + dependencies and integrates nicely with [the Hex package manager](http://hex.pm). |
| 59 | +
|
| 60 | + In order to use dependencies, you just need to add a `:deps` key |
| 61 | + to your project configuration. We often extract the dependencies |
| 62 | + listing to its own functions: |
| 63 | +
|
| 64 | + defmodule MyApp.Mixfile do |
| 65 | + def project do |
| 66 | + [app: :my_app, |
| 67 | + version: "1.0.0", |
| 68 | + deps: deps] |
| 69 | + end |
| 70 | +
|
| 71 | + defp deps do |
| 72 | + [{:ecto, "~> 0.3.0"}, |
| 73 | + {:plug, github: "elixir-lang/plug"}] |
| 74 | + end |
| 75 | + end |
| 76 | +
|
| 77 | + You can run `mix help deps` to learn more about dependencies in Mix. |
| 78 | +
|
| 79 | + ## Environments |
| 80 | +
|
| 81 | + Mix provides environments. |
| 82 | +
|
| 83 | + Environments allow developers to prepare and organize their project |
| 84 | + specific to different scenarios. By default, Mix provides three |
| 85 | + environments: |
| 86 | +
|
| 87 | + * `:dev` - the default environment |
| 88 | + * `:test` - the environment `mix test` runs on |
| 89 | + * `:prod` - the environment your dependencies runs on |
| 90 | +
|
| 91 | + The environment can be changed via the command line by setting |
| 92 | + the `MIX_ENV` environment variable, for example: |
| 93 | +
|
| 94 | + $ MIX_ENV=prod mix run server.exs |
| 95 | +
|
| 96 | + ## Aliases |
| 97 | +
|
| 98 | + Aliases are shortcut or tasks specific to the current project. |
| 99 | +
|
| 100 | + In the `Mix.Task` section, we have defined a task that would be |
| 101 | + available to everyone using our project as a dependency. What if |
| 102 | + we wanted the task to only be available for our project? Just |
| 103 | + define an alias: |
| 104 | +
|
| 105 | + defmodule MyApp.Mixfile do |
| 106 | + def project do |
| 107 | + [app: :my_app, |
| 108 | + version: "1.0.0", |
| 109 | + aliases: aliases] |
| 110 | + end |
| 111 | +
|
| 112 | + defp aliases do |
| 113 | + [c: "compile", |
| 114 | + hello: &hello/1] |
| 115 | + end |
| 116 | +
|
| 117 | + defp hello(_) do |
| 118 | + Mix.shell.info "Hello world" |
| 119 | + end |
| 120 | + end |
| 121 | +
|
| 122 | + In the example above, we have defined two aliases. One is `mix c` |
| 123 | + which is a shortcut for `mix compile`. The other is named |
| 124 | + `mix hello`, which is the equivalent to the `Mix.Tasks.Hello` |
| 125 | + we have defined in the `Mix.Task` section. |
| 126 | +
|
| 127 | + Aliases may also be a list, specifying multiple tasks to run |
| 128 | + at once: |
| 129 | +
|
| 130 | + [all: [&hello/1, "deps.get --only #{Mix.env}", "compile"]] |
| 131 | +
|
| 132 | + In the example above, we have defined an alias named `mix all`, |
| 133 | + that prints hello, then fetches dependencies specific to the |
| 134 | + current environment and compiles it. |
| 135 | +
|
| 136 | + Finally, aliases can also be use to augment existing tasks. |
| 137 | + Let's suppose you want to augment `mix clean` to clean another |
| 138 | + directory Mix does not know about: |
| 139 | +
|
| 140 | + [clean: ["clean", &clean_extra/1]] |
| 141 | +
|
| 142 | + Where `&clean_extra/1` would be a function in your `mix.exs` |
| 143 | + with extra clean up logic. |
| 144 | +
|
| 145 | + Note aliases do not show up on `mix help`. |
12 | 146 | """
|
13 | 147 |
|
14 | 148 | use Application
|
|
0 commit comments