Created
November 14, 2025 08:39
-
-
Save jul/91c423269d9af84565416991a10cb31c to your computer and use it in GitHub Desktop.
a template for a bash script à la make
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| declare -a action; | |
| RD='\e[31m' | |
| GB='\e[33m' | |
| BL='\e[34m' | |
| RZ='\e[0m' | |
| DEBUG=${DEBUG:-1} | |
| ONE_VAR=${ONE_VAR:-template.dot} | |
| echo -e "${GB}REPRODUCIBLE BUILD WITH" | |
| echo -e "-----------------------$RZ" | |
| echo | |
| for var in DEBUG ONE_VAR; | |
| do | |
| printf "%s='%s' " "$var" "$( eval eval echo \\$\$var)" | |
| done | |
| echo -n $0 $@ | |
| echo | |
| echo | |
| d() { | |
| [ -z "$DEBUG" ] || echo -e "DEBUG:$(date +"%H:%M:%S"):$BL $* $RZ" | |
| } | |
| [ -d out ] || mkdir out | |
| push() { | |
| local stack | |
| declare -a stack | |
| stack=( "$@" ) | |
| for ((i=${#@}; i--; i)); do | |
| action=( "${stack[$i]}" "${action[@]}" ); | |
| done | |
| } | |
| pop() { | |
| declare -n v=$1 | |
| v=${action[0]} | |
| action=("${action[@]:1}") | |
| } | |
| set +x | |
| dispatch_action() { | |
| while [[ ${#action} -gt 0 ]]; do | |
| pop act | |
| d "DISPATCHING $act" | |
| case $act in | |
| all) | |
| [ -f out/this ] || push "before" ; dispatch_action | |
| touch out/that | |
| d all bells and whistles | |
| push "clean" ; dispatch_action | |
| ;; | |
| before) | |
| [ -f out/init ] || push init; dispatch_action | |
| d after init before all and clean | |
| touch out/this | |
| ;; | |
| init) | |
| d init : begin all | |
| touch out/init | |
| ;; | |
| clean) | |
| d clean everything | |
| set +e | |
| d out/* | |
| rm out/* | |
| set -e | |
| ;; | |
| *) | |
| echo unrecognized "$act"; | |
| d "${action[@]}" | |
| break | |
| ;; | |
| esac | |
| done | |
| } | |
| if [ ! -z $1 ]; then | |
| action=( "$@" ); | |
| else | |
| push "all" | |
| fi | |
| dispatch_action | |
| d fin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment