diff --git a/Makefile b/Makefile index 12629ca7dcffd1ce0da9c8252b557c0b8215a20d..60a5f94b61895e882b46c02186c61391bc8261a5 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ MEX_DESCRIPTION="This provides some additional tools for makefiles." MEX_LICENSE="Released under the BSD two-clause license, see the LICENSE file for more information." MEX_ASSURE="python3 ls tree clang" +MEX_ENVIRONMENT="local.env second.env" include makext.mk @@ -8,6 +9,14 @@ help: .help demo-assure: .assure @echo "All good, continuing..." + +demo-envars: + @echo "Envrionment variables" + @echo " HOME: $(HOME)" + @echo " TERM: $(TERM)" + @echo " ENV: $(MEX_ENVIRONMENT)" + @echo " AUDIO_BUCKET: $(AUDIO_BUCKET)" + @echo " DB_HOST: $(DB_HOST)" build-app: clean-cache # Build the application @echo "Building the application..." diff --git a/README.md b/README.md index ff8801476926ba7509d3c7a95337d491345b7650..947115717a45f7ce05269cf73f0f10ba5301e910 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,11 @@ > This extensions are abusing GNU Make in some sense since it was not > meant to really be a task runner. Keep that in mind. However, dispite > that, I constantly find myself using it as such. -| Extension | Description | -|-----------|-----------------------------------------------------| -| .help | Displays all targets with a comment in help format. | -| .assure | Check for the existance of programs on a machine. | +| Extension | Description | +|-------------|-----------------------------------------------------| +| help | Displays all targets with a comment in help format. | +| assure | Check for the existance of programs on a machine. | +| environment | Loads environmental variables from other files. | Additional features: @@ -146,7 +147,7 @@ ## Assure extension Often times project uses multiple programs and to ensure that these -programs are already installed before recipes are executed `assure` can +programs are already installed before recipes are executed `.assure` can be used. If programs are missing recipes can only partially be executed leaving project in a potentially broken state. @@ -163,6 +164,41 @@ `.assure` prerequisite will loop over the list of programs defined in `MEX_ASSURE` variable and in case one is missing will exit `make` with status code error 1. This will stop executing the recipe and therefore not execute anything in target `build-app`. + +## Environment extension + +This extension helps loading of additional environmental files in your +project. The files should have environmental variables defined in the +usual way. Seperate each file by a space and that is about it. + +If a file is missing this will break the execution of make and exit with +status code 1. + +```env +API_KEY=abc123 +SECRET_KEY=def456 +``` + +By defining `MEX_ENVIRONMENT` variable you can provide additional files +and they will be loaded automatically. + +```make +MEX_ENVIRONMENT="local.env second.env" + +include makext.mk + +demo-envars: + @echo "Envrionment variables" + @echo " HOME: $(HOME)" + @echo " TERM: $(TERM)" + @echo " ENV: $(MEX_ENVIRONMENT)" + @echo " AUDIO_BUCKET: $(AUDIO_BUCKET)" + @echo " DB_HOST: $(DB_HOST)" +``` + +After that they can be used in your recipes like all the other variables +you have. They will however override variables the shell already has +defined. ## Acknowledgement diff --git a/local.env b/local.env new file mode 100644 index 0000000000000000000000000000000000000000..d02831b1c964b2388a60e9c865c8716c79b9fb47 --- /dev/null +++ b/local.env @@ -0,0 +1,14 @@ +# Database settings +DB_HOST=localhost +DB_PORT=5432 +DB_NAME=mydatabase +DB_USER=myuser +DB_PASSWORD=mypassword + +# API keys +API_KEY=abc123 +SECRET_KEY=def456 + +# Other settings +DEBUG_MODE=true +LOG_LEVEL=info" diff --git a/makext.mk b/makext.mk index 4c8937720831e99d346ea8f8b2bb17930a3b0eaa..1ade3fdc35856b14da67eeaf4810a4ff17ee0b76 100644 --- a/makext.mk +++ b/makext.mk @@ -15,6 +15,13 @@ ifeq ($(OS),Windows_NT) $(error makext does not support Windows operating system) endif +# Load environmental files from `MEX_ENVIRONMENT`. By default GNU make +# loads what is already in `env`. This extends it to other files. +ifdef MEX_ENVIRONMENT +TEMP_ENV_FILES=$(shell echo $(MEX_ENVIRONMENT) | tr ',' ' ') +$(foreach file,$(TEMP_ENV_FILES),$(eval include $(file))) +endif + # Help extension that lists all the targets with descriptions # and adds description and license information if data provided. .PHONY: .help diff --git a/second.env b/second.env new file mode 100644 index 0000000000000000000000000000000000000000..616e1463e9d1bc6801afed700fff30f32026b324 --- /dev/null +++ b/second.env @@ -0,0 +1,5 @@ +# Audio storage settings +AUDIO_BUCKET=my_audio_bucket +AUDIO_ACCESS_KEY=my_access_key +AUDIO_SECRET_KEY=my_secret_key +AUDIO_REGION=us-west-1