Skip to content

Commit af84851

Browse files
committed
restructure: Remove duplicate variable checking code
1 parent ea289d4 commit af84851

File tree

2 files changed

+30
-138
lines changed

2 files changed

+30
-138
lines changed

bake

Lines changed: 26 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Learn more about it [on GitHub](https://github.com/hyperupcall/bake)
1717

1818
if [ "$0" != "${BASH_SOURCE[0]}" ] && [ "$BAKE_INTERNAL_CAN_SOURCE" != 'yes' ]; then
19-
printf '%s\n' 'Error: This file should not be sourced' >&2
19+
printf '%s\n' "Error: This file should not be sourced" >&2
2020
return 1
2121
fi
2222

@@ -91,59 +91,25 @@ bake.assert_cmd() {
9191
fi
9292
}
9393

94-
# @description Determine if a flag was passed as an argument
95-
# @arg $1 string Flag name to test for
96-
# @arg $@ string Rest of the arguments to search through
97-
bake.has_flag() {
98-
local flag_name="$1"
99-
100-
if [ -z "$flag_name" ]; then
101-
bake.die "Argument must not be empty"
102-
fi
103-
if ! shift; then
104-
bake.die 'Failed to shift'
105-
fi
106-
107-
local arg=
108-
for arg; do
109-
if [ "$arg" = "$flag_name" ]; then
110-
return 0
111-
fi
112-
done; unset -v arg
113-
114-
return 1
115-
}
116-
117-
# @description Change the behavior of Bake. See [guide.md](./docs/guide.md) for details
94+
# @description Change the behavior of Bake
11895
# @arg $1 string Name of config property to change
11996
# @arg $2 string New value of config property
12097
bake.cfg() {
121-
local cfg="$1"
122-
local value="$2"
98+
local cfg=$1
99+
local value=$2
123100

124101
case $cfg in
125102
stacktrace)
126103
case $value in
127-
yes) __bake_internal_warn "Passing either 'yes' or 'no' as a value for 'bake.cfg stacktrace' is deprecated. Instead, use either 'on' or 'off'"; __bake_cfg_stacktrace='on' ;;
128-
no) __bake_internal_warn "Passing either 'yes' or 'no' as a value for 'bake.cfg stacktrace' is deprecated. Instead, use either 'on' or 'off'"; __bake_cfg_stacktrace='off' ;;
129-
on|off) __bake_cfg_stacktrace=$value ;;
130-
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'on' or 'off'" ;;
104+
yes|no) __bake_cfg_stacktrace=$value ;;
105+
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'yes' or 'no'" ;;
131106
esac
132107
;;
133108
pedantic-task-cd)
134109
case $value in
135-
yes) __bake_internal_warn "Passing either 'yes' or 'no' as a value for 'bake.cfg pedantic-task-cd' is deprecated. Instead, use either 'on' or 'off'"; trap '__bake_trap_debug' 'DEBUG' ;;
136-
no) __bake_internal_warn "Passing either 'yes' or 'no' as a value for 'bake.cfg pedantic-task-cd' is deprecated. Instead, use either 'on' or 'off'"; trap - 'DEBUG' ;;
137-
on) trap '__bake_trap_debug' 'DEBUG' ;;
138-
off) trap - 'DEBUG' ;;
139-
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'on' or 'off'" ;;
140-
esac
141-
;;
142-
big-print)
143-
case $value in
144-
yes|no) __bake_internal_warn "Passing either 'yes' or 'no' as a value for 'bake.cfg big-print' is deprecated. Instead, use either 'on' or 'off'" ;;
145-
on|off) ;;
146-
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'on' or 'off'" ;;
110+
yes) trap '__bake_trap_debug' 'DEBUG' ;;
111+
no) trap - 'DEBUG' ;;
112+
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'yes' or 'no'" ;;
147113
esac
148114
;;
149115
*)
@@ -155,7 +121,7 @@ bake.cfg() {
155121
# @description Prints stacktrace
156122
# @internal
157123
__bake_print_stacktrace() {
158-
if [ "$__bake_cfg_stacktrace" = 'on' ]; then
124+
if [ "$__bake_cfg_stacktrace" = 'yes' ]; then
159125
if __bake_is_color; then
160126
printf '\033[4m%s\033[0m\n' 'Stacktrace:'
161127
else
@@ -201,17 +167,11 @@ __bake_trap_debug() {
201167
# @exitcode 1 if should not print color
202168
# @internal
203169
__bake_is_color() {
204-
local fd="1"
205-
206-
if [[ ${NO_COLOR+x} || "$TERM" = 'dumb' ]]; then
170+
if [[ -v NO_COLOR || $TERM == dumb ]]; then
207171
return 1
208-
fi
209-
210-
if [ -t "$fd" ]; then
172+
else
211173
return 0
212174
fi
213-
214-
return 1
215175
}
216176

217177
# @description Calls `__bake_internal_error` and terminates with code 1
@@ -270,38 +230,15 @@ __bake_error() {
270230
# @description Nicely prints all 'Bakefile.sh' tasks to standard output
271231
# @internal
272232
__bake_print_tasks() {
273-
printf '%s\n' 'Tasks:'
274-
local str=
275-
276233
# shellcheck disable=SC1007,SC2034
277-
local regex="^([[:space:]]*function[[:space:]]*)?task\.(.*?)\(\)[[:space:]]*\{[[:space:]]*(#[[:space:]]*(.*))?"
234+
local regex="^(([[:space:]]*function[[:space:]]*)?task\.(.*?)\(\)).*"
278235
local line=
236+
printf '%s\n' 'Tasks:'
279237
while IFS= read -r line || [ -n "$line" ]; do
280238
if [[ "$line" =~ $regex ]]; then
281-
str+=" -> ${BASH_REMATCH[2]}"
282-
283-
local task_comment=${BASH_REMATCH[4]}
284-
if [ -n "$task_comment" ]; then
285-
if __bake_is_color; then
286-
str+=$' \033[3m'"($task_comment)"$'\033[0m'
287-
else
288-
str+=" ($task_comment)"
289-
fi
290-
fi
291-
292-
str+=$'\n'
239+
printf '%s\n' " -> ${BASH_REMATCH[3]}"
293240
fi
294241
done < "$BAKE_FILE"; unset -v line
295-
296-
if [ -z "$str" ]; then
297-
if __bake_is_color; then
298-
str=$' \033[3mNo tasks\033[0m\n'
299-
else
300-
str=$' No tasks\n'
301-
fi
302-
fi
303-
304-
printf '%s' "$str"
305242
} >&2
306243

307244
# @description Prints text that takes up the whole terminal width
@@ -310,21 +247,13 @@ __bake_print_tasks() {
310247
__bake_print_big() {
311248
local print_text="$1"
312249

313-
if [ "$__bake_cfg_big_print" = 'off' ]; then
314-
return
315-
fi
316-
317250
# shellcheck disable=SC1007
318251
local _stty_height= _stty_width=
319252
read -r _stty_height _stty_width < <(
320-
if stty size &>/dev/null; then
253+
if command -v stty &>/dev/null; then
321254
stty size
322255
else
323-
if [ -n "$COLUMNS" ]; then
324-
printf '%s\n' "20 $COLUMNS"
325-
else
326-
printf '%s\n' '20 80'
327-
fi
256+
printf '%s\n' '20 80'
328257
fi
329258
)
330259

@@ -366,20 +295,11 @@ __bake_parse_args() {
366295
__bake_internal_die "Specified file '$BAKE_FILE' is not actually a file"
367296
fi
368297
;;
369-
-v)
370-
local bake_version='1.9.0'
371-
printf '%s\n' "Version: $bake_version"
372-
exit 0
373-
;;
374298
-h)
375299
local flag_help='yes'
376300
if ! shift; then
377301
__bake_internal_die 'Failed to shift'
378302
fi
379-
;;
380-
*)
381-
break
382-
;;
383303
esac done
384304

385305
if [ -n "$BAKE_FILE" ]; then
@@ -391,7 +311,7 @@ __bake_parse_args() {
391311
BAKE_FILE="$BAKE_ROOT/${BAKE_FILE##*/}"
392312
else
393313
if ! BAKE_ROOT=$(
394-
while [ ! -f './Bakefile.sh' ] && [ "$PWD" != / ]; do
314+
while [ ! -f 'Bakefile.sh' ] && [ "$PWD" != / ]; do
395315
if ! cd ..; then
396316
exit 1
397317
fi
@@ -409,8 +329,8 @@ __bake_parse_args() {
409329
fi
410330

411331
if [ "$flag_help" = 'yes' ]; then
412-
cat <<-"EOF"
413-
Usage: bake [-h|-v] [-f <Bakefile>] [var=value ...] <task> [args ...]
332+
cat <<-EOF
333+
Usage: bake [-h] [-f <Bakefile>] [var=value ...] <task> [args ...]
414334
EOF
415335
__bake_print_tasks
416336
exit
@@ -422,28 +342,23 @@ __bake_parse_args() {
422342
# @description Main function
423343
# @internal
424344
__bake_main() {
425-
__bake_cfg_stacktrace='off'
426-
__bake_cfg_big_print='on'
345+
__bake_cfg_stacktrace='no'
427346

428-
# Environment boilerplate
429347
set -ETeo pipefail
430348
shopt -s dotglob extglob globasciiranges globstar lastpipe shift_verbose
431-
export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' \
432-
LC_MONETARY='C' LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' \
433-
LC_TELEPHONE='C' LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
349+
export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' LC_MONETARY='C' \
350+
LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' LC_TELEPHONE='C' \
351+
LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
434352
trap '__bake_trap_err' 'ERR'
435-
trap ':' 'INT' # Ensure Ctrl-C ends up printing <- ERROR ==== etc.
436-
bake.cfg pedantic-task-cd 'off'
353+
bake.cfg pedantic-task-cd 'no'
437354

438-
# Parse arguments
439355
# Set `BAKE_{ROOT,FILE}`
440356
BAKE_ROOT=; BAKE_FILE=
441357
__bake_parse_args "$@"
442358
if ! shift "$REPLY"; then
443359
__bake_internal_die 'Failed to shift'
444360
fi
445361

446-
# Set variables à la Make
447362
# shellcheck disable=SC1007
448363
local __bake_key= __bake_value=
449364
local __bake_arg=
@@ -461,8 +376,8 @@ __bake_main() {
461376
;;
462377
*) break
463378
esac done; unset -v __bake_arg
379+
# Note: Don't unset '__bake_variable' or none of the variables will stay set
464380
unset -v __bake_key __bake_value
465-
unset -vn __bake_variable
466381

467382
local __bake_task="$1"
468383
if [ -z "$__bake_task" ]; then
@@ -482,21 +397,6 @@ __bake_main() {
482397
__bake_task= source "$BAKE_FILE"
483398

484399
if declare -f task."$__bake_task" >/dev/null 2>&1; then
485-
local line=
486-
local shouldTestNextLine='no'
487-
while IFS= read -r line; do
488-
if [ "$shouldTestNextLine" = 'yes' ]; then
489-
if [[ $line == *'bake.cfg'*big-print*@(no|off)* ]]; then
490-
__bake_cfg_big_print='off'
491-
fi
492-
shouldTestNextLine='no'
493-
fi
494-
495-
if [[ $line == @(task."$__bake_task"|init)*'('*')'*'{' ]]; then
496-
shouldTestNextLine='yes'
497-
fi
498-
done < "$BAKE_FILE"; unset -v line shouldTestNextLine
499-
500400
__bake_print_big "-> RUNNING TASK '$__bake_task'"
501401
if declare -f init >/dev/null 2>&1; then
502402
init "$__bake_task"

pkg/src/public/bash-core.sh

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
# kill -USR1 $$
1515
# core.trap_remove 'some_handler' 'USR1'
1616
core.trap_add() {
17-
if ! [ ${___global_bash_core_has_init__+x} ]; then
18-
core.private.util.init
19-
fi
17+
core.private.util.init
2018
local function="$1"
2119

2220
core.private.util.validate_args "$function" $#
@@ -57,9 +55,7 @@ core.trap_add() {
5755
# kill -USR1 $$
5856
# core.trap_remove 'some_handler' 'USR1'
5957
core.trap_remove() {
60-
if ! [ ${___global_bash_core_has_init__+x} ]; then
61-
core.private.util.init
62-
fi
58+
core.private.util.init
6359
local function="$1"
6460

6561
core.private.util.validate_args "$function" $#
@@ -106,9 +102,7 @@ core.trap_remove() {
106102
# [[ 'variable' == @(foxtrot|golf|echo|variable) ]] && printf '%s\n' 'Woof!'
107103
# core.shopt_pop
108104
core.shopt_push() {
109-
if ! [ ${___global_bash_core_has_init__+x} ]; then
110-
core.private.util.init
111-
fi
105+
core.private.util.init
112106
local shopt_action="$1"
113107
local shopt_name="$2"
114108

@@ -153,9 +147,7 @@ core.shopt_push() {
153147
# [[ 'variable' == @(foxtrot|golf|echo|variable) ]] && printf '%s\n' 'Woof!'
154148
# core.shopt_pop
155149
core.shopt_pop() {
156-
if ! [ ${___global_bash_core_has_init__+x} ]; then
157-
core.private.util.init
158-
fi
150+
core.private.util.init
159151

160152
if (( ${#___global_shopt_stack___[@]} == 0 )); then
161153
core.panic 'Unable to pop as nothing is in the shopt stack'

0 commit comments

Comments
 (0)