rwx/sh/main.sh
Marc Beninca 58b0919a7c
All checks were successful
/ job (push) Successful in 4m29s
−useless
2025-07-06 22:35:05 +02:00

264 lines
5.9 KiB
Bash
Executable file

#! /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"
# prefix for command functions
RWX_SELF_COMMAND="_${RWX_SELF_NAME}_cmd_"
# ╭──────┬───────────╮
# │ main │ variables │
# ╰──────┴───────────╯
RWX_COMMAND_ARGUMENT="${0}"
# currently running shell name
RWX_SHELL="$(cat "/proc/${$}/comm")"
# command name used to run
# (stripped from hyphen interactive flag)
RWX_COMMAND_NAME="$(basename "${RWX_COMMAND_ARGUMENT}" | sed "s|^-||")"
case "${RWX_COMMAND_NAME}" in
"bash" | "dash" | "sh") unset RWX_COMMAND_NAME ;;
*) ;;
esac
# 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 required parsing awk script
RWX_AWK="${RWX_ROOT_SYSTEM}/${RWX_MAIN_NAME%.sh}.awk"
# 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}"
# parse code cache
rwx_parse_code
# context / command
if [ -n "${RWX_COMMAND_NAME}" ]; then
"${RWX_SELF_COMMAND}${RWX_COMMAND_NAME}" "${@}"
# 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 <<EOF
${modules}
EOF
}
# ╭──────┬─────╮
# │ main │ log │
# ╰──────┴─────╯
__rwx_log() {
if rwx_shell_interactive; then
[ ${#} -gt 0 ] || set -- ""
local line
for line in "${@}"; do
echo "${line}"
done
fi
}
# ╭──────┬───────╮
# │ main │ shell │
# ╰──────┴───────╯
# test if active shell is in interactive mode
rwx_shell_interactive() {
case "${-}" in
*i*) ;;
*) return 1 ;;
esac
}
# ╭──────┬──────╮
# │ main │ find │
# ╰──────┴──────╯
# find directory’s files by extension
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 │ parse │
# ╰──────┴───────╯
RWX_REGEX_CONSTANT="[_A-Z][_0-9A-Z]*"
RWX_REGEX_FUNCTION="[_A-Za-z][_0-9A-Za-z]*"
RWX_REGEX_SET="=.*"
RWX_REGEX_SPACES="[[:space:]]*"
RWX_REGEX_VARIABLE="[_a-z][_0-9a-z]*"
RWX_REGEX_BEGIN="^"
RWX_REGEX_OPEN="\
${RWX_REGEX_SPACES}(${RWX_REGEX_SPACES})${RWX_REGEX_SPACES}{.*"
RWX_REGEX_TARGET_CONSTANT="\
${RWX_REGEX_BEGIN}\\(${RWX_REGEX_CONSTANT}\\)${RWX_REGEX_SET}"
RWX_REGEX_TARGET_FUNCTION="\
${RWX_REGEX_BEGIN}\\(${RWX_REGEX_FUNCTION}\\)${RWX_REGEX_OPEN}"
RWX_REGEX_TARGET_VARIABLE="\
${RWX_REGEX_BEGIN}\\(${RWX_REGEX_VARIABLE}\\)${RWX_REGEX_SET}"
# cache for code variables
_rwx_code_variables=""
rwx_parse_code() {
# parse aliases
local line text
RWX_ALIASES="$(rwx_parse_aliases)"
while IFS= read -r line; do
text="$(echo "${line}" | sed "s| |() { |")"
text="${text} \"\${@}\"; }"
eval "${text}"
done <<EOF
${RWX_ALIASES}
EOF
# parse constants
RWX_CONSTANTS="$(rwx_parse_constants)"
# parse functions
RWX_FUNCTIONS="$(rwx_parse_functions)"
# parse variables
_rwx_code_variables="$(rwx_parse_variables)"
}
rwx_parse_aliases() {
printf "%s" "${_rwx_code}" |
awk \
--assign action="alias" \
--file "${RWX_AWK}"
}
rwx_parse_constants() {
printf "%s" "${_rwx_code}" |
sed --silent "s|${RWX_REGEX_TARGET_CONSTANT}|\\1|p"
}
rwx_parse_functions() {
printf "%s" "${_rwx_code}" |
sed --silent "s|${RWX_REGEX_TARGET_FUNCTION}|\\1|p"
}
rwx_parse_variables() {
printf "%s" "${_rwx_code}" |
sed --silent "s|${RWX_REGEX_TARGET_VARIABLE}|\\1|p"
}
# ╭──────┬──────╮
# │ main │ test │
# ╰──────┴──────╯
#= rt
rwx_test() {
local item
set \
"main" \
"alias/git" \
\
"RWX_MAIN_NAME" \
\
"_rwx_code" \
\
"rwx_cache" \
\
"alias/batcat" \
"b"
for item in "${@}"; do
echo
rwx_doc "${item}"
done
}
# ╭──────┬─────╮
# │ main │ run │
# ╰──────┴─────╯
# run main function
rwx_main "${@}"