summaryrefslogtreecommitdiff
path: root/bos-namespaces.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bos-namespaces.sh')
-rw-r--r--bos-namespaces.sh63
1 files changed, 63 insertions, 0 deletions
diff --git a/bos-namespaces.sh b/bos-namespaces.sh
new file mode 100644
index 0000000..3e9e711
--- /dev/null
+++ b/bos-namespaces.sh
@@ -0,0 +1,63 @@
+#!bash
+## namespace handling
+
+function bos-namespaces/store-for-into() {
+ local name="bos_namespaces__${2//[^[:word:]/__}__${1//[^[:word:]]/__}"
+ local -n result="$2"
+ declare -gA "$name"
+ result="$name"
+}
+
+# save all currently-visible functions in an associative array
+function bos-namespaces/start() {
+ local namespace="$1";shift
+ local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store
+ local -n saved_funcs="$ns_store"
+ local _ funcname
+
+ while read -r _ _ funcname; do
+ saved_funcs+=( ["$funcname"]="$(declare -pf "$funcname")" )
+ done < <( declare -pF )
+
+ return 0
+}
+
+# diff all currently-visible function against the saved ones, list the
+# ones that differ
+function bos-namespaces/list-new-funcs-into() {
+ local namespace="$1";shift
+ local -n result="$1"
+ local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store
+ local -n saved_funcs="$ns_store"
+ local _ funcname new_function
+
+ while read -r _ _ funcname; do
+ new_function="$(declare -pf "$funcname")"
+ if [[ "$new_function" != "${saved_funcs["$funcname"]}" ]]; then
+ result+=( "${funcname}" )
+ fi
+ done < <( declare -pF )
+
+ return 0
+}
+
+# rename a bunch of functions by prepending a prefix; restore the
+# saved function of the same name, if it exists
+function bos-namespaces/qualify-funcs() {
+ local namespace="$1";shift
+ local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store
+ local -n saved_funcs="$ns_store"
+ local _ funcname new_function
+
+ for funcname in "$@"; do
+ new_function="$(declare -pf "$funcname")"
+ eval "${namespace}/${new_function}"
+ if [[ -n "${saved_funcs["$funcname"]}" ]]; then
+ eval "${saved_funcs["$funcname"]}"
+ else
+ unset -f "$funcname"
+ fi
+ done
+
+ return 0
+}