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.
To know more about Make program we can write this command in the terminal.
you should see something like this :
make - GNU make utility to maintain groups of programs
if you faced any errors make sure that
make utility installed 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.
for most Linux distributions you will find make installed on your machine.
- Ubuntu run
sudo apt-get install build-essentialfor other distributions check your distro package manager.
- Mac OS run
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
- Create a file in the application root folder named
makewhat to do.
makefileand paste the next code snippet and make sure to use tabs not spaces :
serve: php -S localhost:8000 -t public
make serve in the terminal instead of
php -S localhost:8000 -t public this is a basic usage for makerfile.
Makefile consists of rules as the next structure of the rule :
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 :
:this the name we will execute the rule with.
:this is a list of prerequisites rule we declared before and want to run before The rule.
- Next line starts with tab contain a command we want to run or script consists of several lines.
#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;
- You can run any rule just by type
make rule-nameterminal 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
fresh-dbrules only execute a command you can write any bash command.
fresh-serverule runs the
serverules as prerequisites.
fresh-testrule runs the
testrules as prerequisites and bash
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.