1616# Learn more about it [on GitHub](https://github.com/hyperupcall/bake)
1717
1818if [ " $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
2121fi
2222
@@ -91,12 +91,12 @@ bake.assert_cmd() {
9191 fi
9292}
9393
94- # @description Change the behavior of Bake
94+ # @description Change the behavior of Bake. See [guide.md](./docs/guide.md) for details
9595# @arg $1 string Name of config property to change
9696# @arg $2 string New value of config property
9797bake.cfg () {
98- local cfg=$1
99- local value=$2
98+ local cfg=" $1 "
99+ local value=" $2 "
100100
101101 case $cfg in
102102 stacktrace)
@@ -112,6 +112,12 @@ bake.cfg() {
112112 * ) __bake_internal_die2 " Config property '$cfg ' accepts only either 'yes' or 'no'" ;;
113113 esac
114114 ;;
115+ big-print)
116+ case $value in
117+ yes|no) ;;
118+ * ) __bake_internal_die2 " Config property '$cfg ' accepts only either 'yes' or 'no'" ;;
119+ esac
120+ ;;
115121 * )
116122 __bake_internal_die2 " No config property matched '$cfg '"
117123 ;;
@@ -230,15 +236,27 @@ __bake_error() {
230236# @description Nicely prints all 'Bakefile.sh' tasks to standard output
231237# @internal
232238__bake_print_tasks () {
239+ printf ' %s\n' ' Tasks:'
240+ local str=
241+
233242 # shellcheck disable=SC1007,SC2034
234243 local regex=" ^(([[:space:]]*function[[:space:]]*)?task\.(.*?)\(\)).*"
235244 local line=
236- printf ' %s\n' ' Tasks:'
237245 while IFS= read -r line || [ -n " $line " ]; do
238246 if [[ " $line " =~ $regex ]]; then
239- printf ' %s\n ' " -> ${BASH_REMATCH[3]} "
247+ str+= " -> ${BASH_REMATCH[3]} " $' \n '
240248 fi
241249 done < " $BAKE_FILE " ; unset -v line
250+
251+ if [ -z " $str " ]; then
252+ if __bake_is_color; then
253+ str=$' \033 [3mNo tasks\033 [0m\n '
254+ else
255+ str=$' No tasks\n '
256+ fi
257+ fi
258+
259+ printf ' %s' " $str "
242260} >&2
243261
244262# @description Prints text that takes up the whole terminal width
@@ -247,13 +265,21 @@ __bake_print_tasks() {
247265__bake_print_big () {
248266 local print_text=" $1 "
249267
268+ if [ " $__bake_cfg_big_print " = ' no' ]; then
269+ return
270+ fi
271+
250272 # shellcheck disable=SC1007
251273 local _stty_height= _stty_width=
252274 read -r _stty_height _stty_width < <(
253- if command -v stty & > /dev/null; then
275+ if stty size & > /dev/null; then
254276 stty size
255277 else
256- printf ' %s\n' ' 20 80'
278+ if [ -n " $COLUMNS " ]; then
279+ printf ' %s\n' " 20 $COLUMNS "
280+ else
281+ printf ' %s\n' ' 20 80'
282+ fi
257283 fi
258284 )
259285
@@ -276,6 +302,7 @@ __bake_parse_args() {
276302 unset REPLY; REPLY=
277303 local -i total_shifts=0
278304
305+ # FIXME: bug for when passing -v to child task argument
279306 local __bake_arg=
280307 for arg; do case $arg in
281308 -f)
@@ -295,6 +322,10 @@ __bake_parse_args() {
295322 __bake_internal_die " Specified file '$BAKE_FILE ' is not actually a file"
296323 fi
297324 ;;
325+ -v)
326+ local bake_version=' 1.8.2'
327+ printf ' %s\n' " Version: $bake_version "
328+ ;;
298329 -h)
299330 local flag_help=' yes'
300331 if ! shift ; then
@@ -311,7 +342,7 @@ __bake_parse_args() {
311342 BAKE_FILE=" $BAKE_ROOT /${BAKE_FILE##*/ } "
312343 else
313344 if ! BAKE_ROOT=$(
314- while [ ! -f ' Bakefile.sh' ] && [ " $PWD " != / ]; do
345+ while [ ! -f ' ./ Bakefile.sh' ] && [ " $PWD " != / ]; do
315346 if ! cd ..; then
316347 exit 1
317348 fi
@@ -329,8 +360,8 @@ __bake_parse_args() {
329360 fi
330361
331362 if [ " $flag_help " = ' yes' ]; then
332- cat << -EOF
333- Usage: bake [-h] [-f <Bakefile>] [var=value ...] <task> [args ...]
363+ cat << -" EOF "
364+ Usage: bake [-h|-v ] [-f <Bakefile>] [var=value ...] <task> [args ...]
334365 EOF
335366 __bake_print_tasks
336367 exit
@@ -343,22 +374,27 @@ __bake_parse_args() {
343374# @internal
344375__bake_main () {
345376 __bake_cfg_stacktrace=' no'
377+ __bake_cfg_big_print=' yes'
346378
379+ # Environment boilerplate
347380 set -ETeo pipefail
348381 shopt -s dotglob extglob globasciiranges globstar lastpipe shift_verbose
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'
382+ export LANG=' C' LC_CTYPE=' C' LC_NUMERIC=' C' LC_TIME=' C' LC_COLLATE=' C' \
383+ LC_MONETARY =' C' LC_MESSAGES =' C' LC_PAPER =' C' LC_NAME =' C' LC_ADDRESS =' C' \
384+ LC_TELEPHONE= ' C ' LC_MEASUREMENT=' C' LC_IDENTIFICATION=' C' LC_ALL=' C'
352385 trap ' __bake_trap_err' ' ERR'
386+ trap ' :' ' INT' # Ensure Ctrl-C ends up printing <- ERROR ==== etc.
353387 bake.cfg pedantic-task-cd ' no'
354388
389+ # Parse arguments
355390 # Set `BAKE_{ROOT,FILE}`
356391 BAKE_ROOT=; BAKE_FILE=
357392 __bake_parse_args " $@ "
358393 if ! shift " $REPLY " ; then
359394 __bake_internal_die ' Failed to shift'
360395 fi
361396
397+ # Set variables à la Make
362398 # shellcheck disable=SC1007
363399 local __bake_key= __bake_value=
364400 local __bake_arg=
@@ -376,8 +412,8 @@ __bake_main() {
376412 ;;
377413 * ) break
378414 esac done ; unset -v __bake_arg
379- # Note: Don't unset '__bake_variable' or none of the variables will stay set
380415 unset -v __bake_key __bake_value
416+ unset -vn __bake_variable
381417
382418 local __bake_task=" $1 "
383419 if [ -z " $__bake_task " ]; then
@@ -397,6 +433,21 @@ __bake_main() {
397433 __bake_task= source " $BAKE_FILE "
398434
399435 if declare -f task." $__bake_task " > /dev/null 2>&1 ; then
436+ local line=
437+ local shouldTestNextLine=' no'
438+ while IFS= read -r line; do
439+ if [ " $shouldTestNextLine " = ' yes' ]; then
440+ if [[ $line == * ' bake.cfg' * big-print* no* ]]; then
441+ __bake_cfg_big_print=' no'
442+ fi
443+ shouldTestNextLine=' no'
444+ fi
445+
446+ if [[ $line == @ (task." $__bake_task " | init)* ' (' * ' )' * ' {' ]]; then
447+ shouldTestNextLine=' yes'
448+ fi
449+ done < " $BAKE_FILE " ; unset -v line shouldTestNextLine
450+
400451 __bake_print_big " -> RUNNING TASK '$__bake_task '"
401452 if declare -f init > /dev/null 2>&1 ; then
402453 init " $__bake_task "
0 commit comments