|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# @description Source Bash packages to initialize any functions |
| 4 | +# that they may want to provide in the global scope |
| 5 | +# @exitcode 4 Unexpected internal error |
| 6 | +# @exitcode 3 Problem with underlying package structure preventing proper sourcing |
| 7 | +# @exitcode 2 Source itself failed |
| 8 | +# @exitcode 1 Miscellaneous errors |
| 9 | +bpm-load() { |
| 10 | + local __bpm_flag_global='no' |
| 11 | + for arg; do |
| 12 | + case "$arg" in |
| 13 | + --global|-g) |
| 14 | + __bpm_flag_global='yes' |
| 15 | + shift |
| 16 | + ;; |
| 17 | + esac |
| 18 | + done |
| 19 | + |
| 20 | + local __bpm_pkg_name="${1:-}" |
| 21 | + local __bpm_file="${2:-}" |
| 22 | + |
| 23 | + if [ -z "$__bpm_pkg_name" ]; then |
| 24 | + printf '%s\n' "bpm-load: Error: Must pass in package name as first parameter" |
| 25 | + return 1 |
| 26 | + fi |
| 27 | + |
| 28 | + # Functions we use from 'bpm' likely require |
| 29 | + # 'nullglob' and 'extglob' to function properly |
| 30 | + local __bpm_setNullglob= __bpm_setExtglob= |
| 31 | + if shopt -q nullglob; then |
| 32 | + __bpm_setNullglob='yes' |
| 33 | + else |
| 34 | + __bpm_setNullglob='no' |
| 35 | + fi |
| 36 | + |
| 37 | + if shopt -q extglob; then |
| 38 | + __bpm_setExtglob='yes' |
| 39 | + else |
| 40 | + __bpm_setExtglob='no' |
| 41 | + fi |
| 42 | + |
| 43 | + shopt -s nullglob extglob |
| 44 | + |
| 45 | + # Source utility file |
| 46 | + local __bpm_bpm_lib_dir="${BASH_SOURCE[0]%/*}" |
| 47 | + __bpm_bpm_lib_dir="${__bpm_bpm_lib_dir%/*}" |
| 48 | + # shellcheck disable=SC1091 |
| 49 | + if ! source "$__bpm_bpm_lib_dir/util/util.sh"; then |
| 50 | + printf '%s\n' "bpm-load: Error: Unexpected error sourcing file '$__bpm_bpm_lib_dir/util/util.sh'" |
| 51 | + __bpm_bpm_load_restore_options |
| 52 | + return 4 |
| 53 | + fi |
| 54 | + |
| 55 | + # Get package information |
| 56 | + if ! util.extract_data_from_input "$__bpm_pkg_name"; then |
| 57 | + printf '%s\n' "bpm-load: Error: Unexpected error calling function 'util.extract_data_from_input' with argument '$__bpm_pkg_name'" |
| 58 | + __bpm_bpm_load_restore_options |
| 59 | + return 4 |
| 60 | + fi |
| 61 | + local __bpm_site="$REPLY2" |
| 62 | + local __bpm_package="$REPLY3" |
| 63 | + unset REPLY REPLY1 REPLY2 REPLY3 REPLY4 REPLY5 # Be extra certain of no clobbering |
| 64 | + |
| 65 | + # Get the bpm root dir (relative to this function's callsite) |
| 66 | + local __bpm_root_dir= |
| 67 | + if [ "$__bpm_flag_global" = yes ]; then |
| 68 | + __bpm_root_dir="${BPM_ROOT:-"${XDG_DATA_HOME:-$HOME/.local/share}/bpm"}/cellar" |
| 69 | + else |
| 70 | + if ! __bpm_root_dir="$(util.get_project_root_dir)/bpm_packages"; then |
| 71 | + printf '%s\n' "bpm-load: Error: Unexpected error calling function 'util.get_project_root_dir' with PWD '$PWD'" |
| 72 | + __bpm_bpm_load_restore_options |
| 73 | + return 4 |
| 74 | + fi |
| 75 | + fi |
| 76 | + |
| 77 | + # Source file, behavior depending on whether it was specifed |
| 78 | + if [ -n "$__bpm_file" ]; then |
| 79 | + local __bpm_full_path="$__bpm_root_dir/packages/$__bpm_site/$__bpm_package/$__bpm_file" |
| 80 | + |
| 81 | + if [ -d "$__bpm_full_path" ]; then |
| 82 | + printf '%s\n' "bpm-load: Error: '$__bpm_full_path' is a directory" |
| 83 | + __bpm_bpm_load_restore_options |
| 84 | + return 3 |
| 85 | + elif [ -e "$__bpm_full_path" ]; then |
| 86 | + # Ensure the error can be properly handled at the callsite, and not |
| 87 | + # bail here if errexit is set |
| 88 | + if ! source "$__bpm_full_path"; then |
| 89 | + return 2 |
| 90 | + fi |
| 91 | + else |
| 92 | + printf '%s\n' "bpm-load: Error: '$__bpm_full_path' does not exist" |
| 93 | + __bpm_bpm_load_restore_options |
| 94 | + return 3 |
| 95 | + fi |
| 96 | + else |
| 97 | + local __bpm_file= __bpm_file_was_sourced='no' |
| 98 | + # shellcheck disable=SC2041 |
| 99 | + for __bpm_file in 'load.bash'; do |
| 100 | + local __bpm_full_path="$__bpm_root_dir/packages/$__bpm_site/$__bpm_package/$__bpm_file" |
| 101 | + |
| 102 | + if [ -f "$__bpm_full_path" ]; then |
| 103 | + __bpm_file_was_sourced='yes' |
| 104 | + |
| 105 | + # Ensure the error can be properly handled at the callsite, and not |
| 106 | + # bail here if errexit is set |
| 107 | + if ! source "$__bpm_full_path"; then |
| 108 | + return 2 |
| 109 | + fi |
| 110 | + fi |
| 111 | + done |
| 112 | + |
| 113 | + # sleep 2000 |
| 114 | + if [ "$__bpm_file_was_sourced" = 'no' ]; then |
| 115 | + printf '%s\n' "bpm-load: Error: Could not automatically find package file to source" |
| 116 | + __bpm_bpm_load_restore_options |
| 117 | + return 3 |
| 118 | + fi |
| 119 | + fi |
| 120 | + |
| 121 | + __bpm_bpm_load_restore_options |
| 122 | +} |
| 123 | + |
| 124 | +# @description Restore the previous options |
| 125 | +# @noargs |
| 126 | +__bpm_bpm_load_restore_options() { |
| 127 | + if [ "$__bpm_setNullglob" ]; then |
| 128 | + shopt -s nullglob |
| 129 | + else |
| 130 | + shopt -u nullglob |
| 131 | + fi |
| 132 | + |
| 133 | + if [ "$__bpm_setExtglob" ]; then |
| 134 | + shopt -s extglob |
| 135 | + else |
| 136 | + shopt -u extglob |
| 137 | + fi |
| 138 | +} |
0 commit comments