#! /usr/bin/env sh # ╭──────╮ # │ main │ # ╰──────╯ # main module # ╭──────┬───────────╮ # │ main │ constants │ # ╰──────┴───────────╯ # name of the entrypoint file RWX_MAIN_NAME="main.sh" # name of the project itself RWX_SELF_NAME="rwx" # ╭──────┬───────────╮ # │ main │ variables │ # ╰──────┴───────────╯ # cache of all sourced code modules _rwx_code="" # TODO variablize # system root directory of the project RWX_ROOT_SYSTEM="/usr/local/lib/${RWX_SELF_NAME}" # ╭──────┬──────╮ # │ main │ find │ # ╰──────┴──────╯ # find directory’s files by extension #| find #| sort rwx_find_extension() { local extension="${1}" local root="${2}" local file="${3}" set -- \ "${root}" \ -name "*.${extension}" \ -type "f" [ -n "${file}" ] && set -- "${@}" \ -not \ -name "${file}" find "${@}" \ -printf "%P\n" | sort } # find directory’s sh files rwx_find_shell() { rwx_find_extension "sh" "${@}" } # ╭──────┬───────╮ # │ main │ shell │ # ╰──────┴───────╯ # test if active shell is in interactive mode rwx_shell_interactive() { case "${-}" in *i*) ;; *) return 1 ;; esac } # ╭──────┬─────╮ # │ main │ log │ # ╰──────┴─────╯ _rwx_main_log() { if rwx_shell_interactive; then [ ${#} -gt 0 ] || set -- "" local line for line in "${@}"; do echo "${line}" done fi } # ╭──────┬────────╮ # │ main │ source │ # ╰──────┴────────╯ # source code from root path but file rwx_main_source() { local root="${1}" [ -d "${root}" ] || return 1 local file="${2}" local count module modules count=0 _rwx_main_log "" \ ". ${root}" modules="$(rwx_find_shell "${root}" "${file}")" while IFS= read -r module; do count=$((count + 1)) _rwx_main_log "$(printf "%02d" "${count}") ${module%.sh}" # shellcheck disable=SC1090 . "${root}/${module}" # cache code rwx_main_cache "${root}" "${module}" done <