Skip to content

Commit

Permalink
re-order library loading
Browse files Browse the repository at this point in the history
This commit removes the loops for loading libraries, switching to explicit
sourcing with comments attempting to explain why.

This change also addresses a bug with the use of preexec library which requires
control over DEBUG and PROMPT_COMMAND and must be loaded last.
  • Loading branch information
cornfeedhobo committed Feb 3, 2021
2 parents ce6487f + 8fcf713 commit e7fa160
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 59 deletions.
127 changes: 75 additions & 52 deletions bash_it.sh
Original file line number Diff line number Diff line change
@@ -1,117 +1,129 @@
#!/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')"
export BASH
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

# 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
# Load the base libraries ...
# Note: Ordering is important and intentional

# 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
# "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"

# "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: "
Expand All @@ -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"
Expand All @@ -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"
12 changes: 5 additions & 7 deletions scripts/reloader.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash
BASH_IT_LOG_PREFIX="core: reloader: "
pushd "${BASH_IT}" >/dev/null || exit 1

function _set-prefix-based-on-path()
{
Expand All @@ -9,7 +8,7 @@ function _set-prefix-based-on-path()
BASH_IT_LOG_PREFIX="$extension: $filename: "
}

if [ "$1" != "skip" ] && [ -d "./enabled" ]; then
if [[ "$1" != "skip" ]] && [[ -d "$BASH_IT/enabled" ]]; then
_bash_it_config_type=""

case $1 in
Expand All @@ -20,7 +19,7 @@ if [ "$1" != "skip" ] && [ -d "./enabled" ]; then
_log_debug "Loading all enabled components..." ;;
esac

for _bash_it_config_file in $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash")); do
for _bash_it_config_file in $(sort <(compgen -G "$BASH_IT/enabled/*${_bash_it_config_type}.bash")); do
if [ -e "${_bash_it_config_file}" ]; then
_set-prefix-based-on-path "${_bash_it_config_file}"
_log_debug "Loading component..."
Expand All @@ -32,12 +31,12 @@ if [ "$1" != "skip" ] && [ -d "./enabled" ]; then
done
fi

if [ -n "${2}" ] && [ -d "${2}/enabled" ]; then
if [[ -n "${2}" ]] && [[ -d "$BASH_IT/${2}/enabled" ]]; then
case $2 in
aliases|completion|plugins)
_log_warning "Using legacy enabling for $2, please update your bash-it version and migrate"
for _bash_it_config_file in $(sort <(compgen -G "./${2}/enabled/*.bash")); do
if [ -e "$_bash_it_config_file" ]; then
for _bash_it_config_file in $(sort <(compgen -G "$BASH_IT/${2}/enabled/*.bash")); do
if [[ -e "$_bash_it_config_file" ]]; then
_set-prefix-based-on-path "${_bash_it_config_file}"
_log_debug "Loading component..."
# shellcheck source=/dev/null
Expand All @@ -51,4 +50,3 @@ fi

unset _bash_it_config_file
unset _bash_it_config_type
popd >/dev/null || exit 1

0 comments on commit e7fa160

Please sign in to comment.