Elixir Tasks

Note to the reader: These commands are current as of Phoenix 1.3.

I've been working on a side project to hone my skills in another language and web framework. I've decided to pick Elixir and Phoenix. The project I'm working on is named "Dank Kombucha". Although it's not quite in a working state, I've gone through quite a few learning experiences since the start. There's nothing quite like a project to really put your theoretical knowledge of something to the test.

Here's what I've learned about Elixir tasks thus far.

What are mix tasks?

Put simply, mix tasks are Elixir functions that offer behavior to complete a built-in or user defined task which can be run from the command line. Tasks are often associated with modules provided by hex dependencies but there are also some built in tasks offered by Elixir.

Here's an example of a task that is offered by Phoenix to create a new Phoenix project.

mix phx.new my_project

Discovering mix tasks

If you want to learn what tasks are available in your project you can run mix help from within an Elixir project. This will create a list of all possible tasks that can be run from within the Elixir project that you're working in.

Pairing mix help with grep can help you to cut down on the noise if you already know what you're looking for.

You can also list all tasks under a dependency's module by executing commands of the following form mix <dependency_name>. So for example if you want to list tasks that are provided by Ecto you can run the following.

Ecto Tasks

For some reason there are some tasks that are not listed when those options are run. I found them by reading through this authN/Z example.

# create the database, run migrations and seed data
mix ecto.setup
# drop the database and then run ecto.setup
mix ecto.reset

The above commands are super useful, specifically the ecto.reset task. For example if I want to test user authentication of my personal email address, and I want to start tabula rasa, just running mix ecto.reset allows me to start from ground zero.

Phoenix Tasks

# see what routes are defined
mix phx.routes

Of course for Phoenix, you can always check the router.ex file for a list of routes, but sometimes it's nice to get a pretty printed listing instead.