Read Write eXecute framework
rwx.rwx.work
1# ╭──────╮
2# │ code │
3# ╰──────╯
4
5# ╭──────┬───────────╮
6# │ code │ variables │
7# ╰──────┴───────────╯
8
9# user root directory of the project
10rwx_code_root="${HOME}/${RWX_MAIN_NAME}"
11
12# main modules code cache
13rwx_code_cache_main=""
14# user modules code cache
15rwx_code_cache_user=""
16# main modules names
17rwx_code_modules_main=""
18# user modules names
19rwx_code_modules_user=""
20
21# cache for the parsing awk script
22_rwx_code_awk="$(cat "${rwx_main_root}/core/code.awk")"
23# cache for code aliases
24_rwx_code_aliases=""
25# cache for code aliases functions
26_rwx_code_aliases_functions=""
27# cache for code binaries
28_rwx_code_binaries=""
29# cache for code commands
30_rwx_code_commands=""
31# cache for code commands functions
32_rwx_code_commands_functions=""
33# cache for code constants
34_rwx_code_constants=""
35# cache for code functions
36_rwx_code_functions=""
37# cache for code tasks
38_rwx_code_tasks=""
39_rwx_code_tasks_fixme=""
40_rwx_code_tasks_todo=""
41# cache for code variables
42_rwx_code_variables=""
43
44# ╭──────┬───────────╮
45# │ code │ functions │
46# ╰──────┴───────────╯
47
48# show the cached awk script
49rwx_code_awk() {
50 echo "${_rwx_code_awk}"
51}
52
53# show the cached aliases
54#= rca
55rwx_code_aliases() {
56 echo "${_rwx_code_aliases}"
57}
58
59# show the cached aliases and functions
60#= rcaf
61rwx_code_aliases_functions() {
62 echo "${_rwx_code_aliases_functions}"
63}
64
65# show the cached binaries
66rwx_code_binaries() {
67 echo "${_rwx_code_binaries}"
68}
69
70# output cached main code
71rwx_code_cache_main() {
72 echo "${rwx_code_cache_main}"
73}
74
75# output cached user code
76rwx_code_cache_user() {
77 echo "${rwx_code_cache_user}"
78}
79
80# show the cached commands
81rwx_code_commands() {
82 echo "${_rwx_code_commands}"
83}
84
85# show the cached commands and functions
86#= rccf
87rwx_code_commands_functions() {
88 echo "${_rwx_code_commands_functions}"
89}
90
91# show the cached constants
92#= rcc
93rwx_code_constants() {
94 echo "${_rwx_code_constants}"
95}
96
97# show the cached functions
98#= rcf
99rwx_code_functions() {
100 echo "${_rwx_code_functions}"
101}
102
103# show the cached main modules
104#= rcmm
105rwx_code_modules_main() {
106 echo "${rwx_code_modules_main}"
107}
108
109# show the cached user modules
110#= rcmu
111rwx_code_modules_user() {
112 echo "${rwx_code_modules_user}"
113}
114
115# show the cached tasks
116#= rct
117rwx_code_tasks() {
118 echo "${_rwx_code_tasks}"
119}
120
121# show the cached fixme tasks
122#= rctf
123rwx_code_tasks_fixme() {
124 echo "${_rwx_code_tasks_fixme}"
125}
126
127# show the cached todo tasks
128#= rctt
129rwx_code_tasks_todo() {
130 echo "${_rwx_code_tasks_todo}"
131}
132
133# show the cached variables
134#= rcv
135rwx_code_variables() {
136 echo "${_rwx_code_variables}"
137}
138
139# ╭──────┬───────╮
140# │ code │ cache │
141# ╰──────┴───────╯
142
143# output all cached code
144rwx_code_cache() {
145 rwx_code_cache_main
146 rwx_code_cache_user
147}
148
149# ╭──────┬─────────╮
150# │ code │ install │
151# ╰──────┴─────────╯
152
153#/ rwx_install
154rwx_code_install() {
155 local target="${1}"
156 local command file name root
157 # code
158 if [ -n "${target}" ]; then
159 root="${target}${rwx_main_root}"
160 rwx_remove "${root}"
161 cp --recursive "${rwx_main_root}" "${root}"
162 fi
163 # commands
164 root="${target}/usr/local/bin"
165 name="${RWX_MAIN_NAME}.${RWX_MAIN_EXTENSION}"
166 file="${root}/${name}"
167 rwx_remove "${file}"
168 rwx_link "${file}" "${rwx_main_path}"
169 while IFS= read -r command; do
170 file="${root}/${command}"
171 rwx_remove "${file}"
172 rwx_link "${file}" "${name}"
173 done <<EOF
174${_rwx_code_commands}
175EOF
176 # sh
177 file="${target}/etc/profile.d/${RWX_MAIN_NAME}.${RWX_MAIN_EXTENSION}"
178 rwx_remove "${file}"
179 rwx_file_write "${file}" "\
180export ENV=\"${rwx_main_path}\"
181"
182 # bash
183 file="${target}/etc/bash.bashrc"
184 rwx_remove "${file}"
185 rwx_link "${file}" "${rwx_main_path}"
186}
187
188# ╭──────┬───────╮
189# │ code │ parts │
190# ╰──────┴───────╯
191
192# call awk for action with target
193rwx_code_action_target() {
194 local action="${1}"
195 local target="${2}"
196 [ -n "${action}" ] || return
197 rwx_code_cache |
198 awk \
199 -v action="${action}" \
200 -v target="${target}" \
201 "${_rwx_code_awk}"
202}
203
204# show all the cached main modules
205#= rcm
206rwx_code_modules() {
207 rwx_code_modules_main
208 rwx_code_modules_user
209}
210
211# ╭──────┬───────╮
212# │ code │ parse │
213# ╰──────┴───────╯
214
215# check source code
216rwx_code_check() {
217 # check syntax
218 rwx_log
219 rwx_shellcheck "${rwx_main_root}"
220 rwx_log
221 [ -d "${rwx_code_root}" ] &&
222 rwx_shellcheck "${rwx_code_root}"
223 # check format
224 rwx_log
225 rwx_shfmt "${rwx_main_root}"
226 rwx_log
227 [ -d "${rwx_code_root}" ] &&
228 rwx_shfmt "${rwx_code_root}"
229}
230
231# fetch matching doc for given name
232#= rcd
233rwx_code_doc() {
234 local name="${1}"
235 [ -n "${name}" ] || return
236 rwx_code_action_target "doc" "${name}"
237}
238
239rwx_code_load() {
240 local line
241 # parse functions aliases
242 _rwx_code_aliases_functions="$(rwx_code_action_target "eval" "alias")"
243 while IFS= read -r line; do
244 eval "${line}"
245 done <<EOF
246${_rwx_code_aliases_functions}
247EOF
248 # parse functions commands
249 _rwx_code_commands_functions="$(rwx_code_action_target "eval" "command")"
250 while IFS= read -r line; do
251 eval "${line}"
252 done <<EOF
253${_rwx_code_commands_functions}
254EOF
255 # parse aliases
256 _rwx_code_aliases="$(rwx_code_action_target "filter" "alias" | sort)"
257 # parse binaries
258 _rwx_code_binaries="$(rwx_code_action_target "filter" "binary")"
259 # parse commands
260 _rwx_code_commands="$(rwx_code_action_target "filter" "command")"
261 # parse constants
262 _rwx_code_constants="$(rwx_code_action_target "filter" "constant" | sort)"
263 # parse functions
264 _rwx_code_functions="$(rwx_code_action_target "filter" "function" | sort)"
265 # parse tasks
266 _rwx_code_tasks="$(rwx_code_action_target "tasks")"
267 _rwx_code_tasks_fixme="$(rwx_code_action_target "tasks" "FIXME")"
268 _rwx_code_tasks_todo="$(rwx_code_action_target "tasks" "TODO")"
269 # parse variables
270 _rwx_code_variables="$(rwx_code_action_target "filter" "variable" | sort)"
271}
272
273rwx_code_parse() {
274 local action="${1}"
275 rwx_code_cache |
276 awk \
277 -v action="${action}" \
278 "${_rwx_code_awk}"
279}
280
281# ╭──────┬──────╮
282# │ code │ main │
283# ╰──────┴──────╯
284
285rwx_code_main() {
286 local modules_main="${1}"
287 local module
288 rwx_code_modules_main="${modules_main}"
289 # find user modules
290 rwx_code_modules_user="$(rwx_main_find "${rwx_code_root}")"
291 # source user modules
292 [ -n "${rwx_code_modules_user}" ] &&
293 while IFS= read -r module; do
294 # shellcheck disable=SC1090
295 . "${rwx_code_root}/${module}"
296 done <<EOF
297${rwx_code_modules_user}
298EOF
299 # cache main modules
300 while IFS= read -r module; do
301 # cache main module
302 rwx_code_cache_main="${rwx_code_cache_main}\
303#. ${module}
304$(cat "${rwx_main_root}/${module}")
305"
306 done <<EOF
307${modules_main}
308EOF
309 # cache user modules
310 [ -n "${rwx_code_modules_user}" ] &&
311 while IFS= read -r module; do
312 # cache user module
313 rwx_code_cache_user="${rwx_code_cache_user}\
314#. ${module}
315$(cat "${rwx_code_root}/${module}")
316"
317 done <<EOF
318${rwx_code_modules_user}
319EOF
320 # load code cache
321 rwx_code_load
322}