This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.sht
159 lines (113 loc) · 3.45 KB
/
loader.sht
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
# #{_} => preprocessor macro expansion
# before anything, im very sorry about these `local` things, bash
# is sometimes trash, this is one example of bash handling clearly
# local variables like globals
# all function names starting with _ will be mangled
export BAZ_LOADER_VERSION='#{BAZ_VER}'
#{BAZ_LOGGING_FN}
_baz_load_envs() {
#{BAZ_LOG} "loading environment variables from '$BAZP_NAME'"
local f="$1/environments" e
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "loading env variable '$f'"
# to avoid forking, im using read
#{BAZ_RD} e <"$f"
# could we optimise these read calls without subshells ? ref ^
# basically, can we turn this into a single call ?
eval "#{BAZ_RD} e<<#{BAZ_RDELIM}
$e
#{BAZ_RDELIM}"
export "${f##*/}"="$e"
done
}
_baz_load_commands() {
#{BAZ_LOG} "adding '$BAZP_NAME' commands to PATH"
local d="$1/commands"
[ -d "$d" ] || return
export PATH="$d:$PATH"
#{BAZ_LOG} "making '$BAZP_NAME' commands executable"
chmod -- 700 "$d"/*
}
_baz_load_functions() {
#{BAZ_LOG} "loading functions from '$BAZP_NAME'"
local f="$1/functions" c
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "loading function '$f'"
#{BAZ_RD} c <"$f"
eval "function ${f##*/}(){
$c
}"
done
}
_baz_load_aliases() {
#{BAZ_LOG} "loading aliases from '$BAZP_NAME'"
local f="$1/aliases" a
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "loading alias '$f'"
#{BAZ_RD} a <"$f"
# shellcheck disable=SC2139
alias "${f##*/}"="$a"
done
}
_baz_load_runners() {
#{BAZ_LOG} "running runners in '$BAZP_NAME'"
local f="$1/runners"
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "starting runner : '$f'"
. "$f"
done
}
_baz_load_completions() {
#{BAZ_LOG} "adding completions from '$BAZP_NAME'"
local f="$1/completions" c
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "adding completion : '$f'"
read -r c <"$f"
complete -F "$c" -o bashdefault -o default "${f##*/}"
done
}
_baz_load_keybinds() {
#{BAZ_LOG} "loading readline keybinds from '$BAZP_NAME'"
local f="$1/keybinds"
local k="$f/all.rl" f="$f/bindings"
if [ -f "$k" ]; then
#{BAZ_LOG} 'loading full-context keybinds'
bind -f "$k"
fi
[ -d "$f" ] && for f in "$f"/*; do
#{BAZ_LOG} "loading keybinds for context : $f"
bind -m "${f##*/}" -f "$f"
done
}
baz_load_plugin_low() {
. "$1/baz.env"
local p="$1/$BAZP_SRC"
# having everything inline helps with performance,
# although reduces maintainablity
# we could also inline the function calls, but i think its
# too unmaintainable at that point
_baz_load_envs "$p"
_baz_load_commands "$p"
_baz_load_functions "$p"
_baz_load_aliases "$p"
_baz_load_runners "$p"
_baz_load_completions "$p"
_baz_load_keybinds "$p"
}
# mid level all plugins loader
baz_loader() {
local p=('#{BAZ_PLUG_DIR}'/*)
#{BAZ_LOG} 'checking if #{BAZ_PLUG_DIR} exists and isnt empty'
[ -d "${p[0]}" ] || return 1
#{BAZ_LOG} 'loading baz from data dir : #{BAZ_DATA_DIR}'
for p in "${p[@]}"; do
if [ ! -e "$p/disabled" ] && [ -f "$p/baz.env" ]; then
#{BAZ_LOG}
baz_load_plugin_low "$p"
#{BAZ_LOG}
fi
done
#{BAZ_NOP}
}
[ "$BAZ_LOADER_ENABLED" ] && baz_loader