diff options
author | Singustromo <singustromo@disroot.org> | 2023-11-05 01:49:48 +0100 |
---|---|---|
committer | Singustromo <singustromo@disroot.org> | 2023-11-05 01:49:54 +0100 |
commit | 6fb85835ba6aaa5c41fedad085479ca4c0ea7a04 (patch) | |
tree | 24759aa507c591d5278bd711291c26d32a464461 | |
parent | 6aa6562805eec575c057e46a5ed79110a61538c4 (diff) | |
download | bash-framework-master.tar.gz bash-framework-master.zip |
-rw-r--r-- | lib/array/contains.sh | 2 | ||||
-rwxr-xr-x | lib/bootstrap.sh | 28 | ||||
-rw-r--r-- | lib/io/log.sh | 2 | ||||
-rw-r--r-- | lib/io/message.sh | 36 | ||||
-rw-r--r-- | lib/io/print_spaces.sh | 2 | ||||
-rw-r--r-- | lib/util/command.sh | 6 | ||||
-rw-r--r-- | lib/util/function/copy.sh | 2 | ||||
-rw-r--r-- | lib/util/function/exists.sh | 2 | ||||
-rw-r--r-- | lib/util/function/inject_code.sh | 2 | ||||
-rw-r--r-- | lib/util/function/list.sh | 2 | ||||
-rw-r--r-- | lib/util/function/rename.sh | 2 | ||||
-rw-r--r-- | lib/util/slice.sh | 2 | ||||
-rw-r--r-- | lib/util/uuid_generate.sh | 2 | ||||
-rw-r--r-- | lib/var/is_number.sh | 2 | ||||
-rw-r--r-- | lib/var/sanitize.sh | 2 | ||||
-rw-r--r-- | lib/var/slash_replacement.sh | 4 | ||||
-rwxr-xr-x | your-script.sh | 11 |
17 files changed, 42 insertions, 67 deletions
diff --git a/lib/array/contains.sh b/lib/array/contains.sh index f311935..6f30e23 100644 --- a/lib/array/contains.sh +++ b/lib/array/contains.sh @@ -1,5 +1,5 @@ # This is an alternative to the default defined in bootstrap.sh -Array::contains() { +Array:contains() { local element for element in "${@:2}"; do [ "$element" = "$1" ] && return diff --git a/lib/bootstrap.sh b/lib/bootstrap.sh index 72373a6..e315086 100755 --- a/lib/bootstrap.sh +++ b/lib/bootstrap.sh @@ -13,6 +13,7 @@ File:absolute_path() { printf "%s\n" "$file" return fi + local file_basename="${file##*/}" local file_path="$(realpath "$file" >/dev/null 2>&1)" local file_path="${file_path%/*}" @@ -35,8 +36,7 @@ System:die() { System:SourceFile() { local libPath="${1:?no file provided}"; shift - libPath="$(File:absolute_path "$libPath")" \ - || return + libPath="$(File:absolute_path "$libPath")" || return ## already imported? -> let's return # if declare -f "$libPath" &> /dev/null && @@ -79,7 +79,7 @@ System:ImportOne() { # try global library # try local library { - local localPath="$( cd "${BASH_SOURCE[1]%/*}" && pwd )" + local localPath="$( cd "${BASH_SOURCE[1]%/*}" && builtin pwd )" localPath="${localPath}/${libPath}" System:SourcePath "${localPath}" "$@" } && return @@ -88,15 +88,28 @@ System:ImportOne() { || System:SourcePath "${libPath}" "$@" \ || System:SourcePath "${_PRISM_LIBPATH}/${libPath}" "$@" \ || System:SourcePath "${_PRISM_ROOT}/${libPath}" "$@" + } System:Import() { - local libPath + local libPath ImportFailure for libPath in "$@"; do - System:ImportOne "$libPath" || return + System:ImportOne "$libPath" + +# if ! Array:contains "$libPath" "${_PRISM_IMPORTS[@]}"; then +# _PRISM_FAILED_IMPORTS=true +# ImportFailure+="$libPath " +# fi done + +# [ "$_PRISM_FAILED_IMPORTS" == "true" ] && { +# printf "Unable to import: %s\n" "${ImportFailure}" +# false; exit +# } } +import() { System:Import $*; } + ## note: aliases are visible inside functions only if ## they were initialized AFTER they were created ## this is the reason why we have to load files in a specific order @@ -122,9 +135,6 @@ declare -g -r _PRISM_ROOT="${_PRISM_LIBPATH%/*}" declare -g -a _PRISM_IMPORTS : "${_PRISM_ALLOW_FILERELOAD:=false}" - -import() { - eval "System:Import $*" -} +: "${_PRISM_FAILED_IMPORTS:=false}" System:Bootstrap && declare -g -r _PRISM_BOOTSTRAPPED=true diff --git a/lib/io/log.sh b/lib/io/log.sh index d384fae..aa12755 100644 --- a/lib/io/log.sh +++ b/lib/io/log.sh @@ -1,7 +1,7 @@ # Prints debug messages to fd2 # expands escape sequences # depends: $DEBUG == 1|true -IO::log() { +IO:log() { { [[ "$DEBUG" == '1' || "$DEBUG" == "true" ]] && [ -n "$*" ]; } || { true; return; } local line_no="${BASH_LINENO[0]}" i=2 out="(${FUNCNAME[1]}:_LNO_) " # called function while [ -n "${FUNCNAME[$i]}" ]; do # caller functions diff --git a/lib/io/message.sh b/lib/io/message.sh deleted file mode 100644 index 03ff59e..0000000 --- a/lib/io/message.sh +++ /dev/null @@ -1,36 +0,0 @@ -#! /usr/bin/env bash - -: "${MSG_PREFIX_ERR:-[!!!]}" -: "${MSG_PREFIX_WARN:-[!]}" -: "${MSG_PREFIX_INFO:-[*]}" - -# Prints normal user messages (cli) -# $1: Type: e,err, i, info (optional) -# $2: no_nl (optional) -# $2-$X: Text -IO:message() { - local fd_out='1' prefix output newline=true - [ -n "$*" ] || return - case $1 in - (e|err|error) - prefix="$MSG_PREFIX_ERR" - fd_out=2; shift ;; - (w|warn|warning) prefix="$MSG_PREFIX_WARN"; shift ;; - (i|info) prefix="$MSG_PREFIX_INFO"; shift ;; - esac - [ "$1" == 'no_nl' ] && { newline=false; shift; } - - local first_line=true - for msg in "$@"; do - [ "${#prefix}" -gt 0 ] && [ "$newline" == "true" ] \ - && [ "$first_line" == "false" ] && { - for (( i=0; i<${#prefix}; i++ )); do output+=" "; done - } - output+="$msg\n" - first_line=false - done - - [ "$no_newline" = true ] && output="${output//'\n'/}\n" - [ "$fd_out" = '2' ] && printf "%s%b" "$prefix" "$output" >&2 && return - printf "%s%b" "$prefix" "$output" -} diff --git a/lib/io/print_spaces.sh b/lib/io/print_spaces.sh index 3790e12..023249b 100644 --- a/lib/io/print_spaces.sh +++ b/lib/io/print_spaces.sh @@ -1,4 +1,4 @@ -IO::print_spaces() { +IO:print_spaces() { local howMany="${1:?specifiy a count}" [[ "$howMany" =~ ^[0-9]*$ && "$howMany" -gt 0 ]] || return ( printf "%*s" "$howMany" ) diff --git a/lib/util/command.sh b/lib/util/command.sh index e1ba46a..666dd59 100644 --- a/lib/util/command.sh +++ b/lib/util/command.sh @@ -1,17 +1,17 @@ # no dependencies -Command::get_type() { +Command:get_type() { local name="${1:?no name specified}" local _type=$(type -t "$name" 2> /dev/null || true) printf "%s" "$_type" } -Command::exists(){ +Command:exists(){ local _type=$(Command::get_type "$1") [[ "$_type" == "alias" || "$_type" == "function" || "$_type" == "builtin" ]] } -Command::is_alias(){ +Command:is_alias(){ local _type=$(Command::get_type "$1") [[ "$_type" == "alias" ]] } diff --git a/lib/util/function/copy.sh b/lib/util/function/copy.sh index 87b138f..d65c098 100644 --- a/lib/util/function/copy.sh +++ b/lib/util/function/copy.sh @@ -1,6 +1,6 @@ import util/slice -Function::copy() { +Function:copy() { declare -F $1 > /dev/null || return eval "$(printf "%s()\n" "$2"; declare -f $1 | Util::slice -1)" } diff --git a/lib/util/function/exists.sh b/lib/util/function/exists.sh index 9c0c4d2..57714a4 100644 --- a/lib/util/function/exists.sh +++ b/lib/util/function/exists.sh @@ -1,4 +1,4 @@ -Function::exists() { +Function:exists() { local name="${1:?no function name provided}" declare -f "$name" &> /dev/null } diff --git a/lib/util/function/inject_code.sh b/lib/util/function/inject_code.sh index 31961b1..8ba8ced 100644 --- a/lib/util/function/inject_code.sh +++ b/lib/util/function/inject_code.sh @@ -1,4 +1,4 @@ -Function::inject_code() { +Function:inject_code() { local functionName="${1:?no function name provided}" local injectBefore="${2:-}" local injectAfter="${3:-}" diff --git a/lib/util/function/list.sh b/lib/util/function/list.sh index 08bc5b9..7214f48 100644 --- a/lib/util/function/list.sh +++ b/lib/util/function/list.sh @@ -1,5 +1,5 @@ # Returns a list of declared functions separated by a new line -Function::list() { +Function:list() { local line [ -n "$1" ] && { builtin compgen -A 'function' "$1"; return; } while read -r line; do diff --git a/lib/util/function/rename.sh b/lib/util/function/rename.sh index 08b8b6d..b7e05bb 100644 --- a/lib/util/function/rename.sh +++ b/lib/util/function/rename.sh @@ -1,6 +1,6 @@ import util/function/copy -Function::rename() { +Function:rename() { Function::copy "$1" "$2" \ && unset -f "$1" } diff --git a/lib/util/slice.sh b/lib/util/slice.sh index 6d68114..1e80968 100644 --- a/lib/util/slice.sh +++ b/lib/util/slice.sh @@ -4,7 +4,7 @@ # returns nothing, if delimiter does not exist in stdin # $2: n # $3: c (delimiter) -Util::slice() { +Util:slice() { local n="${1:-0}" c="${2:-$'\n'}" arr=() [[ "$n" =~ ^\-?[0-9]*$ ]] || return diff --git a/lib/util/uuid_generate.sh b/lib/util/uuid_generate.sh index a98fd5d..6611aea 100644 --- a/lib/util/uuid_generate.sh +++ b/lib/util/uuid_generate.sh @@ -1,4 +1,4 @@ -Util::uuid_generate() { +Util:uuid_generate() { ## https://gist.github.com/markusfisch/6110640 local N B C='89ab' diff --git a/lib/var/is_number.sh b/lib/var/is_number.sh index 39c4dae..e133752 100644 --- a/lib/var/is_number.sh +++ b/lib/var/is_number.sh @@ -1,4 +1,4 @@ -Var::is_number() { +Var:is_number() { local input="${1:?no input}" local regex='^-?[0-9]+([.][0-9]+)?$' [[ "$input" =~ $regex ]] || return diff --git a/lib/var/sanitize.sh b/lib/var/sanitize.sh index 6a1e0ac..99b9786 100644 --- a/lib/var/sanitize.sh +++ b/lib/var/sanitize.sh @@ -1,4 +1,4 @@ -Var::sanitize() { +Var:sanitize() { local type="${1:?no input}" printf "%s" "${type//[^a-zA-Z0-9]/_}" } diff --git a/lib/var/slash_replacement.sh b/lib/var/slash_replacement.sh index 2f36def..b6b0c9f 100644 --- a/lib/var/slash_replacement.sh +++ b/lib/var/slash_replacement.sh @@ -1,13 +1,13 @@ # Workaround for a Bash bug that causes # string replacement to fail when a \ is in the string -Var::slashes_replace() { +Var:slashes_replace() { local stringToMark="${1:?no input}" slash="\\" local slashReplacement='_%SLASH%_' printf "%s" "${stringToMark/$slash$slash/$slashReplacement}" } -Var::slashes_restore() { +Var:slashes_restore() { local stringToMark="${1:?no input}" slash="\\" local slashReplacement='_%SLASH%_' printf "%s" "${stringToMark/$slashReplacement/$slash}" diff --git a/your-script.sh b/your-script.sh index 4b9a9a0..9d3a446 100755 --- a/your-script.sh +++ b/your-script.sh @@ -6,7 +6,7 @@ readonly workdir="${workdir%/*}" . "$workdir"/lib/bootstrap.sh ## MAIN EXAMPLES ## -import util/log util/msg util/function/list util/print_spaces +import io/msg util/function/list # Files not in lib/ can also be imported: # import my_file.sh @@ -18,8 +18,9 @@ import util/log util/msg util/function/list util/print_spaces ## YOUR CODE GOES HERE ## -Util::log "imports:\n" "${_VOID_IMPORTS[@]}" +IO:msg i "Hello, dear!\n" \ + "IMPORTS:" \ + "${_PRISM_IMPORTS[@]}\n" \ + "Currently declared functions:" -Function::list - -Util::print_spaces 15 +Function:list |