]> git.corax.cc Git - toolbox/commitdiff
toolbox: Allow modules to be included from TOOLBOX_HOME user-includes
authorMatthias Kruk <m@m10k.eu>
Sat, 3 Apr 2021 05:42:42 +0000 (14:42 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 3 Apr 2021 05:42:42 +0000 (14:42 +0900)
The current implementation requires that modules be located in the
global module search path, TOOLBOX_PATH/include. This commit modifies
the implementation so that modules may be included from the user's
TOOLBOX_HOME in addition to the global search path:
When include() is invoked, it will attempt to include the module from
the TOOLBOX_HOME/include first, and only if it didn't succeed, it will
attempt to load the module from TOOLBOX_PATH/include.

toolbox.sh

index 9f419c40d929a9d0fa04318dd186e875a66163fd..432a36ea54cfd816e99d8b7f0a8c9f412c490b37 100644 (file)
@@ -6,65 +6,91 @@
 #
 
 __toolbox_init() {
-       export TOOLBOX_PATH="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
-       export TOOLBOX_HOME="$HOME/.toolbox"
+       local modpath
 
-       declare -ag __TOOLBOX_INCLUDED=()
+       if ! modpath="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"; then
+               echo "Could not determine toolbox path" 1>&2
+               return 1
+       fi
+
+       declare -gxr TOOLBOX_PATH="$modpath"
+       declare -gxr TOOLBOX_HOME="$HOME/.toolbox"
+       declare -axgr __TOOLBOX_MODULEPATH=(
+               "$TOOLBOX_HOME/include"
+               "$modpath/include"
+       )
+
+       declare -Axg __TOOLBOX_INCLUDED
 
        return 0
 }
 
 have() {
-       local module
-       local included
+       local module="$1"
 
-       module="$1"
+        if [[ -n "${__TOOLBOX_INCLUDED[$module]}" ]]; then
+               return 0
+       fi
 
-       for included in "${__TOOLBOX_INCLUDED[@]}"; do
-               local modpath
+       return 1
+}
 
-               modpath="$TOOLBOX_PATH/include/$module.sh"
+_try_include() {
+       local mod_name
+       local mod_path
+       local err
 
-               if [[ "$included" == "$modpath" ]]; then
-                       return 0
-               fi
-       done
+       mod_name="$1"
+       mod_path="$2"
 
-       return 1
+       if ! . "$mod_path" &>/dev/null; then
+               return 1
+       fi
+
+       if ! __init; then
+               echo "ERROR: Could not initialize $module" 1>&2
+               err=1
+       else
+               __TOOLBOX_INCLUDED["$mod_name"]="$mod_path"
+               err=0
+       fi
+
+       unset -f __init
+
+       return "$err"
 }
 
 include() {
-       local err
        local module
 
-       err=0
-
        for module in "$@"; do
-               local modpath
+               local searchpath
+               local loaded
 
                if have "$module"; then
                        continue
                fi
 
-               modpath="$TOOLBOX_PATH/include/$module.sh"
+               loaded=false
 
-               if ! . "$modpath"; then
-                       echo "ERROR: Could not load $modpath" 1>&2
-                       err=1
-                       continue
-               fi
+               for searchpath in "${__TOOLBOX_MODULEPATH[@]}"; do
+                       local modpath
 
-               if ! __init; then
-                       echo "ERROR: Could not initialize $module" 1>&2
-                       err=1
-               else
-                       __TOOLBOX_INCLUDED+=("$modpath")
-               fi
+                       modpath="$searchpath/$module.sh"
+
+                       if _try_include "$module" "$modpath"; then
+                               loaded=true
+                               break
+                       fi
+               done
 
-               unset -f __init
+               if ! "$loaded"; then
+                       echo "ERROR: Could not include $module" 1>&2
+                       return 1
+               fi
        done
 
-       return "$err"
+       return 0
 }
 
 {