Skip to content

Instantly share code, notes, and snippets.

@dreknix
Last active June 18, 2023 16:30
Show Gist options
  • Save dreknix/b5e50acb5608036a7af37bf8b4bfa45b to your computer and use it in GitHub Desktop.
Save dreknix/b5e50acb5608036a7af37bf8b4bfa45b to your computer and use it in GitHub Desktop.
Template for Makefiles with '.env' file, precondition check and automatic help.
#!/usr/bin/env make
ENV_FILE:=./.env$(if $(FOO),_$(FOO),)
ifneq (,$(wildcard ${ENV_FILE}))
include ${ENV_FILE}
VARS:=$(shell sed -ne 's/ *\#.*$$//; /./ s/=.*$$// p' .env )
$(foreach v,$(VARS),$(eval $(shell echo export $(v)="$(shell echo $($(v)) | sed "s/^'//")")))
else
$(error Environment file '${ENV_FILE}' not found)
endif
.DEFAULT_GOAL:=help
.PHONY: .pre foobar help
.pre:
@ls . > /dev/null 2>&1 || { echo "failure!"; exit 1; }
foobar: .pre ## Do the main thing
echo "Doing the main thing..."
help: .pre ## Show this help
@printf "\nUsage: make \033[36m<target>\033[0m\n"
@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-10s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@printf "\nVariable \033[36mFOO\033[0m can be used to switch between environments, e.g.: FOO=dev make \033[36m<target>\033[0m\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment