DEV Community

Cover image for Why you should use a Makefile
cerico
cerico

Posted on • Edited on

Why you should use a Makefile

Why you should use a Makefile

If you have a project or application where you need to run a lot of commands, npm, rake, bin, rails, etc, then you should consider using a Makefile as a front end utility. This has a few advantages:

  • You can run multiple commands at once
  • All commands are launched with the same command, make
  • All commands are self-documenting

Lets look at an example of a sample Makefile

tldr:
        @echo Available commands
        @echo ------------------
        @grep '^[[:alpha:]][^:[:space:]]*:' Makefile | cut -d ':' -f 1 | sort -u | sed 's/^/make /'
install:
        bundle install
        yarn install --check-files
exchange_rates:
        ./bin/rake exchange_rates:refresh
subscription_plans:
        bundle exec rails runner 'SubscriptionPlan.import_all_plans'
generate:
        ./bin/init.js
storybook:
        yarn storybook
start:
        npm run start
Enter fullscreen mode Exit fullscreen mode

Here we have a variety of commands that are all launched in different ways. With a Makefile, we can run them each of them with an easier to remember make \<command\>. We can also see all the commands available with make tldr or just make. Any new commands added to the Makefile are automatically caught by the make tldr command.

In action

☁  cericow@kelso:esplanade ➜ make
Available commands
------------------
make tldr
make install
make exchange_rates
make subscription_plans
make generate
make storybook
Enter fullscreen mode Exit fullscreen mode

Running the make command picks up the first rule (tldr) and runs it. The rule greps the file for available rules and lists them out in the format in which they should be run.

Top comments (0)