Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create lib finalize hook #2004

Merged
merged 2 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions bash_it.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ fi
# shellcheck disable=SC1090
source "${BASH_IT}"/vendor/github.com/erichs/composure/composure.sh

# We need to load logging module first as well in order to be able to log
# shellcheck source=./lib/log.bash
# Declare our end-of-main finishing hook
declare -a _bash_it_library_finalize_hook

# We need to load logging module early in order to be able to log
# shellcheck source-path=SCRIPTDIR/lib
source "${BASH_IT}/lib/log.bash"

# We can only log it now
Expand Down Expand Up @@ -148,3 +151,8 @@ if ! _command_exists reload && [[ -n "${BASH_IT_RELOAD_LEGACY:-}" ]]; then
;;
esac
fi

for _bash_it_library_finalize_f in "${_bash_it_library_finalize_hook[@]:-}"; do
eval "${_bash_it_library_finalize_f?}" # Use `eval` to achieve the same behavior as `$PROMPT_COMMAND`.
done
unset "${!_bash_it_library_finalize_@}"
5 changes: 5 additions & 0 deletions docs/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ Plugin Disable Callbacks
Plugins can define a function that will be called when the plugin is being disabled.
The callback name should be ``{PLUGIN_NAME}_on_disable``\ , you can see ``gitstatus`` for usage example.

Library Finalization Callback
-----------------------------

Specifically for Bash-it library code, e.g. in the `lib` subdirectory, a hook is available to run some code at the very end of the main loader script after all other code has been loaded. For example, `lib/theme` uses `_bash_it_library_finalize_hook+=('_bash_it_appearance_scm_init')` to add a function to be called after all plugins have been loaded.

Using the pre-commit hook
-------------------------

Expand Down
2 changes: 1 addition & 1 deletion themes/base.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function _bash_it_appearance_scm_init() {
fi
fi
}
_bash_it_appearance_scm_init
_bash_it_library_finalize_hook+=('_bash_it_appearance_scm_init')
NoahGorny marked this conversation as resolved.
Show resolved Hide resolved

function scm {
if [[ "$SCM_CHECK" = false ]]; then
Expand Down