summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2024-05-12 12:35:16 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2024-05-12 12:35:16 +0200
commit712e5df429b7a93ecee52d9f7e7d76594122a516 (patch)
treef87e798818997723dcde1aee8170ba478d01ff6a
parentd76533abb2a92a678cdf3236f1f8b94792e0b0c5 (diff)
downloadmakext-712e5df429b7a93ecee52d9f7e7d76594122a516.tar.gz
Added environmental variables from file loader extension
-rw-r--r--Makefile9
-rw-r--r--README.md46
-rw-r--r--local.env14
-rw-r--r--makext.mk7
-rw-r--r--second.env5
5 files changed, 76 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 12629ca..60a5f94 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
@@ -9,6 +10,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 ff88014..9471157 100644
--- a/README.md
+++ b/README.md
@@ -11,10 +11,11 @@ tasks.
> 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 @@ more information.
## 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.
@@ -164,6 +165,41 @@ build-app: .assure
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
- https://stackoverflow.com/a/59087509
diff --git a/local.env b/local.env
new file mode 100644
index 0000000..d02831b
--- /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 4c89377..1ade3fd 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 0000000..616e146
--- /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