refactor module library

move non essential functions to extra module
minimize essential modules functions to module.base and load that
master
David Kebler 2023-12-17 12:54:45 -08:00
parent 82dec74a7b
commit 856efb5e35
6 changed files with 172 additions and 88 deletions

2
make-module-base Executable file
View File

@ -0,0 +1,2 @@
module_load minimize
minimize module.base.src module.base

53
module.base Normal file
View File

@ -0,0 +1,53 @@
module_confirm() {
local FILE
local DIR
local NAME
DIR=$1
NAME=$2
FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
COUNT=0
if [ "$FILE" ]; then
COUNT=$(echo $FILE | xargs -n 1 | wc -l)
fi
if [ $COUNT -gt 1 ]; then
echo two or more modules of same name found, aborting
echo $FILE | xargs -n 1
return 1
fi
[ $COUNT == 1 ] && echo $FILE && return 0
return 1
}
module_find() {
[ ! $1 ] && echo "no module specified" && return 1
local MDIRS
local MDIR
local DIRS
local MODULE=$1
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} ${BASH_SHELL_NETWORKS_LOADED} )
[ -d BASH_SHELL_USER_DIR ] && DIRS=("${DIRS[@]}" "BASH_SHELL_USER_DIR")
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
DIRS=("${DIRS[@]/%/\/modules}")
DIRS=("${MODULE_DIRS[@]}" "${DIRS[@]}")
cnt=${#DIRS[@]}
for ((i=1;i<=cnt;i++)); do
DIR="${DIRS[cnt-i]}"
if [[ -d $DIR ]]; then
RES=$(module_confirm "$DIR" "$MODULE")
[ $? -eq 0 ] && echo $RES && return 0
fi
done
return 1
}
module_load() {
[ ! $1 ] && echo "no module specified" && return 1
local FILE
FILE=$(module_find $1)
[ $? -ne 0 ] && echo no module $1 found && return 1
shift 1
source $FILE "$@"
return 0
}
shell_run () {
bash -ci ". ${1}"
}

View File

@ -1,8 +1,8 @@
#!/bin/bash
# echo loading module library
# source with set -a and set +a to have these bootstrap functions available in the environment
module_confirm() {
local FILE
@ -12,7 +12,7 @@ DIR=$1
NAME=$2
# TODO use file instead for valid extensions
# allow explicit extension
# will look for valid module extensions and also look for duplicate modules with a directory which is not allowed
FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
# echo files found $FILE
COUNT=0
@ -29,7 +29,7 @@ fi
return 1
}
# if succesfull returns the path
# Returns path to module if succesful
module_find() {
[ ! $1 ] && echo "no module specified" && return 1
@ -39,32 +39,26 @@ local MDIR
local DIRS
local MODULE=$1
# MODULE_DIRS is array set in the environment and is an array of additional directories to
# search for modules. This makes is easy to include a custom module libraries outside
# the shell system. These take precedence over any modules found in shell directories below.
# Precedence is first in list to last.
if [[ $MODULE_DIRS ]]; then
for DIR in "${MODULE_DIRS[@]}"
do
if [[ -d $DIR ]]; then
RES=$(module_confirm "$DIR" "$MODULE")
[ $? -eq 0 ] && echo $RES && return 0
fi
done
fi
# BASH_SHELL_DIRS holds shell directories to search for modules. If will be searched
# in revsere order, user or network repos over host over base
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} ${BASH_SHELL_NETWORKS_LOADED} )
[ -d BASH_SHELL_USER_DIR ] && DIRS=("${DIRS[@]}" "BASH_SHELL_USER_DIR")
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
# echo "before: ${DIRS[@]}";
DIRS=("${DIRS[@]/%/\/modules}")
DIRS=("${MODULE_DIRS[@]}" "${DIRS[@]}")
# echo "after: ${DIRS[@]}"
cnt=${#DIRS[@]}
for ((i=1;i<=cnt;i++)); do
# find modules in reverse order so more specific ones override
DIR="${DIRS[cnt-i]}/modules"
DIR="${DIRS[cnt-i]}"
if [[ -d $DIR ]]; then
# echo $DIR $MODULE
RES=$(module_confirm "$DIR" "$MODULE")
@ -89,76 +83,9 @@ source $FILE "$@"
return 0
}
module_print() {
[ ! $1 ] && echo "no module specified" && return 1
# (return 0 2>/dev/null) && echo "module_load was sourced" || echo "module_log was executed"
local FILE
FILE=$(module_find $1)
[ $? -ne 0 ] && echo no module $1 found && return 1
cat $FILE
return 0
}
module_cp() {
[ ! $1 ] && echo "no module specified" && return 1
local FILE
FILE=$(module_find $1)
[ $? -ne 0 ] && echo no module $1 found && return 1
echo cp $FILE ${2:-$(pwd)}
cp $FILE ${2:-$(pwd)}
return 0
}
module_loaded() {
[ ! $1 ] && echo "no module specified" && return 1
local func
local ret
local file
ret=0
# function_list | grep module
file=$(module_find $1)
[ ! $file ] && echo no module $1 found && return 1
# echo list "$(function_list $file)"
for func in $(function_list $file)
do
[[ ! $(function_list | grep $func) ]] && ret=1
[[ $2 = -vv ]] && ([[ $ret = 0 ]] && echo "$func loaded" || echo "$func not loaded")
done
[[ $2 = -v ]] && echo "module: $1" && ([[ $ret = 0 ]] && echo "all functions " || echo "some or all functions not loaded")
[[ $ret = 0 ]] && echo "loaded"
return $ret
}
# put here so it's globally available
# runs a script but loads the current interactive shell environment, not recommended unless there is a need
# mostly for testing. Better to source some part of the shell as needed using shell-process-directory module
shell_run () {
bash -ci ". ${1}"
}
# can one off export a function
# declare -f -x module_load
# declare -f -x module_find
# declare -f -x module_confirm
# uncomment for testing
# function _test_modules {
# RED='\033[0;31m'
# NC='\033[0m' # No Color
# echo module to test: $1
# echo --------------------
# echo -e "testing: ${RED}module_find${NC}"
# FILE=$(module_find $1)
# [ $? -ne 0 ] && echo no module $1 found && return 1
# echo module $FILE found
# # . $FILE
# echo ---------------
# echo -e "testing: ${RED}module_load${NC}"
# module_load $1
# [ $? -ne 0 ] && echo no module $1 found && return 1
# echo loaded module $1
# echo ----------------------
# echo -e "testing: ${RED}module_loaded${NC}"
# module_loaded $1
# }

100
modules/module-extra.lib Normal file
View File

@ -0,0 +1,100 @@
#!/bin/bash
# additional module functions. Only need these explicitly
module_print() {
[ ! $1 ] && echo "no module specified" && return 1
# (return 0 2>/dev/null) && echo "module_load was sourced" || echo "module_log was executed"
local FILE
FILE=$(module_find $1)
[ $? -ne 0 ] && echo no module $1 found && return 1
cat $FILE
return 0
}
module_cp() {
[ ! $1 ] && echo "no module specified" && return 1
local FILE
FILE=$(module_find $1)
[ $? -ne 0 ] && echo no module $1 found && return 1
echo cp $FILE ${2:-$(pwd)}
cp $FILE ${2:-$(pwd)}
return 0
}
module_loaded() {
[ ! $1 ] && echo "no module specified" && return 1
local func
local ret
local file
ret=0
# function_list | grep module
file=$(module_find $1)
[ ! $file ] && echo no module $1 found && return 1
# echo list "$(function_list $file)"
for func in $(function_list $file)
do
[[ ! $(function_list | grep $func) ]] && ret=1
[[ $2 = -vv ]] && ([[ $ret = 0 ]] && echo "$func loaded" || echo "$func not loaded")
done
[[ $2 = -v ]] && echo "module: $1" && ([[ $ret = 0 ]] && echo "all functions " || echo "some or all functions not loaded")
[[ $ret = 0 ]] && echo "loaded"
return $ret
}
module_functions () {
if module_find $1 >/dev/null ; then
module_print $1 | grep -E '^[[:space:]]*([[:alnum:]_]+[[:space:]]*\(\)|function[[:space:]]+[[:alnum:]_]+)' \
| sed 's/[[:space:]]*//g' | cut -d '(' -f 1
fi
}
module_search() {
[ ! $1 ] && echo "no module search string" && return 1
local MDIRS
local MDIR
local DIRS
local NAME=$1
# MODULE_DIRS is array set in the environment and is an array of additional directories to
# search for modules. This makes is easy to include a custom module libraries outside
# the shell system. These take precedence over any modules found in shell directories below.
# Precedence is first in list to last.
if [[ $MODULE_DIRS ]]; then
for DIR in "${MODULE_DIRS[@]}"
do
if [[ -d $DIR ]]; then
echo custom modules
# echo $DIR/$NAME
# ls $DIR/$SEARCH
# [ $? -eq 0 ] && echo $RES && return 0
fi
done
fi
# BASH_SHELL_DIRS holds shell directories to search for modules. If will be searched
# in revsere order, user or network repos over host over base
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} ${BASH_SHELL_NETWORKS_LOADED} )
[ -d BASH_SHELL_USER_DIR ] && DIRS=("${DIRS[@]}" "BASH_SHELL_USER_DIR")
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
cnt=${#DIRS[@]}
for ((i=1;i<=cnt;i++)); do
# find modules in reverse order so more specific ones override
DIR="${DIRS[cnt-i]}/modules"
if [[ -d $DIR ]]; then
echo search: "$DIR/$NAME"
FILES="$(command find -L ${DIR} -type f -iname "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
echo "$FILES"
# [ $? -eq 0 ] && echo $RES && return 0
fi
done
# no module found anywhere
return 1
}

View File

@ -9,9 +9,8 @@ minimize () {
# -o filepath # output to file
# -v # verbose, only applies when using -o. will output to file and return (stdout).
local out; local verbose; local text
local min="/^[[:space:]]*#/d; /#$/d; /^$/d; s/\(.*\)#.*/\1/"
local min="/^[[:space:]]*#/d; /#$/d; /^$/d"
local OPTION; local OPTARG; local OPTIND
# host=$(sed 's/\(.*\):.*/\1/' <<<"$SRC")
while getopts 'to:v' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
@ -36,7 +35,10 @@ minimize () {
res="$(echo "$1" | sed "$min")"
else
res="$(sed "$min" "$1")"
# ; s/\(.*\)#.*/\1/"
# | sed -z 's/\n/;/g' | sed 's/;$/\n/' | tr -s ' ' | sed 's/{;/{\n/g' | sed 's/;};/\n}\n/g')"
fi
out=${out:-$2}
if [[ $out ]]; then
[[ $verbose ]] && echo "$res" | tee "$out" || echo "$res" > "$out"
else

View File

@ -82,7 +82,7 @@ BASH_SHELL_HOST="${parent}/host"
# export BASH_SHELL_ALL_HOSTS=<some directory>
[[ -d $BASH_SHELL_HOST ]] && BASH_SHELL_DIRS+="$BASH_SHELL_HOST " && export BASH_SHELL_HOST
BASH_SHELL_NETWORK=$parent/network
BASH_SHELL_NETWORK=${parent}/network
# BASH_SHELL_ALL_NETWORKS=<some directory>
[[ -d $BASH_SHELL_NETWORK ]] && BASH_SHELL_DIRS+="$BASH_SHELL_NETWORK " && export BASH_SHELL_NETWORK
# these are all the base directories to source
@ -118,8 +118,8 @@ export BASH_NETWORKS_DIR
# by default USER shell sources will be looked for under $HOME/shell
export BASH_SHELL_USER_DIR=${BASH_SHELL_USER_DIR:-$HOME/shell}
# now load and export bootstrap module loading functions library so it is always available
set -a;source $BASH_SHELL_BASE/module.lib;set +a
# now load and export bootstrap module loading functions library so it is always available even for non-interactive shells
set -a;source $BASH_SHELL_BASE/module.base;set +a
# echo done shell.env