baz/loader.sht
Ari Archer bd7148709b
6.1.2 : fix function loading
Signed-off-by: Ari Archer <ari.web.xyz@gmail.com>
2023-02-18 18:26:22 +02:00

159 lines
3.4 KiB
Bash

#!/usr/bin/env bash
# #{_} => preprocessor macro expansion
# before anything, im very sorry about these `local` things, bash
# is sometimes trash, this is one example of bash handling clearly
# local variables like globals
# all function names starting with _ will be mangled
export BAZ_LOADER_VERSION='#{BAZ_VER}'
#{BAZ_LOGGING_FN}
_baz_load_envs() {
#{BAZ_LOG} "loading environment variables from '$BAZP_NAME'"
local f="$1/environments" e
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "loading env variable '$f'"
# to avoid forking, im using read
#{BAZ_RD} e <"$f"
# could we optimise these read calls without subshells ? ref ^
# basically, can we turn this into a single call ?
eval "#{BAZ_RD} e<<#{BAZ_RDELIM}
$e
#{BAZ_RDELIM}"
export "${f##*/}"="$e"
done
}
_baz_load_commands() {
#{BAZ_LOG} "adding '$BAZP_NAME' commands to PATH"
local d="$1/commands"
[ -d "$d" ] || return
export PATH="$d:$PATH"
#{BAZ_LOG} "making '$BAZP_NAME' commands executable"
chmod -- 700 "$d"/*
}
_baz_load_functions() {
#{BAZ_LOG} "loading functions from '$BAZP_NAME'"
local f="$1/functions" c
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "loading function '$f'"
#{BAZ_RD} c <"$f"
eval "function ${f##*/}(){
$c
}"
done
}
_baz_load_aliases() {
#{BAZ_LOG} "loading aliases from '$BAZP_NAME'"
local f="$1/aliases" a
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "loading alias '$f'"
#{BAZ_RD} a <"$f"
# shellcheck disable=SC2139
alias "${f##*/}"="$a"
done
}
_baz_load_runners() {
#{BAZ_LOG} "running runners in '$BAZP_NAME'"
local f="$1/runners"
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "starting runner : '$f'"
. "$f"
done
}
_baz_load_completions() {
#{BAZ_LOG} "adding completions from '$BAZP_NAME'"
local f="$1/completions" c
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "adding completion : '$f'"
read -r c <"$f"
complete -F "$c" -o bashdefault -o default "${f##*/}"
done
}
_baz_load_keybinds() {
#{BAZ_LOG} "loading readline keybinds from '$BAZP_NAME'"
local f="$1/keybinds"
local k="$f/all.rl" f="$f/bindings"
if [ -f "$k" ]; then
#{BAZ_LOG} 'loading full-context keybinds'
bind -f "$k"
fi
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "loading keybinds for context : $f"
bind -m "${f##*/}" -f "$f"
done
}
baz_load_plugin_low() {
. "$1/baz.env"
local p="$1/$BAZP_SRC"
# having everything inline helps with performance,
# although reduces maintainablity
# we could also inline the function calls, but i think its
# too unmaintainable at that point
_baz_load_envs "$p"
_baz_load_commands "$p"
_baz_load_functions "$p"
_baz_load_aliases "$p"
_baz_load_runners "$p"
_baz_load_completions "$p"
_baz_load_keybinds "$p"
}
# mid level all plugins loader
baz_loader() {
local p=('#{BAZ_PLUG_DIR}'/*)
#{BAZ_LOG} 'checking if #{BAZ_PLUG_DIR} exists and isnt empty'
[ -d "${p[0]}" ] || return 1
#{BAZ_LOG} 'loading baz from data dir : #{BAZ_DATA_DIR}'
for p in "${p[@]}"; do
if [ ! -e "$p/disabled" ] && [ -f "$p/baz.env" ]; then
#{BAZ_LOG}
baz_load_plugin_low "$p"
#{BAZ_LOG}
fi
done
#{BAZ_NOP}
}
[ "$BAZ_LOADER_ENABLED" ] && baz_loader