Skip to content

Instantly share code, notes, and snippets.

@udovichenko
Last active December 11, 2022 00:47
Show Gist options
  • Save udovichenko/fc572e3ff97b5808133eafb7b63c15ea to your computer and use it in GitHub Desktop.
Save udovichenko/fc572e3ff97b5808133eafb7b63c15ea to your computer and use it in GitHub Desktop.
Task runner (Makefile Replacement). Pure bash (shell) functions based tasks. Autocomplete (completion) for both bash and zsh
_task() {
TASKS=$(./tasks.sh ls-func)
local -a LIST=()
while IFS='' read -r line; do LIST+=("$line"); done < <(compgen -W "$TASKS")
}
complete -F _task task
_task() {
TASKS=$(./tasks.sh ls-func)
local -a LIST=()
while IFS='' read -r line; do LIST+=("$line"); done < <(compgen -W "$TASKS")
compadd -a LIST
}
compdef _task task -default-
#!/bin/bash
# Makefile Replacement with Pure Bash Function
# Strict mode: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
task1() {
echo "task1"
}
task2() {
task1
echo "task2"
}
task3() {
task1
task2
echo "task3"
}
get-func-list() {
env -i bash --noprofile --norc -c '
source "'"$1"'"
typeset -f |
grep '\''^[^{} ].* () $'\'' |
awk "{print \$1}" |
while read -r fcn_name; do
type "$fcn_name" | head -n 1 | grep -q "is a function$" || continue
echo "$fcn_name"
done
'
}
ls-func() {
FUNC_LIST=""
for fcn_name in $(get-func-list tasks.sh); do
FUNC_LIST="$FUNC_LIST $fcn_name"
done
echo "$FUNC_LIST"
}
# Tries to run the parameter as a function: https://stackoverflow.com/a/16159057/3203441
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment