rwx/sh/self.sh

163 lines
3.5 KiB
Bash
Raw Permalink Normal View History

2025-01-13 06:02:35 +00:00
# meta doc
rwx_doc() {
local name="${1}"
[ -n "${name}" ] || return
local doc line module
rwx_ifs_set
for module in $(rwx_find_shell "${RWX_ROOT_SYSTEM}"); do
while read -r line; do
case "${line}" in
2025-01-13 06:44:14 +00:00
"#"*) doc="${doc}${line}" ;;
"${name}() {")
echo "${doc}"
return
;;
*) doc="" ;;
2025-01-13 06:02:35 +00:00
esac
2025-01-13 06:44:14 +00:00
done <"${RWX_ROOT_SYSTEM}/${module}"
2025-01-13 06:02:35 +00:00
done
rwx_ifs_unset
}
2024-12-22 22:51:32 +00:00
# ╭──────┬───────╮
# │ self │ check │
# ╰──────┴───────╯
# check source code
rwx_self_check() {
# check format
rwx_log
rwx_shfmt "${RWX_ROOT_SYSTEM}"
# check syntax
rwx_log
rwx_shellcheck "${RWX_ROOT_SYSTEM}"
}
2024-12-02 14:24:27 +00:00
# ╭──────┬──────────╮
# │ self │ commands │
# ╰──────┴──────────╯
2024-12-01 20:22:35 +00:00
# get commands from root
rwx_self_commands() {
grep \
--directories "recurse" \
--no-filename \
2024-12-01 20:48:04 +00:00
"^${RWX_SELF_COMMAND}" "${RWX_ROOT_SYSTEM}" |
2024-12-01 20:22:35 +00:00
cut --delimiter "(" --fields 1 |
2024-12-01 20:48:04 +00:00
sed "s|^${RWX_SELF_COMMAND}||"
2024-12-01 20:22:35 +00:00
}
2024-12-02 14:24:27 +00:00
# ╭──────┬───────────╮
# │ self │ functions │
# ╰──────┴───────────╯
2024-12-01 20:22:35 +00:00
# get functions from root
2024-11-29 23:10:16 +00:00
rwx_self_functions() {
grep \
--directories "recurse" \
--no-filename \
"()" "${RWX_ROOT_SYSTEM}" |
cut --delimiter "(" --fields 1
}
2024-12-02 14:24:27 +00:00
# ╭──────┬──────╮
# │ self │ help │
# ╰──────┴──────╯
2024-11-29 21:54:00 +00:00
# output help message
rwx_self_help() {
rwx_log \
"rwx_… = functions" \
" a__… = aliases" \
" u__… = user"
}
2024-12-01 17:58:27 +00:00
2024-12-02 14:24:27 +00:00
# ╭──────┬──────╮
# │ self │ init │
# ╰──────┴──────╯
rwx_self_init() {
# run interactive extras
if rwx_shell_interactive; then
# help
rwx_log
rwx_self_help
fi
}
# ╭──────┬─────────╮
# │ self │ install │
# ╰──────┴─────────╯
2024-12-01 21:28:22 +00:00
_rwx_cmd_rwx_install() { rwx_self_install "${@}"; }
2024-12-01 17:58:27 +00:00
rwx_self_install() {
local target="${1}"
2024-12-08 21:37:36 +00:00
local command file root
# code
2025-01-12 16:04:28 +00:00
if [ -n "${target}" ]; then
root="${target}${RWX_ROOT_SYSTEM}"
rwx_remove "${root}"
cp --recursive "${RWX_ROOT_SYSTEM}" "${root}"
fi
2024-12-01 20:37:55 +00:00
# commands
2024-12-08 21:37:36 +00:00
root="${target}/usr/local/bin"
2024-12-01 20:37:55 +00:00
for command in $(rwx_self_commands); do
file="${root}/${command}"
rwx_remove "${file}"
rwx_link "${file}" "${RWX_MAIN_PATH}"
done
2024-12-01 17:58:27 +00:00
# sh
file="${target}/etc/profile.d/${RWX_SELF_NAME}.sh"
2024-12-01 18:06:44 +00:00
rwx_remove "${file}"
2024-12-08 20:57:14 +00:00
rwx_file_write "${file}" "\
export ENV=\"${RWX_MAIN_PATH}\"
"
2024-12-01 17:58:27 +00:00
# bash
2024-12-08 20:51:24 +00:00
file="${target}/etc/bash.bashrc"
2024-12-01 18:06:44 +00:00
rwx_remove "${file}"
rwx_link "${file}" "${RWX_MAIN_PATH}"
2024-12-01 17:58:27 +00:00
}
2024-12-01 23:06:42 +00:00
2024-12-02 14:24:27 +00:00
# ╭──────┬────────╮
# │ self │ subset │
# ╰──────┴────────╯
2024-12-01 23:06:42 +00:00
rwx_self_subset() {
local argument path
for argument in "${@}"; do
path="${RWX_ROOT_SYSTEM}/${argument}"
if [ -d "${path}" ]; then
2024-12-01 23:30:01 +00:00
local file
for file in $(rwx_find_shell "${path}"); do
echo "${argument}/${file}"
done
2024-12-01 23:06:42 +00:00
elif [ -f "${path}" ]; then
echo "${argument}"
fi
done
}
2024-12-02 14:24:27 +00:00
# ╭──────┬───────╮
# │ self │ write │
# ╰──────┴───────╯
2024-12-01 23:06:42 +00:00
rwx_self_write() {
local target="${1}"
if [ -n "${target}" ]; then
shift
local file text
text="#! /usr/bin/env sh
"
rwx_ifs_set
for file in $(rwx_self_subset "${@}"); do
text="${text}
2024-12-01 23:30:01 +00:00
$(cat "${RWX_ROOT_SYSTEM}/${file}")
"
2024-12-01 23:06:42 +00:00
done
rwx_ifs_unset
rwx_file_write "${target}" "${text}"
2024-12-01 23:39:16 +00:00
rwx_shfmt "${target}"
2024-12-01 23:46:30 +00:00
rwx_shellcheck_file "${target}"
2024-12-01 23:06:42 +00:00
fi
}