Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Elixir Tools

Every big project needs good tooling. In this lesson, I’m going to teach you what tools you’ll need to manage your projects and add dependencies.

Related Links

2.4 Elixir Tools

Hi, and welcome back to Get Started With Elixir. In this lesson, we are going to look at useful tools you want to use with your Elixir project. Let's start from mix. It is the build tool for Elixir. It allows you to organize your project, handle dependencies, provide [INAUDIBLE] tasks and also allows you to create your custom tasks. Let's jump right into the Shell and create a new project. I'm going to call this one coder, since I want to encode and decode text here. In the editor on the left side you can already see the project structure mix created for you. It has a readme file that already explains how to integrate your project into another projects. The mix.exs file contains much of your project's definition. It is similar to a package.json in JavaScript or a gem file in Ruby. It contains the name and version of your project, as well as the dependencies it requires. Then, there is the test directory, which we will learn about more in the next lesson. The lib directory is the one where most of your code will reside. Mix already created a file with the same name as your project. Let's create another file within the coder called encoder. Since I'm in the coder namespace, I have to define the module as coder.encoder. Within the module let's just create a simple function and code that takes a text and reverses it. To run iex within a mix project, type iex -S mix. iex will automatically find and compile your code. Now we can run Coder.Encoder.encode with a string, and it will reverse it for us. The final folder you can see here is the config folder. You can define your project configuration here. Let's look at an example. To create a config you can use the config function to define a keyword list for your project. There are two different ways you can use it, and I will show you both. First, there is a to argument function that takes the name of your project, here coder, and a keyword list. Let's use prefix and set it to Encoded. In the module, we can use the value by calling Application.get_env coder prefix. And for instance prepend it to the encoded value. While it's possible to recompile your project without restarting the shell with the recompile function. This doesn't hold true to config files. So we have to restart iex. When we run the encode function again we can see that the prefix is correctly prepended. The second way of configuring your application, or more specifically, certain modules, is by using the module name with the config function. In this case, we have to access it a bit differently. Instead of requesting the prefix atom, we can use the __MODULE__ macro to get the keyword list and then access it the old fashioned way with square brackets. Now that you know what mix is and how you can use it, we also have to talk about hex, the package manager for the Erlang ecosystem. It works similar to npm or rubygems and integrates seamlessly with mix. Before you can use hex with your mix project you will need to install it by executing mix local.hex. To find a hex package you can either use the hex web search or use mixhex.search if you already know roughly how the package is called. In my case, I want to install a package called poison, which is adjacent parser for Alexa. To add it, I'm going to add it to the project's dependencies. Here I have to specify the project name and the version I want. This falls to semantic versioning scheme. To fetch and compile the dependencies I can use the mix task deps.git. I can browse around multiple tasks at once by using do and a command. mix do deps.get, deps.compile. This first fetches the specified dependencies and then compiles them. Now we can use the poison package in our project. I'm going to create a function to_json that accepts some data. Within the function I can call Poison.encode and pass the data along. Let's try it out. In the Elixir shell I'm going to call Coder.Encoder.to_json, with a map that represents to cause. Since I can't use structs here without additional work, I'm just using a simple map. It returns the json string. To recap, Mix is the build tool for Elixir. It handles project organization, dependency management and provides useful tasks. Your project is defined in the mix.exs file including name, version and dependencies. The hex package manager allows you to easily use third-party libraries. In the next lesson, we are going to learn about a subject that has a very high standing in the Elixir world, testing. See you there.

Back to the top