Demo repository exloring how to automatically provide custom bash functions/symbols to
-
make recipes
-
make shell function calls (i.e.
$(shell ...)
)
When developing larger Makefile's you will recognize
-
duplicated bash code within make your recipes
-
large bash recipes across many lines (possible thanks to make directive
.ONESHELL
) -
longer bash recipe blocks will look very confusing due to the
$
escaping issue
Would'nt it be nice to out source such bash code in separate shell script libraries ?
-
easier maintenance
Makefiles just consist of short recipes just calling your bash functions
Makefile
deploy: build github::release $(GIT_TAG) $(GITHUB_USER) $(GITHUB_TOKEN)
your Bash library
function github::release() { ... } export -f github::release
-
testing
Your bash scripts can be hardened by tests.
There exist a bunch of mature shell testing frameworks like https://github.com/kward/shunit2 or bats for testing bash code
-
reusable code
Your bash functions can now be reused in other slightly different projects.
I think the answer is a clear : Y.E.S. !
This repo contains some working examples implementing automatic configurable provisioning of custom bash libraries in Makefile's.
Depending on your needs you can choose wich variant works best for you.
-
./make-bash-init-script-simply-but-partly-broken/
Contains a very simplistic effort utilizing Bash's
--rcfile
option to inject your bash functions to the Makefile. It has a few caveats but may be okay for smaller Makefiles. -
./make-bash-library-injection-modular-take/ ist the GOLD solution without any caveats. This solution requires a bit more Make/Bash bootstrapping code but fit's every case
So if you don't know which one to use ... head over to the GOLD solution - it works in every case.
I use some of the ideas implemented here in a generic make framework called pnpmkambrium by myself.
pnpmkambrium goes far beyond the technique described here - it provides
-
a generic Meta Makefile maintaining monorepos for building/uploading
-
docker images
-
npm packages
-
wordpress plugins/themes
-
...
out of the box
-
-
customizable by
.env
and.secret
files.In most cases you don't need to write custom Makefile's - just provide
.env
files (for non-sensitive data) and.secrets
files (for sensitive data like credentials). -
You can even extend it with your own Bash functions and Make targets/rules.
You may also have a look at my Makefile recipes in various languages GitHub repo showing how to use almost any scripting language (instead of bash) to implement Makefile recipes
-
GNU make
-
Bash