DEV Community

Joel Jucá
Joel Jucá

Posted on

My Makefile for Elixir/Phoenix

Following my last post about GNU Make and Makefiles, I thought I could share the Makefile I'm using in my Elixir/Phoenix projects to have a standard CLI interface to manipulate all of them.

I designed this Makefile to be easily adapted to be used in other projects (I've been coding with some uncommon tools – Bash, Hy, Janet, Python, SQLite, etc., so remembering the commands specific to each of these is hard. So a standard Makefile helps a lot. I also copy-paste it in new projects, edit its tasks, possibly delete some (eg.: delete database-related commands if they're not needed), and I'm good to go.

If you're not familiar with Makefiles, check this quick introduction to Makefiles I wrote last week. It might give you good insights about what GNU Make is and how it can be used for.

With no ceremony, here's my "standard" Elixir/Phoenix Makefile:

.PHONY: setup build run lint fmt test test.watch db.setup db.create db.migrate db.seed db.drop db.reset repl repl.run repl.test

# Project-wide

setup:
    make build \
    && make db.setup

build:
    mix do deps.get + compile

run:
    mix phx.server

# Code quality

lint:
    mix format --check-formatted

fmt:
    mix format

test:
    mix test

test.watch:
    mix test.watch

# Database

db.setup:
    make db.create \
    && make db.migrate \
    && make db.seed

db.create:
    mix ecto.create

db.migrate:
    mix ecto.migrate

db.seed:
    mix run priv/repo/seeds.exs

db.drop:
    mix ecto.drop

db.reset:
    make db.drop \
    && make db.setup

# REPL

repl:
    iex -S mix

repl.run:
    iex -S mix phx.server

repl.test:
    iex -S mix test.watch
Enter fullscreen mode Exit fullscreen mode

Most of these Mix subcommands are provided by Phoenix, so they work for any Phoenix project you adopt it, with two exceptions: test.watch and repl.test. Both assume that you're using mix_test_watch to run tests every time a change in the source code is made. Everything else is pretty much bare Phoenix subcommands.

That's it. An autocompletion-powered CLI wrapper to work with Phoenix. Now you can type make, hit Tab (once or twice, depending on how your shell is configured), and get autocompletions for the most important tasks of your project.

Top comments (2)

Collapse
 
jontix profile image
Jσɳƚαιx

noice

Collapse
 
raphael_joer_4cfde7f537ad profile image
Raphael Joer

Perfect! Helps me alot