Skip to content

Commit 847d2dd

Browse files
committed
feat: Add 'bpm-load' function
This will deprecate the 'package-path'
1 parent 7e1e404 commit 847d2dd

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed

pkg/bin/bpm-load

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bash
2+
# AUTOGENERATED by the 'util-Bash-generate-bins.sh' Glue action!
3+
# Do NOT edit!
4+
5+
# https://github.com/bashup/realpaths
6+
realpath.location() {
7+
realpath.follow "$1"
8+
realpath.absolute "$REPLY" ".."
9+
}
10+
11+
realpath.dirname() {
12+
REPLY=.
13+
! [[ $1 =~ /+[^/]+/*$|^//$ ]] || REPLY="${1%${BASH_REMATCH[0]}}"
14+
REPLY=${REPLY:-/}
15+
}
16+
17+
realpath.follow() {
18+
local target
19+
while [[ -L "$1" ]] && target=$(readlink -- "$1"); do
20+
realpath.dirname "$1"
21+
# Resolve relative to symlink's directory
22+
[[ $REPLY != . && $target != /* ]] && REPLY=$REPLY/$target || REPLY=$target
23+
# Break out if we found a symlink loop
24+
for target; do [[ $REPLY == "$target" ]] && break 2; done
25+
# Add to the loop-detect list and tail-recurse
26+
set -- "$REPLY" "$@"
27+
done
28+
REPLY="$1"
29+
}
30+
31+
realpath.absolute() {
32+
REPLY=$PWD; local eg=extglob; ! shopt -q $eg || eg=; ${eg:+shopt -s $eg}
33+
while (($#)); do case $1 in
34+
//|//[^/]*) REPLY=//; set -- "${1:2}" "${@:2}" ;;
35+
/*) REPLY=/; set -- "${1##+(/)}" "${@:2}" ;;
36+
*/*) set -- "${1%%/*}" "${1##${1%%/*}+(/)}" "${@:2}" ;;
37+
''|.) shift ;;
38+
..) realpath.dirname "$REPLY"; shift ;;
39+
*) REPLY="${REPLY%/}/$1"; shift ;;
40+
esac; done; ${eg:+shopt -u $eg}
41+
}
42+
43+
44+
# Get the path to the 'lib' directory, which could be different depending on
45+
# the method of installation. If the user installed the program through their
46+
# distribution's package manager, the 'lib' files are in an extra subfolder
47+
# compared to an installation through Git (ex. Basher)
48+
realpath.location "${BASH_SOURCE[0]}"
49+
REPLY="${REPLY/%\/}"
50+
REPLY="${REPLY%/*}"
51+
if [ -d "$REPLY/lib/bpm" ]; then
52+
PROGRAM_LIB_DIR="$REPLY/lib/bpm"
53+
elif [ -d "$REPLY/lib" ]; then
54+
PROGRAM_LIB_DIR="$REPLY/lib"
55+
else
56+
echo "Error: Could not determine \$PROGRAM_LIB_DIR"
57+
exit 1
58+
fi
59+
60+
# shellcheck disable=SC1091
61+
source "$PROGRAM_LIB_DIR/cmd/bpm-load.sh"

pkg/lib/cmd/bpm-load.sh

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}

tests/bpm-load.bats

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bats
2+
3+
load 'util/init.sh'
4+
5+
@test "works without file argument" {
6+
local site='github.com'
7+
local pkg="user/project2"
8+
9+
test_util.setup_pkg "$pkg"; {
10+
echo "printf '%s\n' 'it works :)'" > 'load.bash'
11+
}; test_util.finish_pkg
12+
test_util.mock_add "$pkg"
13+
14+
BPM_ROOT="${BPM_PACKAGES_PATH%/*}"
15+
BPM_ROOT="${BPM_ROOT%/*}"
16+
17+
source bpm-load
18+
run bpm-load --global "$pkg"
19+
20+
assert_success
21+
assert_output "it works :)"
22+
}
23+
24+
@test "properly restores options" {
25+
:
26+
}

0 commit comments

Comments
 (0)