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

Commit

Permalink
2.5.0: Improve loader speed and security, add high level manual plugi…
Browse files Browse the repository at this point in the history
…n loading

Signed-off-by: Ari Archer <[email protected]>
  • Loading branch information
Ari Archer committed Oct 16, 2022
1 parent c5643d4 commit b7bc99a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 49 deletions.
81 changes: 53 additions & 28 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.4.0'
export BAZ_VERSION='2.5.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 @@ -434,45 +434,70 @@ baz_help() {

{
log 'Subcommands:'
entry 'help' 'Print help'

entry 'setup' 'Setup baz plugin manager'
log "Make sure you have $(get_base "$BAZ_LOADER_TEMPLATE") file in CWD" | indent
{
entry 'help' 'Print help'

entry 'unsetup' 'Unsetup (remove) baz plugin manager'
entry 'setup' 'Setup baz plugin manager'
log "Make sure you have $(get_base "$BAZ_LOADER_TEMPLATE") file in CWD" | indent

entry 'install [method] [atoms...]' 'Install plugins'
{
entry 'local' 'Local plugin from a directory'
log 'Atom type: path to plugin, e.g. /tmp/myplug' | indent
entry 'unsetup' 'Unsetup (remove) baz plugin manager'

entry 'git' 'Git plugin from git'
log 'Atom type: <repo url>, e.g. https://ari-web.xyz/gh/baz-example-plugin' | indent
} | indent
entry 'install [method] [atoms...]' 'Install plugins'
{
entry 'local' 'Local plugin from a directory'
log 'Atom type: path to plugin, e.g. /tmp/myplug' | indent

entry 'uninstall [plugins...]' 'Uninstall plugins'
entry 'list' 'List all installed plugins'
entry 'update <plugins...>' 'Update all installed or specified plugins'
entry 'git' 'Git plugin from git'
log 'Atom type: <repo url>, e.g. https://ari-web.xyz/gh/baz-example-plugin' | indent
} | indent

entry 'info [exist|local|git] [atoms...]' 'Describe (get info) about plugins'
{
entry 'exist' 'Describe existing (installed) plugin'
log 'Atom type: plugin name, e.g. myplug' | indent
entry 'uninstall [plugins...]' 'Uninstall plugins'
entry 'list' 'List all installed plugins'
entry 'update <plugins...>' 'Update all installed or specified plugins'

entry 'info [exist|local|git] [atoms...]' 'Describe (get info) about plugins'
{
entry 'exist' 'Describe existing (installed) plugin'
log 'Atom type: plugin name, e.g. myplug' | indent

entry 'local' 'Describe local plugin'
log 'Atom type: path to plugin, e.g. /tmp/myplug' | indent

entry 'git' 'Describe plugin from a git source'
log 'Atom type: <repo url>, e.g. https://ari-web.xyz/gh/baz-example-plugin' | indent
} | indent

entry 'local' 'Describe local plugin'
log 'Atom type: path to plugin, e.g. /tmp/myplug' | indent
entry 'upgrade' 'Upgrade baz'
log "Make sure you're in the baz git repo's directory" | indent

entry 'git' 'Describe plugin from a git source'
log 'Atom type: <repo url>, e.g. https://ari-web.xyz/gh/baz-example-plugin' | indent
entry 'version' 'Print version and exit'

entry 'disable [plugins...]' 'Disable plugins'
entry 'enable [plugins...]' 'Enable plugins'
} | indent

entry 'upgrade' 'Upgrade baz'
log "Make sure you're in the baz git repo's directory" | indent
echo

log 'Functions:'

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

{
entry 1 'No plugin specifed'
entry 2 'Invalid/non-installed plugin'
} | indent
} | indent

entry 'version' 'Print version and exit'
entry 'baz_load_plugins [plugins...]' 'Load specified plugins'
log 'Returns the ammount of plugins failed' | indent

entry 'disable [plugins...]' 'Disable plugins'
entry 'enable [plugins...]' 'Enable plugins'
entry 'baz_loader' 'Load all plugins'
log 'Always should rerturn 0/nothing' | indent
} | indent
} | indent >&2
}

Expand Down
84 changes: 63 additions & 21 deletions loader.sht
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ __expand_var() {
# because I'm trying to reduce the ammount
# of colisions

local _______baz_expand_var_delimiter_______='_______#{RHASH}_baz_expand_var_#{RHASH}_______'

local _______baz_expand_var_cmd_______
_______baz_expand_var_cmd_______="cat <<$_______baz_expand_var_delimiter_______"$'\n'"$(<"$1")"$'\n'"$_______baz_expand_var_delimiter_______"

eval "$_______baz_expand_var_cmd_______"
eval "cat <<________UNSAFE_#{RHASH}_baz_expand_var_#{RHASH}_UNSAFE________"$'\n'"$(<"$1")"$'\n'"________UNSAFE_#{RHASH}_baz_expand_var_#{RHASH}_UNSAFE________"
}

__baz_load_envs() {
Expand Down Expand Up @@ -129,7 +124,10 @@ __baz_load_completions() {
[ -f "$_baz_completion" ] || continue
__baz_vecho "Adding completion: $([ "$BAZ_DEBUG_LOAD" ] && __baz_get_base "$_baz_runner")"

complete -F "$(head -n 1 -- "$_baz_completion")" \
local _baz_completions_first_line
read -r _baz_completions_first_line <"$_baz_completion"

complete -F "$_baz_completions_first_line" \
-o bashdefault -o default "$(__baz_get_base "$_baz_completion")"
done
}
Expand Down Expand Up @@ -157,7 +155,59 @@ __baz_load_keybinds() {
done
}

__baz_loader() {
# Low level plugin loading
__baz_load_plugin() {
. "$1/baz.env"

local stage
for stage in "${__baz_stages[@]}"; do
__baz_vecho "Loading stage: $stage($BAZP_NAME)"
"$stage"
done
}

# High level plugin loading
baz_load_plugin() {
[ ! "$1" ] && echo ' * No plugin specified' 2>&1 && return 1

local baz_plugin="#{BAZ_PLUG_DIR}/$1"

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

local start
start="$(date +%s%N)"

__baz_load_plugin "$baz_plugin"

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

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

local plugin ret=0

for plugin in "$@"; do
if ! baz_load_plugin "$plugin"; then
ret="$((ret + 1))"
fi
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
Expand All @@ -168,8 +218,6 @@ __baz_loader() {
__baz_load_keybinds
)

export BAZ_LOADER_VERSION='#{BAZ_VER}'

if [ -z "$(ls -A -- '#{BAZ_PLUG_DIR}' 2>/dev/null)" ] || [ ! -d '#{BAZ_PLUG_DIR}' ]; then
__baz_vecho 'No plugin dir found'
return 1
Expand All @@ -179,20 +227,14 @@ __baz_loader() {

local plugin
for plugin in '#{BAZ_PLUG_DIR}'/*; do
[ -e "$plugin/disabled" ] && continue
if [ -e "$plugin/disabled" ] || [ ! -f "$plugin/baz.env" ]; then
continue
fi

__baz_vecho

. "$plugin/baz.env"

local stage
for stage in "${__baz_stages[@]}"; do
__baz_vecho "Loading stage: $stage($BAZP_NAME)"
"$stage"
done

__baz_load_plugin "$plugin"
__baz_vecho
done
}

[ "$BAZ_LOADER_ENABLED" ] && __baz_loader
[ "$BAZ_LOADER_ENABLED" ] && baz_loader

0 comments on commit b7bc99a

Please sign in to comment.