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

Commit

Permalink
5.3.0: Improbe loader performance, fix git.sh script
Browse files Browse the repository at this point in the history
Signed-off-by: Ari Archer <[email protected]>
  • Loading branch information
Ari Archer committed Nov 17, 2022
1 parent 5bf30f8 commit c7d5d40
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
8 changes: 4 additions & 4 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='5.2.0'
export BAZ_VERSION='5.3.0'
export BAZ_DIR="$HOME/.local/share/baz"
export BAZ_CONFDIR="$HOME/.config/baz"
export BAZ_CONF="$BAZ_CONFDIR/config.env"
Expand Down Expand Up @@ -351,20 +351,20 @@ baz_loader() {
-e "$(baz_loader_q 'BAZ_PLUG_DIR' "$BAZ_PLUGDIR")" \
-e "$(baz_loader_q 'BAZ_RDELIM' "_____!UNSAFE!_$RANDOM$(head -n 10 /dev/urandom | sha1sum | awk '{print $1}' | head -c 19)$((RANDOM * 2))_!DONT.USE.ME!_____")" \
-e "$(baz_loader_q 'BAZ_VER' "$BAZ_VERSION")" \
-e "$(baz_loader_q 'BAZ_MFD' 'mapfile -d ""')" \
-e "$(baz_loader_q 'BAZ_LOADER_FN' "$(baz_loader_logging_fn)")" \
-e "$(baz_loader_q 'BAZ_LOG' "$(baz_loader_baz_log)")" \
-e "$(baz_loader_q 'BAZ_NOP' "$([ "$BAZ_ENSURE_OK" ] && echo ':')")" \
-e "$(baz_loader_q 'BAZ_RD' "read -rd ''")" \
-e '/^\s*#/d; /^\s*$/d; s/^\s*//g; s/_baz_vecho/._l/g' \
-e 's/ </</g; s/ >/>/g; s/ \&\& /\&\&/g; s/ || /||/g; s/; /;/g' \
"$BAZ_LOADER_TEMPLATE")"

printf '%s' "$loader" | sed -e "$(echo "$loader" | grep '^_.*\(\) {$' |
awk '{print $1}' |
while read -r func_name; do
echo "s/$(head -c -3 <<<"${func_name}")/._$idx/g;"
echo "s/$(head -c -3 <<<"${func_name}")/.$idx/g;"
idx="$((idx + 1))"
done)"
done)" -e 's/() {/(){/g'
}
# Installer helpers
Expand Down
33 changes: 18 additions & 15 deletions loader.sht
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# #{_} => Preprocessor directives
# #{_} => Preprocessor macro expansion

# Before anything, I'm very sorry
# about these `local` things, bash
Expand All @@ -16,6 +16,7 @@ export BAZ_LOADER_VERSION='#{BAZ_VER}' BAZ_NL=$'\n'

#{BAZ_LOADER_FN}

# We need to optimise this function more or even out
_baz_get_base() {
local -n _r="$1"
shift 1
Expand All @@ -37,13 +38,14 @@ _baz_load_envs() {

#{BAZ_LOG} "Loading env variable '$_env_name'"

# To avoid forking, I'm using mapfile
#{BAZ_MFD} _env <"$_baz_env"
# To avoid forking, I'm using read
#{BAZ_RD} _env <"$_baz_env"

# Could we optimise these mapfile calls without subshells? Ref ^
eval '#{BAZ_MFD} _env<<#{BAZ_RDELIM}'"$BAZ_NL""${_env[*]}"'#{BAZ_RDELIM}'
# Could we optimise these read calls without subshells? Ref ^
# Basically, can we turn this into a single call ?
eval "#{BAZ_RD} _env<<#{BAZ_RDELIM}$BAZ_NL$_env$BAZ_NL"'#{BAZ_RDELIM}'

export "$_env_name"="${_env[*]%"$BAZ_NL"}"
export "$_env_name"="$_env"
done
}

Expand Down Expand Up @@ -79,10 +81,10 @@ _baz_load_functions() {

#{BAZ_LOG} "Loading function '$_func_name'"

#{BAZ_MFD} _func <"$_baz_func"
#{BAZ_RD} _func <"$_baz_func"

eval "function $_func_name(){
${_func[*]}
$_func
}"
done
}
Expand All @@ -101,10 +103,10 @@ _baz_load_aliases() {

#{BAZ_LOG} "Loading alias '$_alias_name'"

mapfile -t _alias <"$_baz_alias"
#{BAZ_RD} _alias <"$_baz_alias"

# shellcheck disable=SC2139
alias "$_alias_name"="${_alias[*]}"
alias "$_alias_name"="$_alias"
done
}

Expand All @@ -117,7 +119,7 @@ _baz_load_runners() {
for _baz_runner in "$_runners_dir"/*; do
[ -f "$_baz_runner" ] || continue
#{BAZ_LOG} "Starting runner: '$_baz_runner'"
source "$_baz_runner"
. "$_baz_runner"
done
}

Expand All @@ -129,11 +131,12 @@ _baz_load_completions() {

for _baz_completion in "$_completions_dir"/*; do
[ -f "$_baz_completion" ] || continue
#{BAZ_LOG} "Adding completion: '$_baz_runner'"

#{BAZ_LOG} "Adding completion: '$_baz_completion'"

read -r _baz_completions_first_line <"$_baz_completion"
_baz_get_base _baz_completion "$_baz_completion"

_baz_get_base _baz_completion
complete -F "$_baz_completions_first_line" \
-o bashdefault -o default "$_baz_completion"
done
Expand Down Expand Up @@ -213,7 +216,7 @@ baz_load_plugin() {

_baz_load_plugin "$baz_plugin"

baz_log "Loading '$baz_plugin_name' took: ~$(($(("$(date +%s%N)" - start)) / 1000000)) ms" >&2
baz_log "Loading '$baz_plugin_name' took $(($(("$(date +%s%N)" - start)) / 1000000)) ms" >&2
}

# High level multiple plugins loading
Expand All @@ -240,7 +243,7 @@ baz_load_all() {
baz_load_plugins '#{BAZ_PLUG_DIR}'/*
ret="$?"

baz_log "Loading all plugins took: ~$(($(("$(date +%s%N)" - start)) / 1000000)) ms" >&2
baz_log "Loading all plugins took ~$(($(("$(date +%s%N)" - start)) / 1000000)) ms (this is quite inaccurate)" >&2

return "$ret"
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
main() {
echo 'Did you change the version??'
printf 'Press enter to mean yes'
read -r
read -r _

echo 'Okay.. I will sleep for couple of seconds just in case'
sleep 4s
Expand Down

0 comments on commit c7d5d40

Please sign in to comment.