Skip to content

Commit e4cab9a

Browse files
committed
consolidate regex into single-source-of-truth versions of each
1 parent 1c97dac commit e4cab9a

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

runfile.sh

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ function version() {
1010
head -n 3 < "$0" | tail -1 | cut -c3-
1111
}
1212

13+
function task_re() {
14+
printf '%s' '([[:alnum:]_-][[:alnum:] _-]+):([[:alnum:] _-]*)?(#(.*))?'
15+
}
16+
17+
function subtask_re() {
18+
[[ -z "${RUNFILE_SKIP_SUBTASKS:-}" ]] && printf '%s' '\2'
19+
}
20+
21+
function trap_re() {
22+
printf '%s' '(EXIT|HUP|INT|QUIT|ABRT|KILL|ALRM|TERM)'
23+
}
24+
25+
function runfile_variable_re() {
26+
printf '%s' '^[[:space:]]*[A-Z]+ :*= '
27+
}
28+
29+
function run_arg_re() {
30+
printf '%s' '-([hvrmena]|-(help|version|runfile|makefile|edit|new|alias|eject|verbose|compact|compat|noconfirm|noedit))'
31+
}
32+
1333
function usage() {
1434
cat <<EOF
1535
@@ -166,12 +186,11 @@ function print-makefile() {
166186
sed -E "s!\tmake --makefile ${makefile} !\tmake !" "$@"
167187
}
168188

169-
function print-runfile-commands() { local task_re
170-
task_re='([[:alnum:]_-][[:alnum:] _-]+):[[:alnum:] _-]*(#(.*))?'
189+
function print-runfile-commands() {
171190
# Print current Runfile commands:
172191
echo
173-
grep -E "^${task_re}$" "$( smartcase-file runfile )" \
174-
| sed -Ee "s/^${task_re}$/\1 · \3/g" -e 's/ / /g' -e 's/^/ /g'
192+
grep -E "^$( task_re )$" "$( smartcase-file runfile )" \
193+
| sed -Ee "s/^$( task_re )$/\1 · \4/g" -e 's/ / /g' -e 's/^/ /g'
175194
echo
176195
# Print Runfile command aliases if any are currently available:
177196
awk 'NR==FNR{a[$0]=1;next}a[$0]' <( bash -ic 'alias' ) <( print-runfile-aliases ) \
@@ -180,10 +199,9 @@ function print-runfile-commands() { local task_re
180199
}
181200

182201
function print-runfile-aliases() {
183-
task_re='([[:alnum:]_-][[:alnum:] _-]+):[[:alnum:] _-]*(#(.*))?'
184202
echo '# Runfile Aliases'
185-
grep -E "^${task_re}$" "$( smartcase-file runfile )" \
186-
| sed -E "s/.*(^| )${task_re}$/\2/g" \
203+
grep -E "^$( task_re )$" "$( smartcase-file runfile )" \
204+
| sed -E "s/.*(^| )$( task_re )$/\2/g" \
187205
| awk '!_[substr($1,1,1)]++' `# unique on first char of each command` \
188206
| sed -E "s/(.)(.*)/alias ${RUNFILE_ALIASES_PREFIX:-r}\1='run \1\2'/"
189207
echo '# END Runfile Aliases'
@@ -408,12 +426,9 @@ function run() ( set -euo pipefail
408426
local makefile='' buffer='' task='' baseindent=''
409427
local arg='' make_args=() named_args=() pos_args=() pos_arg_idx=0
410428

411-
local runfile_variables='' runfile_variable_re='' run_arg_re=''
412-
local task_re='' trap_re=''
429+
local runfile_variables=''
413430
local trap_sig='' runfile_grep_filter_args=()
414431

415-
run_arg_re='-([hvrmena]|-(help|version|runfile|makefile|edit|new|alias|eject|verbose|compact|compat|noconfirm|noedit))'
416-
417432
[[ "$*" == '' ]] && print-runfile-commands && exit 0
418433

419434
for action in help version runfile makefile edit new alias eject v r m e n a e
@@ -485,11 +500,11 @@ function run() ( set -euo pipefail
485500
make_args+=( "${arg/make-}" )
486501
elif [[ "${arg}" =~ ^[[:alnum:]_-]+\= ]]
487502
then
488-
if ! [[ "${arg}" =~ ^${run_arg_re}\= ]]
503+
if ! [[ "${arg}" =~ ^$( run_arg_re )\= ]]
489504
then
490505
named_args+=( "${arg}" )
491506
fi
492-
elif ! [[ "${arg}" =~ ^${run_arg_re}$ ]]
507+
elif ! [[ "${arg}" =~ ^$( run_arg_re )$ ]]
493508
then
494509
if [[ -z "${task}" ]]
495510
then
@@ -509,37 +524,26 @@ function run() ( set -euo pipefail
509524
fi
510525
done
511526
512-
task_re='([[:alnum:]_-][[:alnum:] _-]+):([[:alnum:] _-]+)?(#.*)?'
513-
trap_re='(EXIT|HUP|INT|QUIT|ABRT|KILL|ALRM|TERM)'
514-
515527
if [[ "${RUNFILE_TRAP:-}" == '*' ]]
516528
then
517-
runfile_grep_filter_args=( -E "^(${task_re}|\s*${trap_re} .*)$" )
529+
runfile_grep_filter_args=( -E "^($( task_re )|\s*$( trap_re ) .*)$" )
518530
elif [[ -n "${RUNFILE_TRAP:-}" ]]
519531
then
520-
runfile_grep_filter_args=( -E "^(${task_re}|\s*${RUNFILE_TRAP} .*)$" )
521-
else
522-
runfile_grep_filter_args=( -Ev "^\s*${trap_re} .*$" )
523-
fi
524-
525-
if [[ -n "${RUNFILE_SKIP_SUBTASKS:-}" ]]
526-
then
527-
subtask_re=''
532+
runfile_grep_filter_args=( -E "^($( task_re )|\s*${RUNFILE_TRAP} .*)$" )
528533
else
529-
subtask_re='\2'
534+
runfile_grep_filter_args=( -Ev "^\s*$( trap_re ) .*$" )
530535
fi
531536
532-
runfile_variable_re='^[[:space:]]*[A-Z]+ :*= '
533537
runfile_variables="$( \
534-
grep -E "${runfile_variable_re}" "$( smartcase-file runfile )" || true
538+
grep -E "$( runfile_variable_re )" "$( smartcase-file runfile )" || true
535539
)"
536540
if [[ -n "${runfile_variables}" ]]
537541
then
538542
runfile_variables="${runfile_variables}"$'\n\n'
539543
fi
540544
541545
baseindent="$( \
542-
grep -Eoz "^${task_re}[^\n]*\n[[:space:]]*" "$( smartcase-file runfile )" \
546+
grep -Eoz "^$( task_re )[^\n]*\n[[:space:]]*" "$( smartcase-file runfile )" \
543547
| head -2 | tail -1
544548
)"
545549
@@ -551,7 +555,7 @@ SHELL = ${SHELL:-bash}
551555
552556
${runfile_variables}$(
553557
grep "${runfile_grep_filter_args[@]}" "$( smartcase-file runfile )" \
554-
| grep -Ev "${runfile_variable_re}" \
558+
| grep -Ev "$( runfile_variable_re )" \
555559
| sed -E \
556560
-e "s/[[:space:]]*\$//" \
557561
`# trim any trailing whitespace from lines` \
@@ -563,8 +567,8 @@ ${runfile_variables}$(
563567
`# prefix every line with TAB` \
564568
-e "s!^\t\$!!" \
565569
`# remove TAB from blank lines` \
566-
-e "s!^\t${task_re}(.*)\$!\n.PHONY: \1\n\1:${subtask_re}\3!" \
567-
`# remove TAB-prefix from lines that match task pattern` \
570+
-e "s!^\t$( task_re )\$!\n.PHONY: \1\n\1:$( subtask_re )\3!" \
571+
`# add PHONY declarations; remove TAB-prefix from task lines` \
568572
`# Improve Make's default handling of multiline statements (if, for, while):` \
569573
-e "s!^\t\t(.*;[[:space:]]*[^\\]?)\$!\t\t\1 \\\\!" \
570574
-e "s!^\t\t(.*[^\\])\$!\t\t\1; \\\\!" \
@@ -672,7 +676,7 @@ EOF
672676
)
673677
do
674678
# First ensure trap_sig is valid:
675-
if [[ "${trap_sig}" =~ ^${trap_re}$ ]]
679+
if [[ "${trap_sig}" =~ ^$( trap_re )$ ]]
676680
then
677681
# shellcheck disable=SC2064
678682
# "Use single quotes, otherwise this expands now rather than when signalled."

0 commit comments

Comments
 (0)