Skip to content

Instantly share code, notes, and snippets.

@Pierstoval
Last active Mar 2, 2021
Embed
What would you like to do?
"Check requirements" command for your Makefile
# Use it like this:
#
# $ printf $(_TITLE) "Something in green" "The message"
#
# This will output something like this:
#
# [Something in green] The message
#
# (of course, with proper colors)
_TITLE := "\033[32m[%s]\033[0m %s\n" # Green text
_ERROR := "\033[31m[%s]\033[0m %s\n" # Red text
# This function has to be used this way:
#
# @$(call check_binary_command,command name,command line and options to execute)
#
# Example :
#
# @$(call check_binary_command,PHP,php --version)
#
# Note the absence of spaces between the commas ",".
# If you added spaces, they would be added to the commands or output.
#
# This command will display "[OK] {command name} is installed"
# if the {command line and options to execute}'s exit code is 0.
# Else, it will display "[KO] {command name} is not installed",
# and the entire Make command will fail,
# thanks to the temporary file defined later in this config.
#
# Side-note:
# $(1) is the first argument, $(2) the second, etc.
# The "@" sign is here to avoid Make to display the full command-line.
#
define check_binary_command
@($(2) >/dev/null 2>&1) \
&& (printf $(_TITLE) "OK" "$(1) is installed") \
|| (touch var/.err && printf $(_ERROR) "KO" "$(1) is not installed.")
endef
# This temporary file will be present if *any* check command fails.
# If it is present, it allows the "check-requirements" target to fail too,
# which will prevent Make from executing any further command.
CHECK_TMP_FILE=var/.err
check-requirements:
@[[ -f $(CHECK_TMP_FILE) ]] && rm $(CHECK_TMP_FILE)
@$(call check_binary_command,PHP,php --version)
@$(call check_binary_command,Symfony CLI,symfony version)
@$(call check_binary_command,Composer,composer --version)
@$(call check_binary_command,Node.js,node --version)
@$(call check_binary_command,NPM,npm --version)
@$(call check_binary_command,Docker,docker --version)
@$(call check_binary_command,Docker Compose,docker-compose --version)
@([[ -f $(CHECK_TMP_FILE) ]] && \
echo "" && \
printf $(_ERROR) "Requirements failed" "Please check your setup." && \
echo "" && \
rm $(CHECK_TMP_FILE) && \
exit 1) || echo -n ""
.PHONY: check-requirements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment