Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@carlosonunez
Created December 15, 2020 19:23
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 carlosonunez/7313f343392407fa754041fc523546aa to your computer and use it in GitHub Desktop.
Save carlosonunez/7313f343392407fa754041fc523546aa to your computer and use it in GitHub Desktop.
Add a super nice help documentation to any Makefile
# Add this to your Makefile or include it as a separate .mk
# to generate instant professional-looking documentation for your Make
# targets.
#
# To add documentation for a target name, add a ## comment after the _first_ definition of that target.
# To add documentation for environment variables, add a ## comment after the _first_ definition of that environment variable.
# To see the docs, run: make usage.
#
# Inspired by: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
PROJECT_NAME ?= example ## Defines the name of your project.
usage: ## Prints this help text.
printf "make [target]\n \
\n\
Hack on $(PROJECT_NAME).\n \
\n\
TARGETS\n\n\
$$($(MAKE) generate_target_docs)\n\n\
ENVIRONMENT VARIABLES\n\n\
$$($(MAKE) generate_env_var_docs)\n\n\
"
test_target: ## A test to show that this mk works.
echo 'It works!'
generate_target_docs:
fgrep -h '##' $(MAKEFILE_LIST) | \
fgrep -v '?=' | \
fgrep -v gre | \
sed 's/\\$$//' | \
sed -e 's/##//' | \
grep -v 'sed' | \
sort -u | \
awk -F': +' '{printf"%-5s%-20s%s\n","",$$1,$$2}'
generate_env_var_docs:
fgrep '?=' $(MAKEFILE_LIST) | \
grep -v grep | \
sed 's/\?=.*##/;/' | \
grep -v sed | \
sort -u | \
awk -F' ; ' '{printf"%-5s%-20s%s\n","",$$1,$$2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment