#! /usr/bin/env sh # ╭──────╮ # │ main │ # ╰──────╯ # main module # * builtins # * echo # * printf # * read # * binaries # * awk # * cat # * find # * sed # ╭──────┬───────────╮ # │ main │ constants │ # ╰──────┴───────────╯ # name of the entrypoint file RWX_MAIN_NAME="main.sh" # name of the project itself RWX_SELF_NAME="rwx" # ╭──────┬───────────╮ # │ main │ variables │ # ╰──────┴───────────╯ # system root directory of the project RWX_ROOT_SYSTEM="/usr/local/lib/${RWX_SELF_NAME}" # user root directory of the project RWX_SELF_USER="${HOME}/${RWX_SELF_NAME}" # path to the entrypoint main file of the project RWX_MAIN_PATH="${RWX_ROOT_SYSTEM}/${RWX_MAIN_NAME}" # ╭──────┬──────╮ # │ main │ main │ # ╰──────┴──────╯ # run initial steps rwx_main() { # cache main rwx_cache "${RWX_ROOT_SYSTEM}" "${RWX_MAIN_NAME}" # source system root if ! rwx_source "${RWX_ROOT_SYSTEM}" "${RWX_MAIN_NAME}"; then __rwx_log "Not a directory: ${RWX_ROOT_SYSTEM}" return 1 fi # source user root rwx_source "${RWX_SELF_USER}" # load code cache rwx_code_load # context / command local command # command name used to run # (stripped from hyphen interactive flag) command="$(basename "${0}" | sed "s|^-||")" case "${command}" in "bash" | "dash" | "sh") unset command ;; *) ;; esac if [ -n "${command}" ]; then local function # find the matching function function="$(rwx_code_alias_function "${command}")" if [ -n "${function}" ]; then "${function}" "${@}" fi # context / shell else rwx_self_init fi } # ╭──────┬───────╮ # │ main │ cache │ # ╰──────┴───────╯ # cache of all sourced code modules _rwx_code="" # cache source code of a module # inside a global code variable rwx_cache() { local root="${1}" local module="${2}" local name="${module%.sh}" local path="${root}/${module}" local text text="$(cat "${path}")" # all source code _rwx_code="${_rwx_code}\ #. ${name} ${text} " } # ╭──────┬────────╮ # │ main │ source │ # ╰──────┴────────╯ # source code from file path rwx_source() { local root="${1}" [ -d "${root}" ] || return 1 local file="${2}" local count module modules count=0 __rwx_log "" \ ". ${root}" modules="$(rwx_find_shell "${root}" "${file}")" while IFS= read -r module; do count=$((count + 1)) __rwx_log "$(printf "%02d" "${count}") ${module%.sh}" # shellcheck disable=SC1090 . "${root}/${module}" # cache code rwx_cache "${root}" "${module}" done <