Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
2.5.1: Make the basename follow leading / and add baz_load_all
Browse files Browse the repository at this point in the history
Signed-off-by: Ari Archer <[email protected]>
  • Loading branch information
Ari Archer committed Oct 16, 2022
1 parent b7bc99a commit 9c6ee53
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.ccls-cache/
25 changes: 16 additions & 9 deletions baz
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[ "$BAZ_DEBUG" ] && set -x

export BAZ_VERSION='2.5.0'
export BAZ_VERSION='2.5.1'
export BAZ_DIR="$HOME/.local/share/baz"
export BAZ_CONFDIR="$HOME/.config/baz"
export BAZ_CONF="$BAZ_CONFDIR/config.env"
Expand Down Expand Up @@ -30,7 +30,11 @@ log() { [ "$BAZP_NO_LOG" = true ] || ${2:-echo} " * $1"; }
eecho() { [ "$BAZP_NO_LOG" = true ] || echo "$1" >&2; }
unexpand_path() { echo "${1/"$HOME"/"${2:-~}"}"; }
baz_git_clone() { git clone --quiet --recursive -- "$1" "$2"; }
get_base() { echo "${1##*/}"; }

get_base() {
set -- "${1%"${1##*[!/]}"}"
echo "${1##*/}"
}

baz_hook() {
local hook="${BAZP_SRC:?}/hooks/$1"
Expand All @@ -54,10 +58,10 @@ baz_run_hook() {
return 0
}

baz_sanitize() {
__baz_sanitize() {
read -r x
local tmp="${x//[[:space:]\"\'~!#\\$%^&*\/()=]/}"
printf '%s' "${tmp//../}"
echo "${tmp//../}"
}

in_array() {
Expand Down Expand Up @@ -482,7 +486,7 @@ baz_help() {
log 'Functions:'

{
entry 'baz_load_plugin [plugin]' 'Load a specified plugin'
entry 'baz_load_plugin [plugin_or_dir]' 'Load a specified plugin'
{
log 'Return values'

Expand All @@ -492,11 +496,14 @@ baz_help() {
} | indent
} | indent

entry 'baz_load_plugins [plugins...]' 'Load specified plugins'
log 'Returns the ammount of plugins failed' | indent
entry 'baz_load_plugins [plugins_or_dirs...]' 'Load specified plugins'
log 'Returns the ammount of plugins which failed to be loaded' | indent

entry 'baz_load_all' 'Load all installed plugins (high level)'
log 'Returns the ammount of plugins which failed to be loaded' | indent

entry 'baz_loader' 'Load all plugins'
log 'Always should rerturn 0/nothing' | indent
entry 'baz_loader' 'Load all installed plugins (lower level)'
log 'Always should rerturn 0 or nothing' | indent
} | indent
} | indent >&2
}
Expand Down
66 changes: 41 additions & 25 deletions loader.sht
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,34 @@
# example of bash handling clearly
# local variables like globals

export __BAZ_STAGES=(
__baz_load_envs
__baz_load_commands
__baz_load_functions
__baz_load_aliases
__baz_load_runners
__baz_load_completions
__baz_load_keybinds
)

export BAZ_LOADER_VERSION='#{BAZ_VER}'

if [ "$BAZ_DEBUG_LOAD" ]; then
__baz_vecho() { echo "[BAZ_LOAD] $1" >&2; }
__baz_vecho "Generated by baz version '#{BAZ_VER}'"
__baz_vecho "Generated by baz version '$BAZ_LOADER_VERSION'"
else
__baz_vecho() { :; }
fi

__baz_get_base() { echo "${1##*/}"; }
__baz_get_base() {
set -- "${1%"${1##*[!/]}"}"
echo "${1##*/}"
}

__baz_sanitize() {
read -r x
local tmp="${x//[[:space:]\"\'~!#\\$%^&*\/()=]/}"
printf '%s' "${tmp//../}"
echo "${tmp//../}"
}

__expand_var() {
Expand Down Expand Up @@ -160,7 +176,7 @@ __baz_load_plugin() {
. "$1/baz.env"

local stage
for stage in "${__baz_stages[@]}"; do
for stage in "${__BAZ_STAGES[@]}"; do
__baz_vecho "Loading stage: $stage($BAZP_NAME)"
"$stage"
done
Expand All @@ -170,10 +186,12 @@ __baz_load_plugin() {
baz_load_plugin() {
[ ! "$1" ] && echo ' * No plugin specified' 2>&1 && return 1

local baz_plugin="#{BAZ_PLUG_DIR}/$1"
local baz_plugin="$1" baz_plugin_name
[ -d "$1" ] || baz_plugin="#{BAZ_PLUG_DIR}/$1"
baz_plugin_name="$(__baz_get_base "$baz_plugin")"

if [ ! -f "$baz_plugin/baz.env" ]; then
echo " * Plugin '$1' is invalid or is not installed" >&2
echo " * Plugin '$baz_plugin_name' is invalid or is not installed" >&2
return 2
fi

Expand All @@ -182,42 +200,40 @@ baz_load_plugin() {

__baz_load_plugin "$baz_plugin"

local milis="$(($(("$(date +%s%N)" - start)) / 1000000))"
echo " * Loading '$1' took: ~$milis ms (~$((milis / 1000)) s)" >&2
echo " * Loading '$baz_plugin_name' took: ~$(($(("$(date +%s%N)" - start)) / 1000000)) ms" >&2
}

# High level multiple plugins loading
baz_load_plugins() {
if [ "$#" -le 0 ]; then
echo ' * No plugins to load specified'
echo ' * No plugins to load specified' >&2
return 1
fi

local plugin ret=0

for plugin in "$@"; do
if ! baz_load_plugin "$plugin"; then
ret="$((ret + 1))"
fi
baz_load_plugin "$plugin" || ret="$((ret + 1))"
done

return "$ret"
}

# High level all plugins loader
baz_loader() {
export BAZ_LOADER_VERSION='#{BAZ_VER}'

local __baz_stages=(
__baz_load_envs
__baz_load_commands
__baz_load_functions
__baz_load_aliases
__baz_load_runners
__baz_load_completions
__baz_load_keybinds
)
# High level multiple all plugins loading
baz_load_all() {
local start
start="$(date +%s%N)"

baz_load_plugins '#{BAZ_PLUG_DIR}'/*
local ret="$?"

echo " * Loading all plugins took: ~$(($(("$(date +%s%N)" - start)) / 1000000)) ms" >&2

return "$ret"
}

# Mid level all plugins loader
baz_loader() {
if [ -z "$(ls -A -- '#{BAZ_PLUG_DIR}' 2>/dev/null)" ] || [ ! -d '#{BAZ_PLUG_DIR}' ]; then
__baz_vecho 'No plugin dir found'
return 1
Expand Down

0 comments on commit 9c6ee53

Please sign in to comment.