Makefiles Make It Easy
Tags: make, makefile, php, lumen
Published: 2020-05-15 21:35:06

---
title: Makefiles Make It Easy
published: true
description: How to run your applications easily
tags: #make,#makefile,#php,#lumen
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/tonu89o6h0k079lruojt.png
---
Sometimes when we developing applications we have a lot of commands for a run, test, format code, etc. and those commands may be long and we want to alias it, for many reasons we can't memorize these commands and we want to share it with other team members to make development easy for this situation we will use `make` utility to help us.

#Lets Start
To know more about Make program we can write this command in the terminal.
```css
man make
```

you should see something like this :
> make - GNU make utility to maintain groups of programs

if you faced any <mark>errors</mark> make sure that `make` utility [installed](#install-make) on your machine.

as you see in `man make` description that `make` developed to make compile C programs easy and can be used in other tasks.

#Install Make<a name="install-make"></a>
for most Linux distributions you will find make installed on your machine.
- Ubuntu run `sudo apt-get install build-essential` for other distributions check your distro package manager.
- Mac OS run `xcode-select --install`.
- [Windows](http://gnuwin32.sourceforge.net/packages/make.htm)

#Get your hands dirty
Let us apply `make` utility on a PHP lumen app for example if we want to run lumen app with PHP internal server we need to run this command `php -S localhost:8000 -t public` in the terminal what if we make this command shorter to convert it only to `make serve`.

1. Create a file in the application root folder named `makefile` to tell `make` what to do.
2. Open `makefile` and paste the next code snippet and make sure to use <mark>tabs not spaces</mark> :
```make
serve:
        php -S localhost:8000 -t public
```
3.Run `make serve` in the terminal instead of 
`php -S localhost:8000 -t public` this is a basic usage for makerfile.

#Lets Explain
Makefile consists of rules as the next structure of the rule :
```make
target … : prerequisites …
        recipe
        …
        …
```

makefiles have a lot of detail but we will explain what we need in our scope so let us describe `make` rule as :
1. Before `:` this the name we will execute the rule with.
2. After `:` this is a list of prerequisites rule we declared before and want to run before The rule.
3. Next line starts with <mark>tab </mark> contain a command we want to run or script consists of several lines.


#Example
```make
#Variables
PHP = php

#Rules
serve:
        $(PHP) -S localhost:8000 -t public

test:
        ./vendor/bin/phpunit

fresh-db:
        $(PHP) artisan migrate:fresh

fresh-serve: fresh-db serve

fresh-test: fresh-db test
        echo Done;
```

##Expalination
- You can run any rule just by type `make rule-name` terminal opened in your app root folder with makefile in it.
- As in the first block of the file, we can declare variables to reuse them with `$(VariableName)` notation.
- `serve`, `test` and `fresh-db` rules only execute a command you can write any bash command.
- `fresh-serve` rule runs the `fresh-db` and `serve` rules as prerequisites.
- `fresh-test` rule runs the `fresh-db` and `test` rules as prerequisites and bash `echo` command.


#Conclusion
I can't cover all `make` features in one article I just want to explain how I use makefiles to the development process easy you use it with any technology stack you want.


###References
1. [GNU make](https://www.gnu.org/software/make/manual/html_node/index.html#SEC_Contents)