How to become more productive using Makefile

django Jun 27, 2020

Introduction

You want to become more productive, automate your workflow, there are many tools to help you achieve your goal.

make gives you the flexibility to run and compile a program from source.
In this guide, we will use it to automate our development workflow.
Note: I will use Django as an example, but the technics are applicable to other technologies.
In this guide, I will use Makefile to automate many things in Django like database migration, superuser, deployment, and more.
At the end of the guide, you'll be more productive in your development.
Before you continue, ensure that make is installed in your system.

Installation

In Order to use make we need two(2) things one(1) we need to install it in our system and finally we need to create a file called Makefile.

To install make in your system you can follow these links :
OSX
Windows

Basic examples

To explore make let's begin with this simple program, go to any directory, and create a Makefile.
Note: Don't give it an extension.

$ mkdir test_make && cd test_make
$ touch Makefile

Open the file with your text editor and put this

say_hello:
        echo "Hello world"

run the file by typing make inside the directory test_make

$ make

What is the output?
Here is what I get.

echo "Hello world"
Hello world

Django integration

If you're a Django developer, you know how tedious is to repeat again and again these commands :

python manage.py runserver
python manage.py makemigrations
python manage.py migrate
...

Let's automate them with make

Clone the example project here and follow the README guide.

git clone https://github.com/xarala221/consume-restfull-api-with-django && cd consume-restfull-api-with-django

Run the application to ensure that everything is working fine.

python manage.py migrate
python manage.py runserver

Open your browser and go to http://localhost:8000.

Inside the project, folder created a file called Makefile and add these lines.

touch Makefile

The final file will look like this

SHELL := /bin/bash

help:
	@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$'

install:
	pipenv install

activate:
	pipenv shell

run:
	python manage.py runserver

migration:
	python manage.py makemigrations

migrate:
	python manage.py migrate

superuser:
	python manage.py createsuperuser

heroku:
	git push heroku master

deploy:
	docker-compose build
	docker-compose up -d

down:
	docker-compose down

you'll be more productive with these tips.

Conclusion

Become more productive and make things done is very important, in this guide you've learned how to automate your workflow by using make.

What do you think about this? Let us know in the comment section.

We started sharing these tutorials and articles to help and inspire developers and engineers around the world. If our blog has been helpful to you, feel free to buy us a coffee to keep us going :).

Buy us a coffeeBuy us a coffee

Ousseynou Diop

Full stack Developer & Python Lover

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.