16
16
# Learn more about it [on GitHub](https://github.com/hyperupcall/bake)
17
17
18
18
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
20
20
return 1
21
21
fi
22
22
@@ -91,12 +91,12 @@ bake.assert_cmd() {
91
91
fi
92
92
}
93
93
94
- # @description Change the behavior of Bake
94
+ # @description Change the behavior of Bake. See [guide.md](./docs/guide.md) for details
95
95
# @arg $1 string Name of config property to change
96
96
# @arg $2 string New value of config property
97
97
bake.cfg () {
98
- local cfg=$1
99
- local value=$2
98
+ local cfg=" $1 "
99
+ local value=" $2 "
100
100
101
101
case $cfg in
102
102
stacktrace)
@@ -112,6 +112,12 @@ bake.cfg() {
112
112
* ) __bake_internal_die2 " Config property '$cfg ' accepts only either 'yes' or 'no'" ;;
113
113
esac
114
114
;;
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
+ ;;
115
121
* )
116
122
__bake_internal_die2 " No config property matched '$cfg '"
117
123
;;
@@ -230,15 +236,27 @@ __bake_error() {
230
236
# @description Nicely prints all 'Bakefile.sh' tasks to standard output
231
237
# @internal
232
238
__bake_print_tasks () {
239
+ printf ' %s\n' ' Tasks:'
240
+ local str=
241
+
233
242
# shellcheck disable=SC1007,SC2034
234
243
local regex=" ^(([[:space:]]*function[[:space:]]*)?task\.(.*?)\(\)).*"
235
244
local line=
236
- printf ' %s\n' ' Tasks:'
237
245
while IFS= read -r line || [ -n " $line " ]; do
238
246
if [[ " $line " =~ $regex ]]; then
239
- printf ' %s\n ' " -> ${BASH_REMATCH[3]} "
247
+ str+= " -> ${BASH_REMATCH[3]} " $' \n '
240
248
fi
241
249
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 "
242
260
} >&2
243
261
244
262
# @description Prints text that takes up the whole terminal width
@@ -247,13 +265,21 @@ __bake_print_tasks() {
247
265
__bake_print_big () {
248
266
local print_text=" $1 "
249
267
268
+ if [ " $__bake_cfg_big_print " = ' no' ]; then
269
+ return
270
+ fi
271
+
250
272
# shellcheck disable=SC1007
251
273
local _stty_height= _stty_width=
252
274
read -r _stty_height _stty_width < <(
253
- if command -v stty & > /dev/null; then
275
+ if stty size & > /dev/null; then
254
276
stty size
255
277
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
257
283
fi
258
284
)
259
285
@@ -276,6 +302,7 @@ __bake_parse_args() {
276
302
unset REPLY; REPLY=
277
303
local -i total_shifts=0
278
304
305
+ # FIXME: bug for when passing -v to child task argument
279
306
local __bake_arg=
280
307
for arg; do case $arg in
281
308
-f)
@@ -295,6 +322,10 @@ __bake_parse_args() {
295
322
__bake_internal_die " Specified file '$BAKE_FILE ' is not actually a file"
296
323
fi
297
324
;;
325
+ -v)
326
+ local bake_version=' 1.8.2'
327
+ printf ' %s\n' " Version: $bake_version "
328
+ ;;
298
329
-h)
299
330
local flag_help=' yes'
300
331
if ! shift ; then
@@ -311,7 +342,7 @@ __bake_parse_args() {
311
342
BAKE_FILE=" $BAKE_ROOT /${BAKE_FILE##*/ } "
312
343
else
313
344
if ! BAKE_ROOT=$(
314
- while [ ! -f ' Bakefile.sh' ] && [ " $PWD " != / ]; do
345
+ while [ ! -f ' ./ Bakefile.sh' ] && [ " $PWD " != / ]; do
315
346
if ! cd ..; then
316
347
exit 1
317
348
fi
@@ -329,8 +360,8 @@ __bake_parse_args() {
329
360
fi
330
361
331
362
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 ...]
334
365
EOF
335
366
__bake_print_tasks
336
367
exit
@@ -343,22 +374,27 @@ __bake_parse_args() {
343
374
# @internal
344
375
__bake_main () {
345
376
__bake_cfg_stacktrace=' no'
377
+ __bake_cfg_big_print=' yes'
346
378
379
+ # Environment boilerplate
347
380
set -ETeo pipefail
348
381
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'
352
385
trap ' __bake_trap_err' ' ERR'
386
+ trap ' :' ' INT' # Ensure Ctrl-C ends up printing <- ERROR ==== etc.
353
387
bake.cfg pedantic-task-cd ' no'
354
388
389
+ # Parse arguments
355
390
# Set `BAKE_{ROOT,FILE}`
356
391
BAKE_ROOT=; BAKE_FILE=
357
392
__bake_parse_args " $@ "
358
393
if ! shift " $REPLY " ; then
359
394
__bake_internal_die ' Failed to shift'
360
395
fi
361
396
397
+ # Set variables à la Make
362
398
# shellcheck disable=SC1007
363
399
local __bake_key= __bake_value=
364
400
local __bake_arg=
@@ -376,8 +412,8 @@ __bake_main() {
376
412
;;
377
413
* ) break
378
414
esac done ; unset -v __bake_arg
379
- # Note: Don't unset '__bake_variable' or none of the variables will stay set
380
415
unset -v __bake_key __bake_value
416
+ unset -vn __bake_variable
381
417
382
418
local __bake_task=" $1 "
383
419
if [ -z " $__bake_task " ]; then
@@ -397,6 +433,21 @@ __bake_main() {
397
433
__bake_task= source " $BAKE_FILE "
398
434
399
435
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
+
400
451
__bake_print_big " -> RUNNING TASK '$__bake_task '"
401
452
if declare -f init > /dev/null 2>&1 ; then
402
453
init " $__bake_task "
0 commit comments