Skip to content

Instantly share code, notes, and snippets.

@gfranxman
Last active May 9, 2024 14:36
Show Gist options
  • Save gfranxman/73b5dc6369dc684db6848198290330c7 to your computer and use it in GitHub Desktop.
Save gfranxman/73b5dc6369dc684db6848198290330c7 to your computer and use it in GitHub Desktop.
Makefile help target. Allows you to define groups of targets by starting a line with ## or expose a target with help test by including ## indented or after the target's dependencies.
.PHONY: help
# useage config
TARGETLEN:=$(shell echo 16)
# colors
BLUE:=$(shell echo "\033[0;36m")
GREEN:=$(shell echo "\033[0;32m")
YELLOW:=$(shell echo "\033[0;33m")
RED:=$(shell echo "\033[0;31m")
END:=$(shell echo "\033[0m")
## Documentation
help: ## Render the readme
@mdn readme.md || cat readme.md
usage: ## This help.
@awk 'BEGIN { FS = ":.*##"; target="";printf "\nUsage:\n make $(BLUE)<target>\033[33m\n\nTargets:$(END)" } \
/^[.a-zA-Z_0-9-]+:.*?##/ { if(target=="")print ""; target=$$1; printf " $(BLUE)%-$(TARGETLEN)s$(END) %s\n\n", $$1, $$2 } \
/^([.a-zA-Z_0-9-]+):/ {if(target=="")print "";match($$0, "(.*):"); target=substr($$0,RSTART,RLENGTH) } \
/^\t## (.*)/ { match($$0, "[^\t#:\\\\]+"); txt=substr($$0,RSTART,RLENGTH);printf " $(BLUE)%-$(TARGETLEN)s$(END)", target; printf " %s\n", txt ; target=""} \
/^## (.*)/ {match($$0, "[^\t#\\\\]+"); txt=substr($$0,RSTART,RLENGTH);printf "\n$(YELLOW)%-$(TARGETLEN)s$(END)\n", txt ; target=""} \
' $(MAKEFILE_LIST)
@# https://gist.github.com/gfranxman/73b5dc6369dc684db6848198290330c7#file-makefile 05/09/2024
.DEFAULT_GOAL := usage
VENV_DIR ?= venv
VENV_RUN = . $(VENV_DIR)/bin/activate
PIP_CMD ?= pip
## Getting Started
usage: ## Show this help
@grep \#\# $(MAKEFILE_LIST) | fgrep -v thisline | sed -e 's/##//' | sed 's/^ /_/g' | sed 's/^\([a-z]\)/\t\1/g' | sed 's/^_/\n/g'
# https://gist.github.com/gfranxman/73b5dc6369dc684db6848198290330c7#file-makefile
install: ## Install dependencies in local virtualenv folder
(test `which virtualenv` || $(PIP_CMD) install --user virtualenv) && \
(test -e $(VENV_DIR) || virtualenv $(VENV_OPTS) $(VENV_DIR)) && \
(test ! -e requirements.txt || ($(VENV_RUN); $(PIP_CMD) install -r requirements.txt))
## Development
publish: ## Publish the library to the central PyPi repository
# build and upload archive
($(VENV_RUN) && pip install setuptools && ./setup.py sdist upload)
clean: ## Clean up
rm -rf $(VENV_DIR)
.PHONY: clean publish install usage
@gfranxman
Copy link
Author

updates the regexes to allow numbers in the target names. ( like k8s )

@gfranxman
Copy link
Author

Minor indentation for the sub commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment