72 |
72 |
## shell-variable-based caching
|
73 |
73 |
|
74 |
74 |
# usage: local cache_key=...; load_cache; \
|
75 |
|
# if ! cached; then save_cache value; fi; echo_cached_value
|
|
75 |
# if ! cached; then save_cache value || return; fi; echo_cached_value
|
76 |
76 |
# cache_key for function inputs: "$(declare -p "$kw_param"...) $*"
|
77 |
77 |
alias load_cache='declare cache_var="$(str2varname "${FUNCNAME}___$cache_key")"'
|
78 |
78 |
alias cached='isset "$cache_var"'
|
... | ... | |
286 |
286 |
realpath() # caches the last result for efficiency
|
287 |
287 |
{
|
288 |
288 |
local cache_key="$*"; load_cache
|
289 |
|
if ! cached; then save_cache "$(realpath__no_cache "$@")"; fi
|
|
289 |
if ! cached; then save_cache "$(realpath__no_cache "$@")" || return; fi
|
290 |
290 |
echo_cached_value
|
291 |
291 |
}
|
292 |
292 |
fi
|
... | ... | |
309 |
309 |
cd -P . # expand symlinks in $PWD so it matches the output of realpath
|
310 |
310 |
# do before setting $top_script_abs so realpath has less symlinks to resolve
|
311 |
311 |
|
312 |
|
canon_rel_path() { base_dir="$PWD" path="$(realpath "$1")" rel_path; }
|
|
312 |
canon_rel_path()
|
|
313 |
{
|
|
314 |
local path; path="$(realpath "$1")" || return
|
|
315 |
base_dir="$PWD" rel_path
|
|
316 |
}
|
313 |
317 |
|
314 |
318 |
# makes $1 a canon_rel_path if it's a filesystem path
|
315 |
319 |
alias cmd2rel_path="$(cat <<'EOF'
|
316 |
320 |
if test "$(type -t "$1")" = file && test -e "$1"; then # not relative to PATH
|
317 |
321 |
declare _1="$1"; shift
|
318 |
|
set -- "$(canon_rel_path "$_1")" "$@"
|
|
322 |
_1="$(canon_rel_path "$_1")" || return
|
|
323 |
set -- "$_1" "$@"
|
319 |
324 |
fi
|
320 |
325 |
EOF
|
321 |
326 |
)"
|
... | ... | |
373 |
378 |
if self_being_included; then
|
374 |
379 |
|
375 |
380 |
func_loc() # gets where function declared in the format file:line
|
376 |
|
{ local func="$1"; set_func_loc; echo "$(canon_rel_path "$file"):$line"; }
|
|
381 |
{
|
|
382 |
local func="$1"; set_func_loc
|
|
383 |
file="$(canon_rel_path "$file")" || return
|
|
384 |
echo "$file:$line"
|
|
385 |
}
|
377 |
386 |
|
378 |
387 |
# usage: func() { [minor=1] echo_func; ... }
|
379 |
388 |
function echo_func()
|
... | ... | |
384 |
393 |
local func="$1"; shift
|
385 |
394 |
|
386 |
395 |
log++; if test "$minor"; then log++; fi
|
387 |
|
echo_cmd "$(func_loc "$func")" "$func" "$@"
|
|
396 |
local loc; loc="$(func_loc "$func")" || return
|
|
397 |
echo_cmd "$loc" "$func" "$@"
|
388 |
398 |
can_log
|
389 |
399 |
}
|
390 |
400 |
alias echo_func='"echo_func" "$FUNCNAME" "$@" && indent || true'
|
... | ... | |
449 |
459 |
|
450 |
460 |
set_paths()
|
451 |
461 |
{
|
452 |
|
top_script="$(canon_rel_path "$top_script_abs")"; echo_vars top_script
|
453 |
|
top_dir="$(dirname "$top_script")"; echo_vars top_dir
|
|
462 |
top_script="$(canon_rel_path "$top_script_abs")" || return
|
|
463 |
echo_vars top_script
|
|
464 |
top_dir="$(dirname "$top_script")" || return; echo_vars top_dir
|
454 |
465 |
}
|
455 |
466 |
set_paths
|
456 |
467 |
|
bugfix: lib/sh/local.sh: add a manual errexit for $() exprs by embedding them in just a var assignment (without local or declare), whose exit status will then equal the of the $(). a `|| return` also needs to be added because errexit does not work on assignment statements. this commit adds them for func_loc(), echo_func(), canon_rel_path(), set_paths(), save_cache, cached realpath(), local.sh global vars