-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin.env
65 lines (57 loc) · 1.39 KB
/
bin.env
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# adapted from Arch' /etc/profile
# zsh has no nameref; sunrise by hand
append() {
local name="$1" var="$(eval "echo \"\$$1\"")"; shift
local arg
for arg; do
case ":$var:" in
*:"$arg":*) ;;
*) var="${var:+$var:}$arg" ;;
esac
done
eval "$name=\"$var\""
}
prepend() {
local name="$1" var="$(eval "echo \"\$$1\"")"; shift
local arg
for arg; do
case ":$var:" in
*:"$arg":*) ;;
*) var="$arg${var:+:$var}" ;;
esac
done
eval "$name=\"$var\""
}
if [[ -t 2 ]]; then echo "bin.env" >&2; fi
if test -n "$BASH"; then __bin_path="$BASH_SOURCE"
elif test -n "$ZSH_NAME"; then __bin_path="${(%):-%x}"
else echo "bin.env: cannot deduce path: unsupported shell" >&2; return 1
fi
__bin_path="$(realpath -qe -- "$__bin_path")"
__bin_path="$(dirname "$__bin_path")"
if ! [[ -e "$__bin_path/lib/lib.sh" ]]; then
echo "bin.env: bad deduced path: '$__bin_path'" >&2; return 1
fi
# add common utils and command wrappers
__path=(
$__bin_path/hw
$__bin_path/tools
$__bin_path/inc
$__bin_path/util
$__bin_path/wrappers
)
__path_add_dir_and_sub() {
for p in "$1"/*; do
[[ -f "$p" && -x "$p" ]] && { __path+=( "$1" ); break; }
done
for p in "$1"/*; do
[[ -d "$p" ]] && __path+=( "$p" )
done
}
# add custom git subcommands
__path_add_dir_and_sub "$__bin_path/git"
# add external tools
__path_add_dir_and_sub "$__bin_path/ext"
prepend PATH "${__path[@]}"
unset __path_add_dir_and_sub
unset __bin_path __path p