Category Archives: makefile

Laravel – Makefile Recipes

Table for contents

  1. Generator
  2. Getting information
    1. Laravel and composer
    2. Endpoints
    3. Log file
  3. Running developer tools
    1. Tinker
  4. Testing
    1. Testing
  5. Clean up
    1. Clear cache
    2. Clear storage
    3. Clear log file (default configuration)

Generator

You can generate recipes at Nicomedes.

Getting information

Laravel and composer
.PHONY: info
info:
php artisan about
php composer.phar diagnose
Endpoints
.PHONY: routes
routes:
php artisan route:list
Log file
.PHONY: logs
logs:
grep ERROR storage/logs/laravel.log

Running developer tools

Tinker
.PHONY: tinker
tinker:
php composer.phar dump-autoload
php artisan tinker

Testing

Testing
.PHONY: test
test:
php artisan test --env=testing --stop-on-failure

Clean up

Clear cache
php composer.phar dump-autoload
php artisan optimize:clear
Clear storage
.PHONY: clear\:storage
clear\:storage:
rm -rf ./storage/app/*
Clear log file (default configuration)
.PHONY: clear\:log
clear\:log:
rm storage/logs/laravel.log

makefile – Tips and tricks

Table of contents

  1. Open hyperlinks (Ubuntu)
  2. Run multiple tasks
  3. Select specific npm version

A handy recipe to automatically open links via a makefile:

.PHONY: browse
browse:
	xdg-open http://localhost:8000

Run multiple tasks

.PHONY: serve
serve:
	make -j 2 serve-backend serve-frontend browse

.PHONY: serve-frontend
serve-frontend:
	npm run hot

.PHONY: serve-backend
serve-backend:
	php artisan serve

Select specific npm version

.PHONY: install
install:
	. ${NVM_DIR}/nvm.sh && nvm install 14.0.0 && npm install

.PHONY: serve
serve:
	reset && . ${NVM_DIR}/nvm.sh && nvm use 14.0.0 && npm run serve

makefile helper function

Small snippet to print all rules starting with a specific prefix (for example ssh) when you use semicolon to organize subrecipes:

.PHONY: ssh
ssh:
	@echo "recipes:"
	@cat makefile | grep -o "^ssh\\\\:[^(: )]*" | sed 's/\\//' | sed 's/^/  - /'

Then you can add recipes with the following format:

.PHONY: ssh\:x
ssh\:x:
	...
.PHONY: ssh\:y
ssh\:y:
	...
.PHONY: ssh\:z
ssh\:z:
	...

Typing make ssh will print the available options while typing make ssh:x will only execute recipe x.

Modern Makefile example

See: https://medium.com/stack-me-up/using-makefiles-the-right-way-a82091286950

TSC=./node_modules/.bin/tsc
SASS=./node_modules/.bin/sass
MARKUP_FILES=$(wildcard src/index.hbs src/partials/*hbs)
STYLE_FILES=$(wildcard src/index.sass src/styles/*.sass)
SCRIPT_FILES=$(wildcard src/index.ts src/scripts/*.ts)
IMAGES=$(wildcard src/index.ts src/media/*.jpeg)
VIDEOS=$(wildcard src/index.ts src/media/*.mp4)

# ...

.DEFAULT_TARGET: all

all: build markup scripts styles media

build:
	mkdir -p $@
	touch $@

node_modules: package.json
	npm install
	touch $@

markup: build/index.html
build/index.html: $(MARKUP_FILES)
	hbs src/index.hbs --partial 'src/partials/*.hbs' -o ./build

scripts: build/index.js
build:index.js: tsconfig.json $(SCRIPT_FILES) node_modules
	$(TSC) --build $<
	
styles: build/index.css
build/index.css: $(STYLE_FILES) node_modules
	$(SASS) $< $@

media: images videos

images: $(IMAGES)
	for image in $^; do \
		imagemin $$image > build/media/$$image ; \
	done
	touch $@

videos: $(VIDEOS)
	for video in $^; do \
		HandBrakeCLI -i $$video -o build/media/$$video ; \
	done
	touch $@

.PHONY: clean
clean:
	rm -Rf ./build/*