-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashrcd
More file actions
422 lines (372 loc) · 9.27 KB
/
Copy pathbashrcd
File metadata and controls
422 lines (372 loc) · 9.27 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
BASHRCD_PATH="${BASHRCD_PATH}:-/etc/bashrc.d:~/.bashrc.d"
# Load a bashrc.d component (once), display profiling statistics, etc
bashrc.d(){
local arg
local USAGE="$(sed 's/^..//' <<USAGE
bashc.d (<component> <action> | --help)
core actions:
PATH manipulate PATH variables
list show available components
load [<component>] load <component> (or all), if not already loaded
USAGE
)"
local component
while [ $# -gt 0 ]; do
arg="$1"; shift
case "$arg" in
--help|-h|help)
echo "$USAGE"
return
;;
-*)
echo "$USAGE" >&2
return 1
;;
load)
"bashrc.d_core_${arg}" "$@"
return "$?"
;;
*)
if [ -z "$component" ]; then
if bashrc.d_core_-is-loaded "$arg" || bashrc.d_core_load "$arg"; then
component="$arg"
else
return 1
fi
elif [ "$arg" = "noop" ]; then
true
elif [ "$(type -t "bashrc.d_${component}_${arg}")" = "function" ]; then
"bashrc.d_${component}_${arg}" "$@"
return "$?"
elif [ "$(type -t "bashrc.d_${component}")" = "function" ]; then
"bashrc.d_${component}" "${arg}" "$@"
return "$?"
else
echo "unknown command '$component' '$arg'" >&2
echo "$USAGE" >&2
return 1
fi
;;
esac
done
echo "$USAGE" >&2
return 1
}
bashrc.d_core_-fullpath(){
local path="$1"
local pwd="$(builtin pwd -L)"
path="$(builtin printf '%q' "$path")"
[[ -z "${path##\\~*}" ]] && path="${path:1}"
path="$(eval builtin printf '%s' $path)"
[ -z "${path##/*}" ] && builtin printf "%s\n" "$path" && return 0
builtin printf '%s/%s\n' "$pwd" "$path"
}
bashrc.d_core_-is-loaded(){
[ "$1" = "core" ] && return 0
local component="$1"
local loaded="$(builtin printf ':%s:' "${__BASHRCD_LOADED[@]}")"
[ -n "$loaded" -a -z "${loaded//*:$component:*/}" ]
}
bashrc.d_core_-forget-is-loaded(){
local OIFS;
local loaded
local component
loaded="$(builtin printf ':%s:' "${__BASHRCD_LOADED[@]}")"
for component in "$@"; do
bashrc.d_core_-is-loaded "$component" || continue
loaded="${loaded//:$component:/::}"
loaded="${loaded//::/:}"
loaded="${loaded//::/:}"
done
loaded="${loaded%:}"
loaded="${loaded#:}"
OIFS="$IFS"
IFS=':'
__BASHRCD_LOADED=( $loaded )
IFS="$OIFS"
}
bashrc.d_core_PATH(){
local USAGE="$(sed 's/^..//' <<USAGE
usage: bashrc.d core PATH [--PATH=<PATH>] <entry> [<entry>...]
Create a PATH variable by concatenating <entry>s using the PATH separator.
example: PATH="\$(bashrc.d core mkPATH --PATH="\$PATH" --prefix ~/bin)"
options:
--PATH=<PATH> add entries to <PATH>
--prefix add entries to the beginning of <PATH>
--suffix add entries to the end of <PATH>
--file=<FILE> obtain entries from <FILE>, one per line
--zfile=<FILE> obtain entries from <FILE>, one per NUL-terminated line
-f validate/expand all entries into full directories
-z output entries terminated by NUL bytes, rather than as a PATH
--help display this message and return
USAGE
)"
local arg
local do_prefix=0
local do_suffix=1
local do_expand=0
local z=0
local path=()
local path_prefix=()
local path_suffix=()
local entry
local OIFS="$IFS"
while [ $# -gt 0 ]; do
arg="$1"; shift
case "$arg" in
--PATH=*|--path=*)
IFS=:
for entry in ${arg#*=}; do
path[${#path[@]}]="$entry"
done
IFS="$OIFS"
;;
--prefix)
do_prefix=1
do_suffix=0
;;
--suffix)
do_prefix=0
do_suffix=1
;;
--file=*)
arg="$(bashrc.d_core_-fullpath "${arg#*=}")" || return 1
while read entry; do
[ "$do_prefix" = "1" ] && path_prefix[${#path_prefix[@]}]="$entry"
[ "$do_suffix" = "1" ] && path_suffix[${#path_suffix[@]}]="$entry"
done < <( sed 's/^\s*//;s/\s*$//;/^#/d;' "$arg" )
;;
--zfile=*)
arg="$(bashrc.d_core_-fullpath "${arg#*=}")" || return 1
while read -d $'\0' -r entry; do
[ "$do_prefix" = "1" ] && path_prefix[${#path_prefix[@]}]="$entry"
[ "$do_suffix" = "1" ] && path_suffix[${#path_suffix[@]}]="$entry"
done < "$arg"
;;
-f)
do_expand=1
;;
-z)
z=1
;;
--help)
echo "$USAGE"
return
;;
-*)
echo "Unknown argument '$arg'" >&2
echo
echo "$USAGE" >&2
return 1
;;
*)
[ "$do_prefix" = "1" ] && path_prefix[${#path_prefix[@]}]="$arg"
[ "$do_suffix" = "1" ] && path_suffix[${#path_suffix[@]}]="$arg"
;;
esac
done
path=( "${path_prefix[@]}" "${path[@]}" "${path_suffix[@]}" )
local expanded=()
if [ $do_expand -eq 1 ]; then
for entry in "${path[@]}"; do
entry="$(bashrc.d_core_-fullpath "$entry")"
[ -d "$entry" ] || continue
expanded[${#expanded[@]}]="$entry"
done
path=( "${expanded[@]}" )
fi
if [ "$z" = "1" ]; then
printf '%s\0' "${path[@]}"
else
local first=1
for entry in "${path[@]}"; do
[ "$first" = "0" ] && printf '%s' ':'
printf '%s' "$entry"
first=0
done
[ "$first" = "0" ] && echo
fi
return 0
}
bashrc.d_core_list(){
local USAGE="$(sed 's/^..//' <<USAGE
usage: bashrc.d core list [-z] [<prefix>]
options:
--help display this message and return
-z output terminated by NUL byte, rather than newlines
<pattern> limit output to entries matching find-style fnmatch(2) <pattern>
USAGE
)"
local arg
local do_full=0
local patterns=()
local terminator='\n'
local z=()
while [ $# -gt 0 ]; do
arg="$1"; shift
case "$arg" in
-f|--full-paths)
do_full=1
;;
-z)
terminator=''
z=( '-z' )
;;
--help)
echo "$USAGE"
return
;;
*)
patterns[${#patterns[@]}]=-o
patterns[${#patterns[@]}]=-name
patterns[${#patterns[@]}]="$arg"
;;
esac
done
if [ ${#patterns[@]} -gt 0 ]; then
patterns=( -a '(' -false "${patterns[@]}" ')' )
fi
local path=()
local entry
while read -d $'\0' -r entry; do
[ -d "$entry" ] || continue
path[${#path[@]}]="$entry"
done < <( bashrc.d_core_PATH --PATH="$BASHRCD_PATH" -f -z )
local path
local component
local output=component
[ $do_full -eq 1 ] && output=path
find -L "${path[@]}" -maxdepth 1 \
-type f \
-not '(' -name 'core' -o -name 'load' -o -name '.*' -o -name '_*' ')' \
"${patterns[@]}" \
-print0 |
xargs -0 awk '
BEGIN{for(i = 1; i < ARGC; i++){
path=ARGV[i];
if( !match(path, /(^|\/)[-A-Za-z0-9][-A-Za-z0-9]*$/) ) continue;
component=path;
sub(/^.*\//, "", component);
if( !components[ component ] ){
components[ component ] = path;
if( length("'"$terminator"'") ){
printf("%s'"$terminator"'", '"$output"');
continue;
}
printf("%s%c", '"$output"', 0);
}
}}
'
}
__BASHRCD_LOADED=()
__BASHRCD_PROFILE=()
bashrc.d_core_load(){
local USAGE="$(sed 's/^..//' <<USAGE
usage: bashrc.d core load [-f] [-v] [<component>]
USAGE
)"
local components=()
local do_verbose=0
local do_profile=0
local do_force=0
local arg
while [ $# -gt 0 ]; do
arg=$1; shift
case "$arg" in
-v|--verbose)
do_verbose=1
;;
-f|--force)
do_force=1
;;
--profile)
do_profile=1
;;
--help)
echo "$USAGE"
return
;;
-*)
echo "Unknown argument '$arg'" >&2
echo
echo "$USAGE" >&2
return 1
;;
*)
components[${#components[@]}]="$arg"
;;
esac
done
local component
local component_path
local component_paths=()
while read -d $'\0' -r component_path; do
component="${component_path##*/}"
found[${#found[@]}]="$component"
component_paths[${#component_paths[@]}]="$component_path"
done < <( bashrc.d_core_list -f -z "${components[@]}" )
if [ ${#components[@]} -gt 0 -a ${#found[@]} -ne ${#components[@]} ]; then
local candidate
for component in "${components[@]}"; do
for candidate in "${found[@]}"; do
[ "$component" = "$candidate" ] && continue 2
done
echo "Unknown component '$component'" >&2
return 1
done
fi
if [ $do_force -eq 1 ]; then
bashrc.d_core_-forget-is-loaded "${component_paths[@]##*/}"
fi
local profile_begin
local profile_end
local profile_inner_begin
local profile_inner_end
for component_path in "${component_paths[@]}"; do
[ $do_profile -eq 1 ] && profile_begin="$(date +'%s%N')"
component="${component_path##*/}"
bashrc.d_core_-is-loaded "$component" && continue
[ $do_verbose -eq 1 ] && echo "Loading '$component'" >&2
[ $do_profile -eq 1 ] && profile_inner_begin="$(date +'%s%N')"
. "$component_path"
[ $do_profile -eq 1 ] && profile_inner_end="$(date +'%s%N')"
__BASHRCD_LOADED[${#__BASHRCD_LOADED[@]}]="$component"
if [ $do_profile -eq 1 ]; then
profile_end="$(date +'%s%N')"
__BASHRCD_PROFILE[${#__BASHRCD_PROFILE[@]}]="$component"
__BASHRCD_PROFILE[${#__BASHRCD_PROFILE[@]}]="$(( profile_end - profile_begin ))"
__BASHRCD_PROFILE[${#__BASHRCD_PROFILE[@]}]="$(( profile_inner_end - profile_inner_begin ))"
fi
done
}
bashrc.d_core_profile(){
local component
local timing
local timing_inner
local part
for part in "${__BASHRCD_PROFILE[@]}"; do
[ -z "$component" ] && component="$part" && continue
[ -z "$timing" ] && timing="$part" && continue
timing_inner="$part"
printf '%s\t%s\t%s\n' "$timing" "$timing_inner" "$component"
component=
timing=
done | sort -n | awk '
BEGIN{
total=0.0;
total_inner=0.0;
scaler=1000000000;
}
{
total += $1;
total_inner += $2;
printf "%.5f %.5f %.5f %s\n", ($1/scaler), (($1 - $2)/scaler), ($2/scaler), $3;
}
END{
printf "%.5f %.5f %s\n",
(total/scaler),
((total - total_inner)/scaler),
(total_inner/scaler),
"TOTAL";
}'
}