From 42cf2daa121ebf39954ff7d655b253b06fe7346c Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Wed, 3 Feb 2021 13:29:22 -0600 Subject: [PATCH] Avoid loops when loading libraries The library `preexec` requires complete control over the DEBUG trap and PROMPT_COMMAND, but it was being loaded in the library loop. When evaluating options, I came to the conclusion that we don't have enough libraries to justify this premature optimization, so I've flattened as much of the sourcing as possible and added comments to explain their purpose, hoping that future maintainers will use these to guide their decisions when adding more libraries or changing the load order. --- bash_it.sh | 125 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 51 deletions(-) diff --git a/bash_it.sh b/bash_it.sh index cb504202..53cd8b24 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -1,9 +1,7 @@ #!/usr/bin/env bash -# Initialize Bash It -BASH_IT_LOG_PREFIX="core: main: " # Only set $BASH_IT if it's not already set -if [ -z "$BASH_IT" ]; then +if [[ -z "$BASH_IT" ]]; then # Setting $BASH to maintain backwards compatibility export BASH_IT=$BASH BASH="$(bash -c 'echo $BASH')" @@ -11,107 +9,121 @@ if [ -z "$BASH_IT" ]; then BASH_IT_OLD_BASH_SETUP=true fi -# Load composure first, so we support function metadata +# Load composure first, to support function metadata, then create our custom attributes. # shellcheck disable=SC1090 source "${BASH_IT}"/vendor/github.com/erichs/composure/composure.sh +cite _about _param _example _group _author _version +cite about-alias about-completion about-plugin -# We need to load logging module first as well in order to be able to log +# Next, load our logging library so we can give useful feedback to the user # shellcheck source=./lib/log.bash source "${BASH_IT}/lib/log.bash" -# We can only log it now -[ -z "$BASH_IT_OLD_BASH_SETUP" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!" +BASH_IT_LOG_PREFIX='core: main: ' + +# Check for old installations +if [[ -n "$BASH_IT_OLD_BASH_SETUP" ]]; then + _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!" +fi # For backwards compatibility, look in old BASH_THEME location -if [ -z "$BASH_IT_THEME" ]; then +if [[ -z "$BASH_IT_THEME" ]]; then _log_warning "BASH_IT_THEME variable not initialized, please upgrade your bash-it version and reinstall it!" export BASH_IT_THEME="$BASH_THEME" unset BASH_THEME fi -# support 'plumbing' metadata -cite _about _param _example _group _author _version -cite about-alias about-plugin about-completion +# Load the base libraries ... +# Note: Ordering is important and intentional -# libraries, but skip appearance (themes) for now -_log_debug "Loading libraries(except appearance)..." -LIB="${BASH_IT}/lib/*.bash" -APPEARANCE_LIB="${BASH_IT}/lib/appearance.bash" -for _bash_it_config_file in $LIB; do - if [ "$_bash_it_config_file" != "$APPEARANCE_LIB" ]; then - filename=${_bash_it_config_file##*/} - filename=${filename%.bash} - BASH_IT_LOG_PREFIX="lib: ${filename}: " - _log_debug "Loading library file..." - # shellcheck disable=SC1090 - source "$_bash_it_config_file" - fi -done +# "utilities" - internal functions meant for use within the bash-it codebase +BASH_IT_LOG_PREFIX='lib: utilities: ' +_log_debug 'Loading library file ...' +# shellcheck source=./lib/utilities.bash +source "${BASH_IT}/lib/utilities.bash" -# Load vendors -BASH_IT_LOG_PREFIX="vendor: " -for _bash_it_vendor_init in "${BASH_IT}"/vendor/init.d/*.bash; do - _log_debug "Loading \"$(basename "${_bash_it_vendor_init}" .bash)\"..." - # shellcheck disable=SC1090 - source "${_bash_it_vendor_init}" -done -unset _bash_it_vendor_init +# "helpers" - generic functions meant for generic use by end users, +# often wrappers of functions found in utilities +BASH_IT_LOG_PREFIX='lib: helpers: ' +_log_debug 'Loading library file ...' +# shellcheck source=./lib/helpers.bash +source "${BASH_IT}/lib/helpers.bash" + +# "search" - allows end users to search for matching aliases, plugins and completions in bash-it +BASH_IT_LOG_PREFIX='lib: search: ' +_log_debug 'Loading library file ...' +# shellcheck source=./lib/search.bash +source "${BASH_IT}/lib/search.bash" + +# Load aliases, completion, plugins ... +BASH_IT_LOG_PREFIX='core: main: ' -BASH_IT_LOG_PREFIX="core: main: " # Load the global "enabled" directory # "family" param is empty so that files get sources in glob order # shellcheck source=./scripts/reloader.bash source "${BASH_IT}/scripts/reloader.bash" # Load enabled aliases, completion, plugins -for file_type in "aliases" "plugins" "completion"; do +for file_type in 'aliases' 'plugins' 'completion'; do # shellcheck source=./scripts/reloader.bash - source "${BASH_IT}/scripts/reloader.bash" "skip" "$file_type" + source "${BASH_IT}/scripts/reloader.bash" 'skip' "$file_type" done # Load theme, if a theme was set if [[ -n "${BASH_IT_THEME}" ]]; then _log_debug "Loading \"${BASH_IT_THEME}\" theme..." + # Load colors and helpers first so they can be used in base theme - BASH_IT_LOG_PREFIX="themes: colors: " + BASH_IT_LOG_PREFIX='themes: colors: ' + _log_debug 'Loading theme file ...' # shellcheck source=./themes/colors.theme.bash source "${BASH_IT}/themes/colors.theme.bash" - BASH_IT_LOG_PREFIX="themes: githelpers: " + + BASH_IT_LOG_PREFIX='themes: githelpers: ' + _log_debug 'Loading theme file ...' # shellcheck source=./themes/githelpers.theme.bash source "${BASH_IT}/themes/githelpers.theme.bash" - BASH_IT_LOG_PREFIX="themes: p4helpers: " + + BASH_IT_LOG_PREFIX='themes: p4helpers: ' + _log_debug 'Loading theme file ...' # shellcheck source=./themes/p4helpers.theme.bash source "${BASH_IT}/themes/p4helpers.theme.bash" - BASH_IT_LOG_PREFIX="themes: command_duration: " + + BASH_IT_LOG_PREFIX='themes: command_duration: ' + _log_debug 'Loading theme file ...' # shellcheck source=./themes/command_duration.theme.bash source "${BASH_IT}/themes/command_duration.theme.bash" - BASH_IT_LOG_PREFIX="themes: base: " + + BASH_IT_LOG_PREFIX='themes: base: ' + _log_debug 'Loading theme file ...' # shellcheck source=./themes/base.theme.bash source "${BASH_IT}/themes/base.theme.bash" - BASH_IT_LOG_PREFIX="lib: appearance: " + BASH_IT_LOG_PREFIX='lib: appearance: ' + _log_debug 'Loading library file ...' # appearance (themes) now, after all dependencies # shellcheck source=./lib/appearance.bash - source "$APPEARANCE_LIB" + source "${BASH_IT}/lib/appearance.bash" fi -BASH_IT_LOG_PREFIX="core: main: " -_log_debug "Loading custom aliases, completion, plugins..." -for file_type in "aliases" "completion" "plugins"; do - if [ -e "${BASH_IT}/${file_type}/custom.${file_type}.bash" ]; then +# Load custom components ... + +BASH_IT_LOG_PREFIX='core: main: ' +_log_debug 'Loading custom aliases, completion, plugins...' +for file_type in 'aliases' 'completion' 'plugins'; do + if [[ -e "${BASH_IT}/${file_type}/custom.${file_type}.bash" ]]; then BASH_IT_LOG_PREFIX="${file_type}: custom: " - _log_debug "Loading component..." + _log_debug 'Loading component...' # shellcheck disable=SC1090 source "${BASH_IT}/${file_type}/custom.${file_type}.bash" fi done -# Custom BASH_IT_LOG_PREFIX="core: main: " _log_debug "Loading general custom files..." CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/**/*.bash" for _bash_it_config_file in $CUSTOM; do - if [ -e "${_bash_it_config_file}" ]; then + if [[ -e "${_bash_it_config_file}" ]]; then filename=$(basename "${_bash_it_config_file}") filename=${filename%*.bash} BASH_IT_LOG_PREFIX="custom: $filename: " @@ -137,7 +149,6 @@ elif [ -s /Applications/Preview.app ]; then fi # Load all the Jekyll stuff - if [ -e "$HOME/.jekyllconfig" ]; then # shellcheck disable=SC1090 . "$HOME/.jekyllconfig" @@ -155,5 +166,17 @@ if ! command -v reload &> /dev/null && [ -n "$BASH_IT_RELOAD_LEGACY" ]; then esac fi +# Now that everything is loaded and configured, load the previewer +BASH_IT_LOG_PREFIX='lib: preview: ' +# and the associated preview script +# shellcheck source=./lib/preview.bash +source "${BASH_IT}/lib/preview.bash" + # Disable trap DEBUG on subshells - https://github.com/Bash-it/bash-it/pull/1040 set +T + +# Finally, load preexec directly from vendor. +# This has to be last because it wants to have full control over DEBUG trap and PROMPT_COMMAND +BASH_IT_LOG_PREFIX='vendor: bash-preexec: ' +# shellcheck source=./vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh +source "${BASH_IT}/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh"