8.9

Elixir, Mix

make things

Installation, Version

We will use Elixir for our projects and mix for compiling source files, managing dependencies, and assembling executables. Follow the installation instructions for your platform(s) of choice, and then ensure that you have:
  • Elixir version at least 1.16.0 or upward.

  • Erlang/OTP 26 version at least 26.

You can check your version by running elixir -v from a terminal.

IDE

I recommend using the Elixir plugin for IntelliJ. To setup,

If you prefer to use VSCode, this extension looks to provide many of the same features.

In the New Project dialog, you should now be able to select Elixir as the project language, with the SDK you just configured.

Mix

Mix is a tool for managing the dependencies of Elixir projects, as well as building, testing, and so on.

To create a new Elixir project using mix, run mix <your-project-name> in a terminal.

Alternatively, the IntelliJ Elixir plugin uses Mix to create a new project when you select Elixir as the language.

This will create a directory with your provided name. The directory includes several items of note:
  • The file mix.exs. This file defines the configuration information for our project that mix will use for building, managing dependencies, etc.

  • The directory lib/, where we will put the source files of our project.

  • The directory test/, where we will put our tests.

Configuration.

For our present purpose, we need only attend to two portions of the configuration file mix.exs. First, name the :app :stbu. So, part of the file should look like this:

  def project do

     [

       app: :milestone0take0,

       version: "0.1.0",

       ...

     ]

  end

Second, you may wish to use a third-party library. To do so, you may add a line to the deps definition in the file with the name and version of the library in question. For example, to add a dependency on the Jason library for handling JSON, you would update the initial deps definition to the following:

  defp deps do

    [

      {:jason, "~> 1.4"}

    ]

  end

Once you update the dependencies, run ‘mix deps.get‘ at a terminal to fetch and compile the library.

Source Files.

All of the source files you write for milestones will live in the STBU module. In other words, you should create subdirectories of lib/ and test/ named STBU/ and place all of your source/test files in there (or a subdirectory).

For example, a source file that defined a module for bathing animals might begin:

  defmodule STBU.BathTime do

    def shampoo(animal) do

      ...

    end

  end

And the containing file would be located at lib/STBU/BathTime.ex.

Building and Running.

The mix compile command compiles the files in the project, while mix test will run the tests in the test/ directory. Launching iex -S mix will begin a REPL session with the current project files loaded.