core/code
This commit is contained in:
parent
71a1ba6516
commit
bf0e3943d8
2 changed files with 0 additions and 0 deletions
190
sh/core/code.awk
Normal file
190
sh/core/code.awk
Normal file
|
@ -0,0 +1,190 @@
|
|||
function append(line) {
|
||||
if (doc) {
|
||||
doc = doc "\n"
|
||||
}
|
||||
doc = doc line
|
||||
}
|
||||
|
||||
function eval(alias, target) {
|
||||
print alias "() { " target " \"${@}\"; }"
|
||||
}
|
||||
|
||||
function output(name, type) {
|
||||
print "↙ " type
|
||||
print name
|
||||
print doc
|
||||
exit
|
||||
}
|
||||
|
||||
function reset() {
|
||||
if (f == "") {
|
||||
doc = ""
|
||||
}
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
RE_ANY = "(.*)"
|
||||
RE_BEGIN = "^"
|
||||
RE_CONST = "([_A-Z][_0-9A-Z]*)"
|
||||
RE_SET = "=.*"
|
||||
RE_SPACE = "[[:space:]]"
|
||||
RE_VAR = "([_a-z][_0-9a-z]*)"
|
||||
|
||||
RE_SPACES = RE_SPACE "*"
|
||||
|
||||
RE_END = RE_SPACES "$"
|
||||
RE_FUNC = RE_SPACES "\\(" RE_SPACES "\\)" RE_SPACES "{"
|
||||
|
||||
RE_ALIAS = RE_BEGIN "#=" RE_SPACES RE_VAR RE_END
|
||||
RE_BINARY = RE_BEGIN "#\\|" RE_SPACES RE_VAR RE_END
|
||||
RE_CLOSE = RE_BEGIN "}" RE_SPACES RE_END
|
||||
RE_COMMAND = RE_BEGIN "#/" RE_SPACES RE_VAR RE_END
|
||||
RE_CONSTANT = RE_BEGIN RE_CONST RE_SET RE_END
|
||||
RE_DOC = RE_BEGIN RE_SPACES "#" RE_SPACE RE_ANY RE_END
|
||||
RE_FUNCTION = RE_BEGIN RE_VAR RE_FUNC RE_END
|
||||
RE_MODULE = RE_BEGIN "#." RE_SPACES RE_ANY RE_END
|
||||
RE_SHEBANG = RE_BEGIN "#!" RE_SPACES RE_ANY RE_END
|
||||
RE_VARIABLE = RE_BEGIN RE_VAR RE_SET RE_END
|
||||
|
||||
f = ""
|
||||
match_alias = 0
|
||||
match_command = 0
|
||||
match_module = 0
|
||||
reset()
|
||||
}
|
||||
|
||||
{
|
||||
if (action == "aliases") {
|
||||
if (match($0, RE_ALIAS, m)) {
|
||||
print m[1]
|
||||
}
|
||||
} else if (action == "binaries") {
|
||||
if (match($0, RE_BINARY, m)) {
|
||||
binaries[m[1]] = ""
|
||||
}
|
||||
} else if (action == "commands") {
|
||||
if (match($0, RE_COMMAND, m)) {
|
||||
print m[1]
|
||||
}
|
||||
} else if (action == "constants") {
|
||||
if (match($0, RE_CONSTANT, m)) {
|
||||
print m[1]
|
||||
}
|
||||
} else if (action == "functions") {
|
||||
if (match($0, RE_FUNCTION, m)) {
|
||||
print m[1]
|
||||
}
|
||||
} else if (action == "variables") {
|
||||
if (match($0, RE_VARIABLE, m)) {
|
||||
print m[1]
|
||||
}
|
||||
} else if (action == "aliases functions") {
|
||||
if (match($0, RE_ALIAS, m)) {
|
||||
append(m[1])
|
||||
} else if (match($0, RE_FUNCTION, m)) {
|
||||
split(doc, array, "\n")
|
||||
for (item in array) {
|
||||
eval(array[item], m[1])
|
||||
}
|
||||
reset()
|
||||
} else {
|
||||
reset()
|
||||
}
|
||||
} else if (action == "commands functions") {
|
||||
if (match($0, RE_COMMAND, m)) {
|
||||
append(m[1])
|
||||
} else if (match($0, RE_FUNCTION, m)) {
|
||||
split(doc, array, "\n")
|
||||
for (item in array) {
|
||||
eval(array[item], m[1])
|
||||
}
|
||||
reset()
|
||||
} else {
|
||||
reset()
|
||||
}
|
||||
} else if (action == "command function") {
|
||||
if (match($0, RE_COMMAND, m)) {
|
||||
append(m[1])
|
||||
} else if (match($0, RE_FUNCTION, m)) {
|
||||
split(doc, array, "\n")
|
||||
for (item in array) {
|
||||
if (array[item] == target) {
|
||||
print m[1]
|
||||
exit
|
||||
}
|
||||
}
|
||||
reset()
|
||||
} else {
|
||||
reset()
|
||||
}
|
||||
} else if (action == "doc") {
|
||||
# doc
|
||||
if (match($0, RE_SHEBANG, m)) {
|
||||
append("! " m[1])
|
||||
} else if (match($0, RE_DOC, m)) {
|
||||
if (f) {
|
||||
append($0)
|
||||
} else {
|
||||
append(m[1])
|
||||
}
|
||||
} else if (match($0, RE_ALIAS, m)) {
|
||||
append("= " m[1])
|
||||
if (m[1] == target) {
|
||||
match_alias = 1
|
||||
}
|
||||
} else if (match($0, RE_COMMAND, m)) {
|
||||
append("/ " m[1])
|
||||
if (m[1] == target) {
|
||||
match_command = 1
|
||||
}
|
||||
# set
|
||||
} else if (match($0, RE_CONSTANT, m)) {
|
||||
if (m[1] == target) {
|
||||
output(m[1], "constant")
|
||||
} else {
|
||||
reset()
|
||||
}
|
||||
} else if (match($0, RE_VARIABLE, m)) {
|
||||
if (m[1] == target) {
|
||||
output(m[1], "variable")
|
||||
} else {
|
||||
reset()
|
||||
}
|
||||
# others
|
||||
} else if (match($0, RE_MODULE, m)) {
|
||||
reset()
|
||||
if (m[1] == target) {
|
||||
match_module = 1
|
||||
}
|
||||
} else if (match($0, RE_FUNCTION, m)) {
|
||||
f = m[1]
|
||||
} else if (match($0, RE_CLOSE, m)) {
|
||||
if (match_alias) {
|
||||
print "= " target
|
||||
output(f, "function")
|
||||
} else if (match_command) {
|
||||
print "/ " target
|
||||
output(f, "function")
|
||||
} else if (f == target) {
|
||||
output(f, "function")
|
||||
} else {
|
||||
f = ""
|
||||
reset()
|
||||
}
|
||||
} else {
|
||||
if (match_module) {
|
||||
output(target, "module")
|
||||
} else {
|
||||
reset()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
END {
|
||||
if (action == "binaries") {
|
||||
for (binary in binaries) {
|
||||
print binary
|
||||
}
|
||||
}
|
||||
}
|
256
sh/core/code.sh
Normal file
256
sh/core/code.sh
Normal file
|
@ -0,0 +1,256 @@
|
|||
# ╭──────╮
|
||||
# │ code │
|
||||
# ╰──────╯
|
||||
|
||||
# ╭──────┬───────────╮
|
||||
# │ code │ variables │
|
||||
# ╰──────┴───────────╯
|
||||
|
||||
# TODO variablize
|
||||
# path to the entrypoint main file of the project
|
||||
RWX_MAIN_PATH="${RWX_ROOT_SYSTEM}/${RWX_MAIN_NAME}"
|
||||
# user root directory of the project
|
||||
RWX_SELF_USER="${HOME}/${RWX_SELF_NAME}"
|
||||
|
||||
# cache for the parsing awk script
|
||||
_rwx_code_awk="$(cat "${RWX_ROOT_SYSTEM}/core/code.awk")"
|
||||
# cache for code aliases
|
||||
_rwx_code_aliases=""
|
||||
# cache for code aliases functions
|
||||
_rwx_code_aliases_functions=""
|
||||
# cache for code binaries
|
||||
_rwx_code_binaries=""
|
||||
# cache for code commands
|
||||
_rwx_code_commands=""
|
||||
# cache for code commands functions
|
||||
_rwx_code_commands_functions=""
|
||||
# cache for code constants
|
||||
_rwx_code_constants=""
|
||||
# cache for code functions
|
||||
_rwx_code_functions=""
|
||||
# cache for code variables
|
||||
_rwx_code_variables=""
|
||||
|
||||
# ╭──────┬──────╮
|
||||
# │ code │ help │
|
||||
# ╰──────┴──────╯
|
||||
|
||||
# output help message
|
||||
rwx_code_help() {
|
||||
rwx_log \
|
||||
"rwx_… = functions" \
|
||||
" a__… = aliases" \
|
||||
" u__… = user"
|
||||
}
|
||||
|
||||
# ╭──────┬─────────╮
|
||||
# │ code │ install │
|
||||
# ╰──────┴─────────╯
|
||||
|
||||
#/ rwx_install
|
||||
rwx_code_install() {
|
||||
local target="${1}"
|
||||
local command file name root
|
||||
# code
|
||||
if [ -n "${target}" ]; then
|
||||
root="${target}${RWX_ROOT_SYSTEM}"
|
||||
rwx_remove "${root}"
|
||||
cp --recursive "${RWX_ROOT_SYSTEM}" "${root}"
|
||||
fi
|
||||
# commands
|
||||
root="${target}/usr/local/bin"
|
||||
name="${RWX_SELF_NAME}.sh"
|
||||
file="${root}/${name}"
|
||||
rwx_remove "${file}"
|
||||
rwx_link "${file}" "${RWX_MAIN_PATH}"
|
||||
while IFS= read -r command; do
|
||||
file="${root}/${command}"
|
||||
rwx_remove "${file}"
|
||||
rwx_link "${file}" "${name}"
|
||||
done <<EOF
|
||||
${_rwx_code_commands}
|
||||
EOF
|
||||
# sh
|
||||
file="${target}/etc/profile.d/${RWX_SELF_NAME}.sh"
|
||||
rwx_remove "${file}"
|
||||
rwx_file_write "${file}" "\
|
||||
export ENV=\"${RWX_MAIN_PATH}\"
|
||||
"
|
||||
# bash
|
||||
file="${target}/etc/bash.bashrc"
|
||||
rwx_remove "${file}"
|
||||
rwx_link "${file}" "${RWX_MAIN_PATH}"
|
||||
}
|
||||
|
||||
# ╭──────┬───────╮
|
||||
# │ code │ parts │
|
||||
# ╰──────┴───────╯
|
||||
|
||||
# show the cached code
|
||||
#= rc
|
||||
rwx_code() {
|
||||
echo "${_rwx_code}"
|
||||
}
|
||||
|
||||
# show the cached awk script
|
||||
rwx_code_awk() {
|
||||
echo "${_rwx_code_awk}"
|
||||
}
|
||||
|
||||
# show the cached aliases
|
||||
#= rca
|
||||
rwx_code_aliases() {
|
||||
echo "${_rwx_code_aliases}"
|
||||
}
|
||||
|
||||
# find command function
|
||||
rwx_code_command_function() {
|
||||
local name="${1}"
|
||||
[ -n "${name}" ] || return
|
||||
rwx_code |
|
||||
awk \
|
||||
-v action="command function" \
|
||||
-v target="${name}" \
|
||||
"${_rwx_code_awk}"
|
||||
}
|
||||
|
||||
# show the cached aliases and functions
|
||||
#= rcaf
|
||||
rwx_code_aliases_functions() {
|
||||
echo "${_rwx_code_aliases_functions}"
|
||||
}
|
||||
|
||||
# show the cached binaries
|
||||
rwx_code_binaries() {
|
||||
echo "${_rwx_code_binaries}"
|
||||
}
|
||||
|
||||
# show the cached commands
|
||||
rwx_code_commands() {
|
||||
echo "${_rwx_code_commands}"
|
||||
}
|
||||
|
||||
# show the cached commands and functions
|
||||
#= rccf
|
||||
rwx_code_commands_functions() {
|
||||
echo "${_rwx_code_commands_functions}"
|
||||
}
|
||||
|
||||
# show the cached constants
|
||||
#= rcc
|
||||
rwx_code_constants() {
|
||||
echo "${_rwx_code_constants}"
|
||||
}
|
||||
|
||||
# show the cached functions
|
||||
#= rcf
|
||||
rwx_code_functions() {
|
||||
echo "${_rwx_code_functions}"
|
||||
}
|
||||
|
||||
# show the cached variables
|
||||
#= rcv
|
||||
rwx_code_variables() {
|
||||
echo "${_rwx_code_variables}"
|
||||
}
|
||||
|
||||
# ╭──────┬───────╮
|
||||
# │ code │ parse │
|
||||
# ╰──────┴───────╯
|
||||
|
||||
# check source code
|
||||
rwx_code_check() {
|
||||
# check format
|
||||
rwx_log
|
||||
rwx_shfmt "${RWX_ROOT_SYSTEM}"
|
||||
# check syntax
|
||||
rwx_log
|
||||
rwx_shellcheck "${RWX_ROOT_SYSTEM}"
|
||||
}
|
||||
|
||||
# fetch matching doc for given name
|
||||
#= rcd
|
||||
rwx_code_doc() {
|
||||
local name="${1}"
|
||||
[ -n "${name}" ] || return
|
||||
rwx_code |
|
||||
awk \
|
||||
-v action="doc" \
|
||||
-v target="${name}" \
|
||||
"${_rwx_code_awk}"
|
||||
}
|
||||
|
||||
rwx_code_load() {
|
||||
local line text
|
||||
# parse aliases functions
|
||||
_rwx_code_aliases_functions="$(rwx_code_parse "aliases functions")"
|
||||
while IFS= read -r line; do
|
||||
eval "${line}"
|
||||
done <<EOF
|
||||
${_rwx_code_aliases_functions}
|
||||
EOF
|
||||
# parse commands functions
|
||||
_rwx_code_commands_functions="$(rwx_code_parse "commands functions")"
|
||||
while IFS= read -r line; do
|
||||
eval "${line}"
|
||||
done <<EOF
|
||||
${_rwx_code_commands_functions}
|
||||
EOF
|
||||
# parse aliases
|
||||
_rwx_code_aliases="$(rwx_code_parse "aliases")"
|
||||
# parse binaries
|
||||
_rwx_code_binaries="$(rwx_code_parse "binaries")"
|
||||
# parse commands
|
||||
_rwx_code_commands="$(rwx_code_parse "commands")"
|
||||
# parse constants
|
||||
_rwx_code_constants="$(rwx_code_parse "constants")"
|
||||
# parse functions
|
||||
_rwx_code_functions="$(rwx_code_parse "functions")"
|
||||
# parse variables
|
||||
_rwx_code_variables="$(rwx_code_parse "variables")"
|
||||
}
|
||||
|
||||
rwx_code_parse() {
|
||||
local action="${1}"
|
||||
rwx_code |
|
||||
awk \
|
||||
-v action="${action}" \
|
||||
"${_rwx_code_awk}"
|
||||
}
|
||||
|
||||
# ╭──────┬──────╮
|
||||
# │ code │ main │
|
||||
# ╰──────┴──────╯
|
||||
|
||||
rwx_code_main() {
|
||||
# source user root
|
||||
rwx_main_source "${RWX_SELF_USER}"
|
||||
# load code cache
|
||||
rwx_code_load
|
||||
# set 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
|
||||
# context / command
|
||||
if [ -n "${command}" ]; then
|
||||
local function
|
||||
# find the matching function
|
||||
function="$(rwx_code_command_function "${command}")"
|
||||
if [ -n "${function}" ]; then
|
||||
"${function}" "${@}"
|
||||
fi
|
||||
# context / shell
|
||||
else
|
||||
# run interactive extras
|
||||
if rwx_shell_interactive; then
|
||||
# help
|
||||
rwx_log
|
||||
rwx_code_help
|
||||
fi
|
||||
fi
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue