-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.bash
47 lines (40 loc) · 1.38 KB
/
lib.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env bash
set -e
# We are on old bash on macos, we have startup brew directory set and we are not already in such env
if [[ "${BASH_VERSINFO[0]}" -lt 4 ]] && [[ "$OSTYPE" == "darwin"* ]] && [ -n "$BASHLIB_STARTUP_BREW" ] && [ -z "$BASHLIB_BREW_DIR" ];then
# shellcheck source=/dev/null
source "$(dirname "${BASH_SOURCE[0]}")/brew_portable.bash"
brew::enter "$BASHLIB_STARTUP_BREW"
if ! [ -f "$BASHLIB_BREW_DIR/bin/bash" ];then
echo "Composing modern bash for itself" 1>&2
log="$(brew::brew install bash 2>&1)" || echo "$log" && exit 1
fi
# restart
exec bash "$0" "$@"
fi
[[ "${BASH_VERSINFO[0]}" -lt 4 ]] && echo "This application require bash >= 4, please upgrade it first" && exit 1
declare -A bash_lib__imported
bash_lib__paths=( "$(dirname "${BASH_SOURCE[0]}")" )
import::add_path() {
local path="$1"
bash_lib__paths+=("$path")
}
import() {
# because it might be unset in subshell
set -e
for dir in "${bash_lib__paths[@]}";do
local filename="$dir/$*.bash"
if ! [ -f "$filename" ];then
continue
fi
if [ "${bash_lib__imported["$filename"]}" != "1" ];then
bash_lib__imported["$filename"]=1
# shellcheck source=/dev/null
source "$filename"
fi
return
done
echo "Import failed for: $*" >&2
stack::print 1 1
}
import stack