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.
pull/1826/head
cornfeedhobo 2021-02-03 13:29:22 -06:00
parent ff2f961ad7
commit 42cf2daa12
No known key found for this signature in database
GPG Key ID: 724357093F994B26
1 changed files with 74 additions and 51 deletions

View File

@ -1,9 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Initialize Bash It
BASH_IT_LOG_PREFIX="core: main: "
# Only set $BASH_IT if it's not already set # 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 # Setting $BASH to maintain backwards compatibility
export BASH_IT=$BASH export BASH_IT=$BASH
BASH="$(bash -c 'echo $BASH')" BASH="$(bash -c 'echo $BASH')"
@ -11,107 +9,121 @@ if [ -z "$BASH_IT" ]; then
BASH_IT_OLD_BASH_SETUP=true BASH_IT_OLD_BASH_SETUP=true
fi fi
# Load composure first, so we support function metadata # Load composure first, to support function metadata, then create our custom attributes.
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${BASH_IT}"/vendor/github.com/erichs/composure/composure.sh 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 # shellcheck source=./lib/log.bash
source "${BASH_IT}/lib/log.bash" source "${BASH_IT}/lib/log.bash"
# We can only log it now BASH_IT_LOG_PREFIX='core: main: '
[ -z "$BASH_IT_OLD_BASH_SETUP" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!"
# 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 # 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!" _log_warning "BASH_IT_THEME variable not initialized, please upgrade your bash-it version and reinstall it!"
export BASH_IT_THEME="$BASH_THEME" export BASH_IT_THEME="$BASH_THEME"
unset BASH_THEME unset BASH_THEME
fi fi
# support 'plumbing' metadata # Load the base libraries ...
cite _about _param _example _group _author _version # Note: Ordering is important and intentional
cite about-alias about-plugin about-completion
# libraries, but skip appearance (themes) for now # "utilities" - internal functions meant for use within the bash-it codebase
_log_debug "Loading libraries(except appearance)..." BASH_IT_LOG_PREFIX='lib: utilities: '
LIB="${BASH_IT}/lib/*.bash" _log_debug 'Loading library file ...'
APPEARANCE_LIB="${BASH_IT}/lib/appearance.bash" # shellcheck source=./lib/utilities.bash
for _bash_it_config_file in $LIB; do source "${BASH_IT}/lib/utilities.bash"
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
# Load vendors # "helpers" - generic functions meant for generic use by end users,
BASH_IT_LOG_PREFIX="vendor: " # often wrappers of functions found in utilities
for _bash_it_vendor_init in "${BASH_IT}"/vendor/init.d/*.bash; do BASH_IT_LOG_PREFIX='lib: helpers: '
_log_debug "Loading \"$(basename "${_bash_it_vendor_init}" .bash)\"..." _log_debug 'Loading library file ...'
# shellcheck disable=SC1090 # shellcheck source=./lib/helpers.bash
source "${_bash_it_vendor_init}" source "${BASH_IT}/lib/helpers.bash"
done
unset _bash_it_vendor_init # "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 # Load the global "enabled" directory
# "family" param is empty so that files get sources in glob order # "family" param is empty so that files get sources in glob order
# shellcheck source=./scripts/reloader.bash # shellcheck source=./scripts/reloader.bash
source "${BASH_IT}/scripts/reloader.bash" source "${BASH_IT}/scripts/reloader.bash"
# Load enabled aliases, completion, plugins # 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 # shellcheck source=./scripts/reloader.bash
source "${BASH_IT}/scripts/reloader.bash" "skip" "$file_type" source "${BASH_IT}/scripts/reloader.bash" 'skip' "$file_type"
done done
# Load theme, if a theme was set # Load theme, if a theme was set
if [[ -n "${BASH_IT_THEME}" ]]; then if [[ -n "${BASH_IT_THEME}" ]]; then
_log_debug "Loading \"${BASH_IT_THEME}\" theme..." _log_debug "Loading \"${BASH_IT_THEME}\" theme..."
# Load colors and helpers first so they can be used in base 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 # shellcheck source=./themes/colors.theme.bash
source "${BASH_IT}/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 # shellcheck source=./themes/githelpers.theme.bash
source "${BASH_IT}/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 # shellcheck source=./themes/p4helpers.theme.bash
source "${BASH_IT}/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 # shellcheck source=./themes/command_duration.theme.bash
source "${BASH_IT}/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 # shellcheck source=./themes/base.theme.bash
source "${BASH_IT}/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 # appearance (themes) now, after all dependencies
# shellcheck source=./lib/appearance.bash # shellcheck source=./lib/appearance.bash
source "$APPEARANCE_LIB" source "${BASH_IT}/lib/appearance.bash"
fi fi
BASH_IT_LOG_PREFIX="core: main: " # Load custom components ...
_log_debug "Loading custom aliases, completion, plugins..."
for file_type in "aliases" "completion" "plugins"; do BASH_IT_LOG_PREFIX='core: main: '
if [ -e "${BASH_IT}/${file_type}/custom.${file_type}.bash" ]; then _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: " BASH_IT_LOG_PREFIX="${file_type}: custom: "
_log_debug "Loading component..." _log_debug 'Loading component...'
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${BASH_IT}/${file_type}/custom.${file_type}.bash" source "${BASH_IT}/${file_type}/custom.${file_type}.bash"
fi fi
done done
# Custom
BASH_IT_LOG_PREFIX="core: main: " BASH_IT_LOG_PREFIX="core: main: "
_log_debug "Loading general custom files..." _log_debug "Loading general custom files..."
CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/**/*.bash" CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/**/*.bash"
for _bash_it_config_file in $CUSTOM; do 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=$(basename "${_bash_it_config_file}")
filename=${filename%*.bash} filename=${filename%*.bash}
BASH_IT_LOG_PREFIX="custom: $filename: " BASH_IT_LOG_PREFIX="custom: $filename: "
@ -137,7 +149,6 @@ elif [ -s /Applications/Preview.app ]; then
fi fi
# Load all the Jekyll stuff # Load all the Jekyll stuff
if [ -e "$HOME/.jekyllconfig" ]; then if [ -e "$HOME/.jekyllconfig" ]; then
# shellcheck disable=SC1090 # shellcheck disable=SC1090
. "$HOME/.jekyllconfig" . "$HOME/.jekyllconfig"
@ -155,5 +166,17 @@ if ! command -v reload &> /dev/null && [ -n "$BASH_IT_RELOAD_LEGACY" ]; then
esac esac
fi 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 # Disable trap DEBUG on subshells - https://github.com/Bash-it/bash-it/pull/1040
set +T 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"