rwx/sh/core/code.sh

327 lines
6.9 KiB
Bash
Raw Normal View History

2025-07-06 23:15:48 +02:00
# ╭──────╮
# │ code │
# ╰──────╯
2025-07-07 01:13:06 +02:00
# ╭──────┬───────────╮
# │ code │ variables │
# ╰──────┴───────────╯
2025-07-10 02:09:54 +02:00
# main modules code cache
rwx_code_cache_main=""
# user modules code cache
rwx_code_cache_user=""
2025-07-10 00:44:32 +02:00
# main modules names
rwx_code_modules_main=""
# user modules names
rwx_code_modules_user=""
2025-07-08 04:44:46 +02:00
# user root directory of the project
2025-07-09 17:16:00 +02:00
rwx_code_root="${HOME}/${RWX_MAIN_NAME}"
2025-07-08 04:44:46 +02:00
2025-07-09 07:26:17 +02:00
# cache of all sourced code modules
_rwx_code=""
2025-07-07 02:45:58 +02:00
# cache for the parsing awk script
2025-07-09 16:35:11 +02:00
_rwx_code_awk="$(cat "${rwx_main_root}/core/code.awk")"
2025-07-07 02:45:58 +02:00
# cache for code aliases
_rwx_code_aliases=""
# cache for code aliases functions
_rwx_code_aliases_functions=""
2025-07-08 15:17:05 +02:00
# cache for code binaries
_rwx_code_binaries=""
2025-07-08 00:05:07 +02:00
# cache for code commands
_rwx_code_commands=""
2025-07-08 15:48:13 +02:00
# cache for code commands functions
_rwx_code_commands_functions=""
2025-07-07 02:45:58 +02:00
# cache for code constants
_rwx_code_constants=""
# cache for code functions
_rwx_code_functions=""
# cache for code variables
_rwx_code_variables=""
2025-07-07 01:13:06 +02:00
2025-07-10 02:09:54 +02:00
# ╭──────┬───────╮
# │ code │ cache │
# ╰──────┴───────╯
# output all cached code
rwx_code_cache() {
rwx_code_cache_main
rwx_code_cache_user
}
# output cached main code
rwx_code_cache_main() {
echo "${rwx_code_cache_main}"
}
# output cached user code
rwx_code_cache_user() {
echo "${rwx_code_cache_user}"
}
2025-07-08 06:17:21 +02:00
# ╭──────┬──────╮
# │ code │ help │
# ╰──────┴──────╯
# output help message
rwx_code_help() {
2025-07-09 00:39:58 +02:00
rwx_log "" \
2025-07-08 06:17:21 +02:00
"rwx_… = functions" \
" a__… = aliases" \
" u__… = user"
}
2025-07-07 03:51:14 +02:00
# ╭──────┬─────────╮
# │ code │ install │
# ╰──────┴─────────╯
2025-07-08 00:05:07 +02:00
#/ rwx_install
2025-07-07 03:51:14 +02:00
rwx_code_install() {
local target="${1}"
local command file name root
# code
if [ -n "${target}" ]; then
2025-07-09 16:35:11 +02:00
root="${target}${rwx_main_root}"
2025-07-07 03:51:14 +02:00
rwx_remove "${root}"
2025-07-09 16:35:11 +02:00
cp --recursive "${rwx_main_root}" "${root}"
2025-07-07 03:51:14 +02:00
fi
# commands
root="${target}/usr/local/bin"
2025-07-09 16:15:42 +02:00
name="${RWX_MAIN_NAME}.${RWX_MAIN_EXTENSION}"
2025-07-07 03:51:14 +02:00
file="${root}/${name}"
rwx_remove "${file}"
2025-07-10 01:49:19 +02:00
rwx_link "${file}" "${rwx_main_path}"
2025-07-07 04:02:44 +02:00
while IFS= read -r command; do
2025-07-07 03:51:14 +02:00
file="${root}/${command}"
rwx_remove "${file}"
rwx_link "${file}" "${name}"
2025-07-07 04:02:44 +02:00
done <<EOF
2025-07-08 00:05:07 +02:00
${_rwx_code_commands}
2025-07-07 04:02:44 +02:00
EOF
2025-07-07 03:51:14 +02:00
# sh
2025-07-09 16:15:42 +02:00
file="${target}/etc/profile.d/${RWX_MAIN_NAME}.${RWX_MAIN_EXTENSION}"
2025-07-07 03:51:14 +02:00
rwx_remove "${file}"
rwx_file_write "${file}" "\
2025-07-10 01:49:19 +02:00
export ENV=\"${rwx_main_path}\"
2025-07-07 03:51:14 +02:00
"
# bash
file="${target}/etc/bash.bashrc"
rwx_remove "${file}"
2025-07-10 01:49:19 +02:00
rwx_link "${file}" "${rwx_main_path}"
2025-07-07 03:51:14 +02:00
}
2025-07-07 01:13:06 +02:00
# ╭──────┬───────╮
# │ code │ parts │
# ╰──────┴───────╯
2025-07-06 23:09:41 +02:00
# show the cached code
#= rc
rwx_code() {
echo "${_rwx_code}"
}
2025-07-06 23:10:52 +02:00
2025-07-07 02:45:58 +02:00
# show the cached awk script
rwx_code_awk() {
echo "${_rwx_code_awk}"
}
2025-07-07 02:11:32 +02:00
# show the cached aliases
#= rca
rwx_code_aliases() {
echo "${_rwx_code_aliases}"
}
2025-07-08 15:48:13 +02:00
# find command function
rwx_code_command_function() {
2025-07-08 21:18:50 +02:00
local name="${1}"
[ -n "${name}" ] || return
rwx_code |
awk \
2025-07-08 21:52:56 +02:00
-v action="command function" \
-v target="${name}" \
2025-07-08 21:18:50 +02:00
"${_rwx_code_awk}"
2025-07-08 15:48:13 +02:00
}
2025-07-07 00:29:50 +02:00
# show the cached aliases and functions
#= rcaf
rwx_code_aliases_functions() {
echo "${_rwx_code_aliases_functions}"
2025-07-07 00:17:58 +02:00
}
2025-07-08 15:17:05 +02:00
# show the cached binaries
rwx_code_binaries() {
echo "${_rwx_code_binaries}"
}
2025-07-08 00:05:07 +02:00
# show the cached commands
rwx_code_commands() {
echo "${_rwx_code_commands}"
}
2025-07-08 15:48:13 +02:00
# show the cached commands and functions
#= rccf
rwx_code_commands_functions() {
echo "${_rwx_code_commands_functions}"
}
2025-07-06 23:10:52 +02:00
# show the cached constants
#= rcc
rwx_code_constants() {
echo "${_rwx_code_constants}"
}
2025-07-06 23:11:25 +02:00
# show the cached functions
#= rcf
rwx_code_functions() {
echo "${_rwx_code_functions}"
}
2025-07-06 23:15:48 +02:00
2025-07-10 00:44:32 +02:00
# show all the cached main modules
#= rcm
rwx_code_modules() {
rwx_code_modules_main
rwx_code_modules_user
}
# show the cached main modules
#= rcmm
rwx_code_modules_main() {
echo "${rwx_code_modules_main}"
}
# show the cached user modules
#= rcmu
rwx_code_modules_user() {
echo "${rwx_code_modules_user}"
}
2025-07-07 00:01:06 +02:00
# show the cached variables
#= rcv
rwx_code_variables() {
echo "${_rwx_code_variables}"
}
2025-07-06 23:15:48 +02:00
# ╭──────┬───────╮
2025-07-07 01:13:06 +02:00
# │ code │ parse │
2025-07-06 23:15:48 +02:00
# ╰──────┴───────╯
# check source code
rwx_code_check() {
# check format
rwx_log
2025-07-09 16:35:11 +02:00
rwx_shfmt "${rwx_main_root}"
2025-07-06 23:15:48 +02:00
# check syntax
rwx_log
2025-07-09 16:35:11 +02:00
rwx_shellcheck "${rwx_main_root}"
2025-07-06 23:15:48 +02:00
}
2025-07-07 00:29:50 +02:00
# fetch matching doc for given name
#= rcd
rwx_code_doc() {
local name="${1}"
[ -n "${name}" ] || return
2025-07-07 02:45:58 +02:00
rwx_code |
2025-07-07 00:29:50 +02:00
awk \
2025-07-08 21:52:56 +02:00
-v action="doc" \
-v target="${name}" \
2025-07-07 02:23:26 +02:00
"${_rwx_code_awk}"
2025-07-07 00:29:50 +02:00
}
2025-07-07 02:58:39 +02:00
rwx_code_load() {
2025-07-08 23:43:35 +02:00
local line
2025-07-08 15:54:55 +02:00
# parse aliases functions
2025-07-07 03:07:28 +02:00
_rwx_code_aliases_functions="$(rwx_code_parse "aliases functions")"
2025-07-07 02:58:39 +02:00
while IFS= read -r line; do
2025-07-08 20:37:33 +02:00
eval "${line}"
2025-07-07 02:58:39 +02:00
done <<EOF
${_rwx_code_aliases_functions}
EOF
2025-07-08 15:54:55 +02:00
# parse commands functions
_rwx_code_commands_functions="$(rwx_code_parse "commands functions")"
while IFS= read -r line; do
2025-07-08 20:57:42 +02:00
eval "${line}"
2025-07-08 15:54:55 +02:00
done <<EOF
${_rwx_code_commands_functions}
EOF
# parse aliases
_rwx_code_aliases="$(rwx_code_parse "aliases")"
2025-07-08 15:17:05 +02:00
# parse binaries
_rwx_code_binaries="$(rwx_code_parse "binaries")"
2025-07-08 00:05:07 +02:00
# parse commands
_rwx_code_commands="$(rwx_code_parse "commands")"
2025-07-07 02:58:39 +02:00
# parse constants
2025-07-07 03:07:28 +02:00
_rwx_code_constants="$(rwx_code_parse "constants")"
2025-07-07 02:58:39 +02:00
# parse functions
2025-07-07 03:07:28 +02:00
_rwx_code_functions="$(rwx_code_parse "functions")"
2025-07-07 02:58:39 +02:00
# parse variables
2025-07-07 03:07:28 +02:00
_rwx_code_variables="$(rwx_code_parse "variables")"
2025-07-07 02:58:39 +02:00
}
2025-07-07 03:01:21 +02:00
rwx_code_parse() {
local action="${1}"
rwx_code |
awk \
2025-07-08 21:52:56 +02:00
-v action="${action}" \
2025-07-07 03:01:21 +02:00
"${_rwx_code_awk}"
}
2025-07-08 04:35:32 +02:00
# ╭──────┬──────╮
# │ code │ main │
# ╰──────┴──────╯
rwx_code_main() {
2025-07-09 07:26:17 +02:00
local modules_main="${1}"
2025-07-10 00:20:49 +02:00
local module
2025-07-10 00:44:32 +02:00
rwx_code_modules_main="${modules_main}"
2025-07-10 00:20:49 +02:00
# find user modules
2025-07-10 00:44:32 +02:00
rwx_code_modules_user="$(rwx_main_find "${rwx_code_root}")"
2025-07-10 00:20:49 +02:00
# source user modules
2025-07-09 07:26:17 +02:00
while IFS= read -r module; do
# shellcheck disable=SC1090
2025-07-09 17:16:00 +02:00
. "${rwx_code_root}/${module}.${RWX_MAIN_EXTENSION}"
2025-07-09 07:26:17 +02:00
done <<EOF
2025-07-10 00:44:32 +02:00
${rwx_code_modules_user}
2025-07-09 07:26:17 +02:00
EOF
# cache main modules
while IFS= read -r module; do
# cache main module
_rwx_code="${_rwx_code}#. ${module}
2025-07-09 16:35:11 +02:00
$(cat "${rwx_main_root}/${module}.${RWX_MAIN_EXTENSION}")
2025-07-09 07:26:17 +02:00
"
done <<EOF
${modules_main}
EOF
2025-07-10 00:44:32 +02:00
# cache user modules
2025-07-09 07:26:17 +02:00
while IFS= read -r module; do
2025-07-10 00:44:32 +02:00
# cache user module
2025-07-09 07:26:17 +02:00
_rwx_code="${_rwx_code}#. ${module}
2025-07-09 17:16:00 +02:00
$(cat "${rwx_code_root}/${module}.${RWX_MAIN_EXTENSION}")
2025-07-09 07:26:17 +02:00
"
done <<EOF
2025-07-10 00:44:32 +02:00
${rwx_code_modules_user}
2025-07-09 07:26:17 +02:00
EOF
2025-07-08 04:35:32 +02:00
# load code cache
rwx_code_load
2025-07-08 06:52:28 +02:00
# set command
2025-07-08 04:35:32 +02:00
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
2025-07-09 20:42:41 +02:00
# context / shell
if rwx_shell_interactive; then
# display help
rwx_code_help
2025-07-08 06:52:28 +02:00
# context / command
2025-07-09 20:42:41 +02:00
else
2025-07-08 04:35:32 +02:00
local function
# find the matching function
2025-07-08 15:49:19 +02:00
function="$(rwx_code_command_function "${command}")"
2025-07-08 04:35:32 +02:00
if [ -n "${function}" ]; then
"${function}" "${@}"
fi
fi
}