How to pass parameters to a Makefile

Asked

Viewed 61 times

0

I’m developing a Django app on Docker and not have to keep typing docker-compose exec djangoapp python manage.py <alguma coisa> i wrote a Makefile to run the commands I most use. I just don’t know how to make the Makefile take the parameters I pass to it.

For example: I can run migrate like this make migrate with my Makefile but I can’t create a new app like this make startapp novoapp. My question is how do I make the Makefile get the parameters I pass to it?

My Makefile is:

COMPOSE=docker-compose
DJANGOAPP=$(COMPOSE) exec djangoapp python manage.py


build:
    $(COMPOSE) build

up:
    $(COMPOSE) up

start:
    $(COMPOSE) up -d

down:
    $(COMPOSE) down

restart: down start

migrate:
    $(DJANGOAPP) migrate

makemigrations:
    $(DJANGOAPP) migrations

startapp:
    $(DJANGOAPP) startapp

startproject:
    $(DJANGOAPP) startproject

1 answer

1


From what I understand in that reply, you can only pass environment variables to Makefile. Example:

Makefile:

startapp:
    $(DJANGOAPP) startapp $(nome_app)

Command line:

make startapp nome_app=novoapp

But this is not very intuitive, because you need to define all the arguments individually for each command.

To do this you want, instead of creating a Makefile, I use an alias for these most common commands, making it possible to use the commands according to what I need:

Code snippet loaded on ~/.bashrc (I like to keep it separate in a file .bash_aliases):

alias dup='docker-compose up'
alias dcb='docker-compose build'
alias dcr='docker-compose run --rm'
alias dce='docker-compose exec'

# pode usar um alias dentro do alias
alias django='dcr djangoapp python manage.py'

# por gosto pessoal, gosto de deixar o comando completo no alias, 
# para não criar dependências entre os alias, tem suas 
# vantagens e desvantagens 
alias djangoexec='docker-compose exec djangoapp python manage.py'

With that, after updating my bash session (source ~/.bashrc), can execute:

# Obs: com o run --rm não preciso dar um up no docker-compose antes
django startapp novoapp

dcb

# Exemplo com parâmetros
dup -d
djangoexec startapp novoapp

Another trick I use a lot is the history of bash. To not need to add all commands as an alias, I search for other commands that I have previously executed with CTRL + R. If after a while I realized that this was a recurring command, then I would create an alias.

  • 1

    alias... Good! I didn’t even think about it, I went straight to Makefile. Thanks for the tip!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.