Skip to content

Instantly share code, notes, and snippets.

@vudaltsov
Created December 10, 2019 14:55
Show Gist options
  • Save vudaltsov/1801431f6f5a9896e9a7e90d74185a96 to your computer and use it in GitHub Desktop.
Save vudaltsov/1801431f6f5a9896e9a7e90d74185a96 to your computer and use it in GitHub Desktop.
Self-documenting Makefile with categories and aliases
##
## Tools
## -----
ls: list-files
list-files: ## List files
ls -lah
.PHONY: ls list-files
# -----------------------
help:
@awk ' \
BEGIN {RS=""; FS="\n"} \
function printCommand(line) { \
split(line, command, ":.*?## "); \
printf "\033[32m%-25s\033[0m %s\n", command[1], command[2]; \
} \
/^[0-9a-zA-Z_-]+: [0-9a-zA-Z_-]+\n[0-9a-zA-Z_-]+: .*?##.*$$/ { \
split($$1, alias, ": "); \
sub(alias[2] ":", alias[2] " (" alias[1] "):", $$2); \
printCommand($$2); \
next; \
} \
$$1 ~ /^[0-9a-zA-Z_-]+: .*?##/ { \
printCommand($$1); \
next; \
} \
/^##(\n##.*)+$$/ { \
gsub("## ?", "\033[33m", $$0); \
print $$0; \
next; \
} \
' $(MAKEFILE_LIST) && echo "\033[0m"
.DEFAULT_GOAL := help
.PHONY: help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment