Skip to content

Commit 08c54d8

Browse files
committed
docs: Improve function descriptions
1 parent be14629 commit 08c54d8

File tree

2 files changed

+74
-21
lines changed

2 files changed

+74
-21
lines changed

bake

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ 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+
94117
# @description Change the behavior of Bake. See [guide.md](./docs/guide.md) for details
95118
# @arg $1 string Name of config property to change
96119
# @arg $2 string New value of config property
@@ -101,21 +124,26 @@ bake.cfg() {
101124
case $cfg in
102125
stacktrace)
103126
case $value in
104-
yes|no) __bake_cfg_stacktrace=$value ;;
105-
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'yes' or 'no'" ;;
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'" ;;
106131
esac
107132
;;
108133
pedantic-task-cd)
109134
case $value in
110-
yes) trap '__bake_trap_debug' 'DEBUG' ;;
111-
no) trap - 'DEBUG' ;;
112-
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'yes' or 'no'" ;;
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'" ;;
113140
esac
114141
;;
115142
big-print)
116143
case $value in
117-
yes|no) ;;
118-
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'yes' or 'no'" ;;
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'" ;;
119147
esac
120148
;;
121149
*)
@@ -127,7 +155,7 @@ bake.cfg() {
127155
# @description Prints stacktrace
128156
# @internal
129157
__bake_print_stacktrace() {
130-
if [ "$__bake_cfg_stacktrace" = 'yes' ]; then
158+
if [ "$__bake_cfg_stacktrace" = 'on' ]; then
131159
if __bake_is_color; then
132160
printf '\033[4m%s\033[0m\n' 'Stacktrace:'
133161
else
@@ -173,11 +201,17 @@ __bake_trap_debug() {
173201
# @exitcode 1 if should not print color
174202
# @internal
175203
__bake_is_color() {
176-
if [[ -v NO_COLOR || $TERM == dumb ]]; then
204+
local fd="1"
205+
206+
if [[ ${NO_COLOR+x} || "$TERM" = 'dumb' ]]; then
177207
return 1
178-
else
208+
fi
209+
210+
if [ -t "$fd" ]; then
179211
return 0
180212
fi
213+
214+
return 1
181215
}
182216

183217
# @description Calls `__bake_internal_error` and terminates with code 1
@@ -240,11 +274,22 @@ __bake_print_tasks() {
240274
local str=
241275

242276
# shellcheck disable=SC1007,SC2034
243-
local regex="^(([[:space:]]*function[[:space:]]*)?task\.(.*?)\(\)).*"
277+
local regex="^([[:space:]]*function[[:space:]]*)?task\.(.*?)\(\)[[:space:]]*\{[[:space:]]*(#[[:space:]]*(.*))?"
244278
local line=
245279
while IFS= read -r line || [ -n "$line" ]; do
246280
if [[ "$line" =~ $regex ]]; then
247-
str+=" -> ${BASH_REMATCH[3]}"$'\n'
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'
248293
fi
249294
done < "$BAKE_FILE"; unset -v line
250295

@@ -265,7 +310,7 @@ __bake_print_tasks() {
265310
__bake_print_big() {
266311
local print_text="$1"
267312

268-
if [ "$__bake_cfg_big_print" = 'no' ]; then
313+
if [ "$__bake_cfg_big_print" = 'off' ]; then
269314
return
270315
fi
271316

@@ -302,7 +347,6 @@ __bake_parse_args() {
302347
unset REPLY; REPLY=
303348
local -i total_shifts=0
304349

305-
# FIXME: bug for when passing -v to child task argument
306350
local __bake_arg=
307351
for arg; do case $arg in
308352
-f)
@@ -323,14 +367,19 @@ __bake_parse_args() {
323367
fi
324368
;;
325369
-v)
326-
local bake_version='1.8.2'
370+
local bake_version='1.9.0'
327371
printf '%s\n' "Version: $bake_version"
372+
exit 0
328373
;;
329374
-h)
330375
local flag_help='yes'
331376
if ! shift; then
332377
__bake_internal_die 'Failed to shift'
333378
fi
379+
;;
380+
*)
381+
break
382+
;;
334383
esac done
335384

336385
if [ -n "$BAKE_FILE" ]; then
@@ -373,8 +422,8 @@ __bake_parse_args() {
373422
# @description Main function
374423
# @internal
375424
__bake_main() {
376-
__bake_cfg_stacktrace='no'
377-
__bake_cfg_big_print='yes'
425+
__bake_cfg_stacktrace='off'
426+
__bake_cfg_big_print='on'
378427

379428
# Environment boilerplate
380429
set -ETeo pipefail
@@ -384,7 +433,7 @@ __bake_main() {
384433
LC_TELEPHONE='C' LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
385434
trap '__bake_trap_err' 'ERR'
386435
trap ':' 'INT' # Ensure Ctrl-C ends up printing <- ERROR ==== etc.
387-
bake.cfg pedantic-task-cd 'no'
436+
bake.cfg pedantic-task-cd 'off'
388437

389438
# Parse arguments
390439
# Set `BAKE_{ROOT,FILE}`
@@ -437,8 +486,8 @@ __bake_main() {
437486
local shouldTestNextLine='no'
438487
while IFS= read -r line; do
439488
if [ "$shouldTestNextLine" = 'yes' ]; then
440-
if [[ $line == *'bake.cfg'*big-print*no* ]]; then
441-
__bake_cfg_big_print='no'
489+
if [[ $line == *'bake.cfg'*big-print*@(no|off)* ]]; then
490+
__bake_cfg_big_print='off'
442491
fi
443492
shouldTestNextLine='no'
444493
fi

pkg/src/util/util.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# shellcheck shell=bash
22

3+
# @description Initialize global variables required for shopt and trap functions
34
# @internal
45
core.private.util.init() {
56
if [ ${___global_bash_core_has_init__+x} ]; then
@@ -11,6 +12,7 @@ core.private.util.init() {
1112
declare -ga ___global_shopt_stack___=()
1213
}
1314

15+
# @description Function that runs handlers for a particular signal
1416
# @internal
1517
core.private.util.trap_handler_common() {
1618
local signal_spec="$1"
@@ -35,6 +37,7 @@ core.private.util.trap_handler_common() {
3537
done; unset -v trap_handler
3638
}
3739

40+
# @internal
3841
core.private.util.validate_args() {
3942
local function="$1"
4043
local arg_count="$2"
@@ -48,6 +51,7 @@ core.private.util.validate_args() {
4851
fi
4952
}
5053

54+
# @internal
5155
core.private.util.validate_signal() {
5256
local function="$1"
5357
local signal_spec="$2"
@@ -66,7 +70,7 @@ core.private.util.validate_signal() {
6670
fi
6771
}
6872

69-
# @description Prints the current error stored
73+
# @description Prints the current errors
7074
# @internal
7175
core.private.util.err_print() {
7276
printf '%s\n' 'Error found:'

0 commit comments

Comments
 (0)