Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rogeruiz
Last active January 22, 2022 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogeruiz/c112313f5c480b830b3274fc0ac1ad21 to your computer and use it in GitHub Desktop.
Save rogeruiz/c112313f5c480b830b3274fc0ac1ad21 to your computer and use it in GitHub Desktop.
Makefile template
# This is a template Makefile that I use for automating projects that might
# require one.
#
# For more guidance around Makefiles, the checkout out the helpful
# [makefiletutorial](https://makefiletutorial.com/).
directory = tmp
# A loop that automatically runs to check if executables are in the $PATH variable before running any targets.
EXECUTABLES = echo cat grep
K := $(foreach exec,$(EXECUTABLES),\
$(if $(shell command -v $(exec)),some string,$(error "No $(exec) executable in PATH")))
# A function to check if a environment variable is defined or not.
check_defined = \
$(strip $(foreach 1,$1, \
$(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
$(if $(value $1),, \
$(error Undefined $1$(if $2, ($2))))
$(directory):
@echo "Folder ./$(directory) does not exist"
mkdir -p $@
all: $(directory) ## This is the default target. It depends on a ./tmp/ directory to exist...
# An example of checking if something is running on port 8800.
# This could be used for anything like a file existance or a particular
# PID is running. The object of this target is to check if something is true via
# exit status and then output a message if it failed and then to also fail on
# the target so Make doesn't keep running.
.PHONY: is-server-running
is-server-running:
@nc -zv 127.0.0.1 8800 &> /dev/null || \
if [ $$? -eq 1 ]; then echo "The development server is not running.\nRun \`make run-server\` in another terminal."; false; fi
.PHONY: install
install: ## A target to install stuff with required $var1 and $var2 to be passed into the target...
$(call check_defined, var1)
$(call check_defined, var2)
# do stuff here..
.PHONY: help
help: ## Outputs this help message.
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment