Skip to content

Instantly share code, notes, and snippets.

@elliasmatheus
Created March 3, 2024 23:51
Show Gist options
  • Select an option

  • Save elliasmatheus/c182be29c3b6361fc5808e4f2a6ccaf9 to your computer and use it in GitHub Desktop.

Select an option

Save elliasmatheus/c182be29c3b6361fc5808e4f2a6ccaf9 to your computer and use it in GitHub Desktop.
Makefile
# Use bash syntax
SHELL := /bin/bash
ARG := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))
TARGETS_WITH_ARGS = help
.PHONY: TARGETS_WITH_ARGS
.DEFAULT_GOAL := help
## -- Help --
## Display this help message
help:
@printf "Usage: make \033[32m<command>\033[0m [ARGUMENT1=value1] [ARGUMENT2=value2] \n";
@awk -v target=$(ARG) '{ \
# Handle PHONY help \
if ($$0 ~ /^.PHONY: [a-zA-Z\-\_0-9]+$$/) { \
helpCommand = substr($$0, index($$0, ":") + 2); \
if ((helpMessage && !target) || (target && helpCommand == target":")) { \
printf "\n\033[36m%-20s\033[0m %s\n", \
helpCommand, helpMessage; \
helpMessage = ""; \
} \
# Handle command declaration \
} else if ($$0 ~ /^[a-zA-Z\-\_0-9.]+:/) { \
helpCommand = substr($$0, 0, index($$0, ":")); \
if ((helpMessage && !target) || (target && helpCommand == target":")) { \
printf "\n\033[36m%-20s\033[0m %s\n", \
helpCommand, helpMessage; \
helpMessage = ""; \
} \
# Handle multiline comment \
} else if ($$0 ~ /^##/) { \
if (helpMessage) { \
helpMessage = helpMessage"\n "substr($$0, 3); \
} else { \
helpMessage = substr($$0, 3); \
} \
# Handle empty case (begin and end) \
} else { \
if ((helpMessage && !target) || (target && helpCommand == target":")) { \
print "\n "helpMessage"\n" \
} \
helpMessage = ""; \
} \
}' \
$(MAKEFILE_LIST)
echo_args:
@echo "$(ARG)"
# If the first argument is on TARGETS_WITH_ARGS
ifneq (,$(findstring $(firstword $(MAKECMDGOALS)),$(TARGETS_WITH_ARGS)))
# Turn args into do-nothing targets so make won't complain about missing targets or try to run them as recipes
$(eval $(ARG):;@:)
endif
# If the first argument is ephemeral_
ifneq (,$(findstring ephemeral_, $(firstword $(MAKECMDGOALS))))
# Turn args into do-nothing targets so make won't complain about missing targets or try to run them as recipes
$(eval $(ARG):;@:)
endif
# If the first argument is prod_
ifneq (,$(findstring prod_, $(firstword $(MAKECMDGOALS))))
# Turn args into do-nothing targets so make won't complain about missing targets or try to run them as recipes
$(eval $(ARG):;@:)
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment