-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathconcurrent.lib.sh
1096 lines (919 loc) · 37.4 KB
/
concurrent.lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
concurrent() (
#
# General Utilities
#
__crt__error() {
echo "ERROR (concurrent): ${1}" 1>&2
exit 1
}
__crt__is_dry_run() {
[[ -n "${CONCURRENT_DRY_RUN}" ]]
}
__crt__hide_failure() {
"${@}" 2> /dev/null || :
}
#
# Compatibility Check
#
if ! declare -g >/dev/null 2>&1; then
__crt__error "Requires at least Bash version 4.2 for 'declare -g' (you have ${BASH_VERSION:-a different shell})"
fi
#
# Help and Usage
#
__crt__help__version='concurrent 2.4.0'
__crt__help__usage="concurrent - Run tasks in parallel and display pretty output as they complete.
Usage:
concurrent \\
(- TASK COMMAND [ARGS...] | --and-then)... \\
[--sequential | [(--require-all | (--require TASK)...) \\
( --before-all | ( --before TASK)...)...]]
concurrent -h | --help
concurrent --version
Options:
-h --help Show this help.
--version Show version.
- TASK COMMAND [ARGS...] Define a task named TASK for running COMMAND with ARGS.
--and-then Insert between tasks to create groups of concurrent tasks.
--sequential Each task requires the previous task.
--require TASK Require a TASK to complete successfully...
--before TASK ...before another TASK.
--require-all Shortcut for requiring all tasks.
--before-all Given tasks are prerequisites to all others.
Failed Tasks:
If a task fails, all dependent tasks (and their dependent tasks, and so on) are
immediately marked 'SKIP'. The status and output of all failed and skipped tasks
are displayed at the end. The exit status will be 1.
Examples:
# Run three tasks concurrently.
concurrent \\
- 'My long task' sleep 10 \\
- 'My medium task' sleep 5 \\
- 'My short task' sleep 1
# Run three tasks sequentially.
concurrent \\
- 'My long task' sleep 10 \\
- 'My medium task' sleep 5 \\
- 'My short task' sleep 1 \\
--sequential
# Start the medium task *after* the short task succeeds.
concurrent \\
- 'My long task' sleep 10 \\
- 'My medium task' sleep 5 \\
- 'My short task' sleep 1 \\
--require 'My short task' \\
--before 'My medium task'
# Start the short task after *both* other tasks succeed.
...
--require 'My long task' \\
--require 'My medium task' \\
--before 'My short task'
# Start the medium task *and* the long task after the short task succeeds.
...
--require 'My short task' \\
--before 'My medium task' \\
--before 'My long task'
# If your command has a '-' argument, you can use a different task delimiter.
concurrent \\
+ 'My long task' wget -O - ... \\
+ 'My medium task' sleep 5 \\
+ 'My short task' sleep 1
Settable Environment Variables:
CONCURRENT_DRY_RUN (default: unset)
Setting this to any non-empty value causes 'dry-run' mode to be enabled.
In this mode, all task commands are replaced with 'sleep 3'. This is useful
for testing that the dependencies between tasks are correctly specified.
CONCURRENT_LIMIT (default: 50)
Specifies the number of tasks which are allowed to be actively running at
any time. For unlimited concurrency, set this to an integer less than 1.
CONCURRENT_COMPACT (default: 0)
Setting this to a non-zero value causes the 'compact display' mode to be
activated. In this mode, each task is represented by a single character
instead of an entire line. This mode is automatically activated if the
number of tasks exceed the terminal height.
CONCURRENT_NONINTERACTIVE (default: 0)
Setting this to a non-zero value causes the 'non-interactive' mode to be
activated. In this mode, tasks are displayed in the order they finish
and colors are disabled. This mode is automatically activated if the
output is not to a tty.
CONCURRENT_LOG_DIR (default: \$PWD/.logs/\$(date +'%F@%T'))
Optionally change the directory in which all tasks are logged.
Informational Environment Variables:
CONCURRENT_DEPTH
The current nesting depth of this 'concurrent' instance, starting at 0.
CONCURRENT_COMMAND
An array of the arguments being run for a task.
CONCURRENT_TASK_NAME
The name of the task being run.
Requirements:
bash >= 4.2, cat, cp, date, mkdir, mkfifo, mktemp, mv, sed, tail, tput
Author:
Matthew Tardiff <[email protected]>
License:
MIT
Version:
${__crt__help__version}
URL:
https://github.com/themattrix/bash-concurrent"
__crt__help__display_usage_and_exit() {
sed 's/^ //' <<< "${__crt__help__usage}"
exit 0
}
__crt__help__display_version_and_exit() {
echo "${__crt__help__version}"
exit 0
}
if [[ -z "${1}" ]] || [[ "${1}" == '-h' ]] || [[ "${1}" == '--help' ]]; then
__crt__help__display_usage_and_exit
elif [[ "${1}" == '--version' ]]; then
__crt__help__display_version_and_exit
fi
__crt__unset_env() {
# Unset all concurrent-related configuration environment variables.
unset CONCURRENT_DRY_RUN
}
__crt__unset() {
local sub_namespace=${1}
if [[ -n "${sub_namespace}" ]]; then
local namespace="__crt__${sub_namespace}__"
else
local namespace="__crt__"
fi
# shellcheck disable=SC2046
unset -f $(compgen -A function "${namespace}")
# shellcheck disable=SC2046
unset $(compgen -v "${namespace}")
}
__crt__unset 'help'
#
# Settings
#
__crt__ORIG_PWD=${PWD}
__crt__ORIG_OLDPWD=${OLDPWD}
__crt__ORIG_BASHOPTS=${BASHOPTS}
__crt__ORIG_SHELLOPTS=${SHELLOPTS}
__crt__set_original_pwd() {
cd "${__crt__ORIG_PWD}"
export OLDPWD=${__crt__ORIG_OLDPWD}
}
__crt__set_our_shell_options() {
set -o errexit # Exit on a failed command...
set -o pipefail # ...even if that command is in a pipeline.
shopt -s nullglob # Empty glob evaluates to nothing instead of itself
}
__crt__set_original_shell_options() {
__crt__set_our_shell_options
[[ "${__crt__ORIG_SHELLOPTS}" == *errexit* ]] || set +o errexit
[[ "${__crt__ORIG_SHELLOPTS}" == *pipefail* ]] || set +o pipefail
[[ "${__crt__ORIG_BASHOPTS}" == *nullglob* ]] || shopt -u nullglob
}
__crt__set_our_shell_options
#
# Task Management
#
__crt__is_task_started() {
[[ -z "${__crt__pending[${1}]}" ]]
}
__crt__is_task_done() {
[[ -n "${__crt__codes[${1}]}" ]]
}
__crt__are_all_tasks_done() {
[[ "${__crt__running_task_count}" -eq 0 ]]
}
__crt__is_task_running() {
__crt__is_task_started "${1}" && ! __crt__is_task_done "${1}"
}
__crt__is_under_concurrent_limit() {
[[ "${CONCURRENT_LIMIT}" -lt 1 || "${__crt__running_task_count}" -lt "${CONCURRENT_LIMIT}" ]]
}
__crt__clear_event_pipe() {
> "${__crt__event_pipe}"
}
__crt__name_index() {
local name=${1}
local i
for i in "${!__crt__names[@]}"; do
if [[ "${__crt__names[${i}]}" == "${name}" ]]; then
printf '%s' "${i}"
return
fi
done
__crt__error "Failed to find task named '${name}'"
}
__crt__is_task_allowed_to_start() {
# A task is allowed to start if:
# 1. it has not already started, and if
# 2. all prereq tasks have succeeded, and if
# 3. the process has not been interrupted.
# If any prereqs have failed or have been skipped, then this task will
# be skipped.
local task=${1}
if __crt__is_task_started "${task}"; then
return 1 # cannot start again
fi
local requires
local prereqs="__crt__prereqs_${task}[@]"
for requires in "${!prereqs}"; do
if [[ -z "${__crt__codes[${requires}]}" ]]; then
return 1
elif [[ "${__crt__codes[${requires}]}" != "0" ]]; then
__crt__skip_task "${task}" "${__crt__names[${requires}]}"
return 1
fi
done
if [[ "${__crt__interrupted}" -eq 1 ]]; then
__crt__mark_task_as_interrupted "${task}"
return 1
fi
# All prereqs succeeded! This task can be started.
}
__crt__mark_task_with_code() {
local task=${1}
local code=${2}
echo "task:${task}:${code}" >> "${__crt__event_pipe}"
}
__crt__task_runner() (
# Do not create real variables for these so that they do not override
# names from the parent script.
# $1: task index
# $2: command args array
# $3: status dir
# $4: event pipe
set -- "${1}" "__crt__command_${1}[@]" "${__crt__status_dir}" "${__crt__event_pipe}"
# Copy the command over since we're unsetting the __crt__ variables.
CONCURRENT_COMMAND=("${!2}")
# Reset any existing signal handlers.
trap -- - INT EXIT
# Allow nested tasks to refer to parent tasks.
CONCURRENT_TASK_NAME+=("${__crt__names[${1}]}")
export CONCURRENT_TASK_NAME
set +o errexit # a failure of the command should not exit the task
(
__crt__set_original_pwd
__crt__set_original_shell_options
__crt__unset_env
__crt__unset
"${CONCURRENT_COMMAND[@]}" 3>&1 &> "${3}/${1}" < /dev/null |
while read -r meta; do
printf "meta:%d:%s\n" "${1}" "${meta}" >> "${4}"
done
exit "${PIPESTATUS[0]}"
)
code=$?
set -o errexit # ...but other failures should
__crt__mark_task_with_code "${1}" "${code}"
)
__crt__mark_task_as_started() {
if [[ -n "${__crt__pending[${1}]}" ]]; then
unset "__crt__pending[${1}]"
(( __crt__running_task_count++ )) || :
fi
}
__crt__mark_task_as_stopped() {
local index=${1}
local code=${2}
if [[ -z "${__crt__codes[${index}]}" ]]; then
(( __crt__running_task_count-- )) || :
if [[ "${code}" == '0' ]]; then (( __crt__success_task_count++ )) || :
elif [[ "${code}" == 'skip' ]]; then (( __crt__skipped_task_count++ )) || :
elif [[ "${code}" == 'int' ]]; then (( __crt__interrupted_task_count++ )) || :
else (( __crt__failure_task_count++ )) || :
fi
fi
}
__crt__start_task() {
__crt__task_runner "${1}" &
__crt__mark_task_as_started "${1}"
__crt__draw_status "${1}" running
}
__crt__start_all_tasks() {
__crt__draw_initial_tasks
__crt__move_cursor_to_top
__crt__start_allowed_tasks
}
__crt__mark_task_as_interrupted() {
__crt__mark_task_as_started "${1}"
printf '[INTERRUPTED]\n' >> "${__crt__status_dir}/${1}"
printf 'task:%d:int\n' "${1}" >> "${__crt__event_pipe}"
}
__crt__mark_all_running_tasks_as_interrupted() {
local i
for (( i = 0; i < __crt__task_count; i++ )); do
if __crt__is_task_running "${i}"; then
__crt__mark_task_as_interrupted "${i}"
fi
done
}
__crt__skip_task() {
__crt__mark_task_as_started "${1}"
echo "[SKIPPED] Prereq '${2}' failed or was skipped" > "${__crt__status_dir}/${1}"
__crt__mark_task_with_code "${1}" skip
}
__crt__start_allowed_tasks() {
local __crt__i
for __crt__i in "${__crt__pending[@]}"; do
__crt__is_under_concurrent_limit || break
if __crt__is_task_allowed_to_start "${__crt__i}"; then
__crt__start_task "${__crt__i}"
fi
done
}
__crt__save_stdin_stream() {
exec 4<&0 # duplicate stdin stream to fd 4
}
__crt__restore_stdin_stream() {
exec 0<&4 # restore original stdin stream from fd 4
}
__crt__wait_for_all_tasks() {
__crt__start_animation
__crt__save_stdin_stream
__crt__run_event_loop
__crt__status_cleanup
__crt__stop_animation
wait # wait for all (completed) tasks
__crt__restore_stdin_stream
}
__crt__run_event_loop() {
# Main event loop! Each line read from the event pipe is an event to
# handle. We can exit the loop once all tasks have completed.
local __crt__event
local __crt__tail_pipe="${__crt__status_dir}/tail-pipe"
rm -f "${__crt__tail_pipe}"
mkfifo "${__crt__tail_pipe}"
tail -n +0 -f "${__crt__event_pipe}" >> "${__crt__tail_pipe}" &
__crt__tail_pid=$!
while read -r __crt__event; do
if [[ "${__crt__event}" == task:* ]]; then
__crt__handle_done_task "${__crt__event#task:}"
if __crt__are_all_tasks_done; then
break
fi
elif [[ "${__crt__event}" == anim:* ]]; then
__crt__update_running_status_frames
elif [[ "${__crt__event}" == meta:* ]]; then
__crt__manage_meta "${__crt__event#meta:}"
fi
done < "${__crt__tail_pipe}"
__crt__cleanup_event_loop
}
__crt__cleanup_event_loop() {
if [[ -n "${__crt__tail_pid}" ]]; then
__crt__hide_failure kill "${__crt__tail_pid}"
__crt__hide_failure wait "${__crt__tail_pid}"
fi
}
__crt__handle_done_task() {
local index=${1%%:*}
local code=${1#*:}
__crt__mark_task_as_stopped "${index}" "${code}"
__crt__codes["${index}"]=${code}
__crt__draw_status "${index}" "${code}"
cp -- "${__crt__status_dir}/${index}" "${CONCURRENT_LOG_DIR}/${index}. ${__crt__names[${index}]//\//-} (${code}).log"
if [[ "${code}" != "0" ]]; then
__crt__final_status=1
fi
__crt__start_allowed_tasks
}
__crt__manage_meta() {
local index=${1%%:*}
local meta=${1#*:}
__crt__meta["${index}"]=${meta}
__crt__draw_meta "${index}"
}
#
# Argument Parsing
#
__crt__names=() # task names by index
__crt__meta=() # metadata strings by index
__crt__pending=() # indexes of tasks which haven't been started yet
__crt__codes=() # task exit codes (unset, 0-255, 'skip', or 'int') by index
__crt__groups=() # array of task indexes before which --and-then flags were specified
__crt__task_count=0 # total number of tasks
__crt__final_status=0 # 0 if all tasks succeeded, 1 otherwise
__crt__interrupted=0 # 1 if script has been interrupted, 0 otherwise
# Only allow this many tasks running at a time.
export CONCURRENT_LIMIT=${CONCURRENT_LIMIT:-50}
__crt__running_task_count=0
__crt__success_task_count=0
__crt__failure_task_count=0
__crt__skipped_task_count=0
__crt__interrupted_task_count=0
# Arrays of command arguments by task index <T>:
# __crt__command_<T>=(...)
#
# Arrays of prerequisite task indices by task index <T>:
# __crt__prereqs_<T>=(...)
#
# These are dynamically created during argument parsing since bash doesn't
# have a concept of nested lists.
__crt__args__task_delimiter=${1}
__crt__args__is_task_flag() { [[ "${1}" == "${__crt__args__task_delimiter}" ]]; }
__crt__args__is_group_flag() { [[ "${1}" == "--and-then" ]]; }
__crt__args__is_require_flag() { [[ "${1}" == "--require" ]]; }
__crt__args__is_require_all_flag() { [[ "${1}" == "--require-all" ]]; }
__crt__args__is_before_flag() { [[ "${1}" == "--before" ]]; }
__crt__args__is_before_all_flag() { [[ "${1}" == "--before-all" ]]; }
__crt__args__is_sequential_flag() { [[ "${1}" == "--sequential" ]]; }
__crt__args__is_flag_starting_section() {
__crt__args__is_task_flag "${1}" ||
__crt__args__is_group_flag "${1}" ||
__crt__args__is_require_flag "${1}" ||
__crt__args__is_require_all_flag "${1}" ||
__crt__args__is_sequential_flag "${1}"
}
__crt__args__is_item_in_array() {
local item_to_find=${1}
local array_name="${2}[@]"
local i
for i in "${!array_name}"; do
if [[ "${i}" == "${item_to_find}" ]]; then return 0; fi
done
return 1
}
__crt__args__get_tasks_not_in() {
local these_tasks=${1}
local other_tasks=()
local i
for (( i = 0; i < __crt__task_count; i++ )); do
__crt__args__is_item_in_array "${i}" "${these_tasks}" || other_tasks=(${other_tasks[@]} ${i})
done
__crt__args__fn_result=("${other_tasks[@]}")
}
__crt__args__assign_sequential_prereqs() {
local i
for (( i = 1; i < __crt__task_count; i++ )); do
declare -g -a "__crt__prereqs_${i}=($(( i - 1 )))"
done
}
__crt__args__handle_task_flag() {
set -- "${remaining_args[@]}"
shift; (( $# )) || __crt__error "expected task name after '-'"
__crt__names+=("${1}")
shift; (( $# )) || __crt__error "expected command after task name"
local args=()
while (( $# )) && ! __crt__args__is_flag_starting_section "${1}"; do
args+=("${1}")
shift
done
if __crt__is_dry_run; then
args=(sleep 3) # DRY RUN: Sleep for 3 seconds instead of running a real command.
fi
declare -g -a "__crt__command_${__crt__task_count}=(\"\${args[@]}\")"
__crt__pending+=("${__crt__task_count}")
(( __crt__task_count++ )) || :
remaining_args=("${@}")
}
__crt__args__handle_group_flag() {
set -- "${remaining_args[@]}"
shift
__crt__groups+=("${__crt__task_count}")
remaining_args=("${@}")
}
__crt__args__handle_sequential_flag() {
set -- "${remaining_args[@]}"
shift
__crt__args__assign_sequential_prereqs
remaining_args=("${@}")
}
__crt__args__handle_require_flag() {
set -- "${remaining_args[@]}"
local require
local before
while (( $# )) && __crt__args__is_require_flag "${1}"; do
shift; (( $# )) || __crt__error "expected task name after '--require'"
require=(${require[@]} $(__crt__name_index "${1}"))
shift
done
if __crt__args__is_before_all_flag "${1}"; then
shift
__crt__args__get_tasks_not_in 'require'; before=("${__crt__args__fn_result[@]}")
local b
for b in "${before[@]}"; do
declare -g -a "__crt__prereqs_${b}=(\${require[@]})"
done
elif __crt__args__is_before_flag "${1}"; then
while (( $# )) && __crt__args__is_before_flag "${1}"; do
shift; (( $# )) || __crt__error "expected task name after '--before'"
before=$(__crt__name_index "${1}")
shift
if __crt__args__is_item_in_array "${before}" "require"; then
__crt__error "task cannot require itself"
fi
declare -g -a "__crt__prereqs_${before}=(\${__crt__prereqs_${before}[@]} \${require[@]})"
done
else
__crt__error "expected '--before' or '--before-all' after '--require-all'"
fi
remaining_args=("${@}")
}
__crt__args__handle_require_all_flag() {
set -- "${remaining_args[@]}"
local require
local before
shift
if __crt__args__is_before_all_flag "${1}"; then
shift
__crt__args__assign_sequential_prereqs # --require-all --before-all is the same as --sequential
elif __crt__args__is_before_flag "${1}"; then
before=()
while (( $# )) && __crt__args__is_before_flag "${1}"; do
shift; (( $# )) || __crt__error "expected task name after '--before'"
before=(${before[@]} $(__crt__name_index "${1}"))
shift
done
__crt__args__get_tasks_not_in 'before'; require=("${__crt__args__fn_result[@]}")
local b
for b in "${before[@]}"; do
declare -g -a "__crt__prereqs_${b}=(\${require[@]})"
done
else
__crt__error "expected '--before' or '--before-all' after '--require-all'"
fi
remaining_args=("${@}")
}
__crt__args__resolve_group_prereqs() {
local curr_index
local task_index
local curr_group
local next_group
local prev_group=0
# All tasks in group N are prereqs for all tasks in group N+1. If N+1
# does not exist, use the task count instead.
for (( curr_index = 0; curr_index < ${#__crt__groups[@]}; curr_index++ )); do
curr_group=${__crt__groups[${curr_index}]}
next_group=${__crt__groups[$(( curr_index + 1 ))]:-${__crt__task_count}}
for (( task_index = curr_group; task_index < next_group; task_index++ )); do
declare -g -a "__crt__prereqs_${task_index}=(\${__crt__prereqs_${task_index}[@]} {${prev_group}..$(( curr_group - 1 ))})"
done
prev_group=${curr_group}
done
# No longer need this array up in our business.
unset __crt__groups
}
__crt__args__ensure_no_requirement_loops() (
# We will do a lightweight dry-run through all of the tasks and make sure we
# do not get stuck anywhere.
tasks_started=0
is_task_allowed_to_start() {
local task=${1}
[[ -n "${__crt__pending[${task}]}" ]] || return 1
local requires
local prereqs="__crt__prereqs_${task}[@]"
for requires in "${!prereqs}"; do
[[ -z "${__crt__pending[${requires}]}" ]] || return 1
done
}
start_allowed_tasks() {
tasks_started=0
local i
for i in "${__crt__pending[@]}"; do
if is_task_allowed_to_start "${i}"; then
start_task "${i}"
(( tasks_started++ )) || :
fi
done
}
start_task() {
unset "__crt__pending[${1}]"
}
while true; do
start_allowed_tasks
[[ "${#__crt__pending[@]}" != 0 ]] || break
[[ "${tasks_started}" -gt 0 ]] || __crt__error "detected requirement loop"
done
)
__crt__args__parse() {
local remaining_args=("${@}")
while (( ${#remaining_args} )); do
if __crt__args__is_task_flag "${remaining_args[0]}"; then
__crt__args__handle_task_flag
elif __crt__args__is_group_flag "${remaining_args[0]}"; then
__crt__args__handle_group_flag
elif __crt__args__is_require_flag "${remaining_args[0]}"; then
__crt__args__handle_require_flag
elif __crt__args__is_require_all_flag "${remaining_args[0]}"; then
__crt__args__handle_require_all_flag
elif __crt__args__is_sequential_flag "${remaining_args[0]}"; then
__crt__args__handle_sequential_flag
else
__crt__error "unexpected argument '${remaining_args[0]}'"
fi
done
__crt__args__resolve_group_prereqs
__crt__args__ensure_no_requirement_loops
__crt__unset 'args'
}
__crt__args__parse "${@}"
#
# Status Updates
#
# Default to an interactive status display.
export CONCURRENT_NONINTERACTIVE=${CONCURRENT_NONINTERACTIVE:-0}
# If the terminal isn't attached to a tty, disable the interactive display.
[[ -t 1 ]] || export CONCURRENT_NONINTERACTIVE=1
# Keep track of how far we're nested inside concurrent instances.
export CONCURRENT_DEPTH=${CONCURRENT_DEPTH:--1}
(( CONCURRENT_DEPTH++ )) || :
# Default versions of these functions which can be overwritten later.
__crt__draw_meta () { :; }
__crt__start_animation () { :; }
__crt__stop_animation () { :; }
__crt__enable_echo () { :; }
__crt__disable_echo () { :; }
__crt__draw_initial_tasks () { :; }
__crt__move_cursor_to_top () { :; }
__crt__move_cursor_below_status () { :; }
__crt__draw_status () { :; }
__crt__hide_cursor () { :; }
__crt__show_cursor () { :; }
if [[ "${CONCURRENT_NONINTERACTIVE}" != "0" ]]; then
__crt__draw_status() {
# ${1} (index) is irrelevant
local code=${2}
__crt__draw_task "${code}"
}
__crt__draw_task() {
local code=${1}
if [[ "${code}" == "running" ]]; then return 0
elif [[ "${code}" == "int" ]]; then printf " %s " 'SIGINT'
elif [[ "${code}" == "skip" ]]; then printf " %s " ' SKIP '
elif [[ "${code}" == "0" ]]; then printf " %s " ' OK '
else printf " %s " 'FAILED'
fi
printf "%s %s\n" "${__crt__names[${index}]}" "${__crt__meta[${index}]}"
}
elif [[ "${CONCURRENT_DEPTH}" -le 0 ]]; then
__crt__disable_echo() {
# Disable local echo so the user can't mess up the pretty display.
stty -echo
}
__crt__enable_echo() {
# Enable local echo so user can type again. (Simply exiting the subshell
# is not sufficient to reset this, which is surprising.)
stty echo
}
__crt__hide_cursor() {
tput civis
}
__crt__show_cursor() {
tput cnorm
}
__crt__txtred='\e[0;31m' # Red
__crt__txtgrn='\e[0;32m' # Green
__crt__txtylw='\e[0;33m' # Yellow
__crt__txtblu='\e[0;34m' # Blue
__crt__txtbld='\e[1m' # Bold
__crt__txtrst='\e[0m' # Text Reset
__crt__txtclr='\e[0K' # Clear to end of line
export CONCURRENT_COMPACT=${CONCURRENT_COMPACT:-0}
__crt__use_compact_status() {
[[ "${CONCURRENT_COMPACT}" != "0" || "${__crt__task_count}" -ge "$(tput lines)" ]]
}
if __crt__use_compact_status; then
__crt__cols=$(tput cols)
__crt__draw_initial_tasks() {
# task lines + most recent update lines + summary lines
local rows=$(( __crt__task_count / __crt__cols + 4 ))
local row
for (( row = 0; row < rows; row++ )); do echo; done
tput cuu "${rows}"
tput sc
}
__crt__move_cursor_to_top() {
tput rc
}
__crt__move_cursor_to_first_task() {
__crt__move_cursor_to_top
tput cud 2
}
__crt__requires_newline_after_tasks() {
[[ $(( __crt__task_count % __crt__cols )) -ne 0 ]]
}
if __crt__requires_newline_after_tasks; then
__crt__move_cursor_below_tasks() {
__crt__move_cursor_to_index "${__crt__task_count}"
echo
}
else
__crt__move_cursor_below_tasks() {
__crt__move_cursor_to_index "${__crt__task_count}"
}
fi
__crt__move_cursor_below_status() {
__crt__move_cursor_below_tasks
echo
tput el
tput cuu 1
}
__crt__move_cursor_to_index() {
local index=${1}
local col=$(( index % __crt__cols ))
local row=$(( index / __crt__cols ))
__crt__move_cursor_to_first_task
[[ "${col}" -eq 0 ]] || tput cuf "${col}"
[[ "${row}" -eq 0 ]] || tput cud "${row}"
}
__crt__draw_status() {
local index=${1}
local code=${2}
__crt__move_cursor_to_top
__crt__draw_summary
__crt__move_cursor_to_index "${index}"
__crt__draw_task "${code}"
__crt__move_cursor_below_tasks
[[ "${code}" == "running" ]] || __crt__draw_recent_verbose_task "${index}" "${code}"
__crt__move_cursor_to_top
}
__crt__draw_task() {
local code=${1}
if [[ "${code}" == "int" ]]; then printf "${__crt__txtred}%c${__crt__txtrst}" '!'
elif [[ "${code}" == "skip" ]]; then printf "${__crt__txtylw}%c${__crt__txtrst}" '-'
elif [[ "${code}" == "running" ]]; then printf "${__crt__txtblu}%c${__crt__txtrst}" '>'
elif [[ "${code}" == "0" ]]; then printf '.'
else printf "${__crt__txtred}%c${__crt__txtrst}" 'X'
fi
}
__crt__draw_recent_verbose_task() {
local index=${1}
local code=${2}
local meta=${__crt__meta[${index}]}
if [[ "${code}" == "int" ]]; then printf "\n ${__crt__txtred}%s${__crt__txtrst} " 'SIGINT'
elif [[ "${code}" == "skip" ]]; then printf "\n ${__crt__txtylw}%s${__crt__txtrst} " ' SKIP '
elif [[ "${code}" == "0" ]]; then printf "\n ${__crt__txtgrn}%s${__crt__txtrst} " ' OK '
else printf "\n ${__crt__txtred}%s${__crt__txtrst} " 'FAILED'
fi
printf "%s" "${__crt__names[${index}]}"
if [[ -n "${meta}" ]]; then printf " ${__crt__txtbld}%s${__crt__txtrst}" "${meta}"
fi
tput el # clear to the end of the line in case the task previously displayed was longer
}
__crt__draw_summary() {
local percent=$(( ${#__crt__codes[@]} * 100 / __crt__task_count ))
local success=" ${__crt__success_task_count} passed"
local failure
local skipped
local interrupted
[[ "${__crt__failure_task_count}" -eq 0 ]] || failure=" ${__crt__failure_task_count} failed"
[[ "${__crt__skipped_task_count}" -eq 0 ]] || skipped=" ${__crt__skipped_task_count} skipped"
[[ "${__crt__interrupted_task_count}" -eq 0 ]] || interrupted=" ${__crt__interrupted_task_count} interrupted"
printf " %3d%% %s%s%s%s\n\n" \
"${percent}" \
"${success}" \
"${failure}" \
"${skipped}" \
"${interrupted}"
}
else
__crt__seconds_between_frames=1.0
__crt__running_status_current_frame=0
__crt__running_status_frames=(
" ${__crt__txtblu} =>${__crt__txtrst} "
" ${__crt__txtblu} >${__crt__txtrst} "
)
__crt__start_animation() {
__crt__update_running_status_frames
while true; do
sleep "${__crt__seconds_between_frames}"
echo "anim:" >> "${__crt__event_pipe}"
done &
__crt__animation_pid=$!
}
__crt__stop_animation() {
__crt__hide_failure kill "${__crt__animation_pid}"
__crt__hide_failure wait "${__crt__animation_pid}"
}
__crt__move_cursor_to_top() {
tput cuu "${__crt__task_count}"
tput sc
}
__crt__move_cursor_below_tasks() {
tput cud "${__crt__task_count}"
tput sc
}
__crt__move_cursor_below_status() {
__crt__move_cursor_below_tasks
}
__crt__draw_initial_tasks() {
local i
for (( i = 0; i < __crt__task_count; i++ )); do
echo " ${__crt__names[${i}]}"
done
}
__crt__move_cursor_to_index() {
local index=${1}
[[ "${index}" -eq 0 ]] || tput cud "${index}"
}
__crt__draw_status() {
local index=${1}
local code=${2}
tput rc
__crt__move_cursor_to_index "${index}"
__crt__draw_task "${code}"
tput rc
}
__crt__draw_task() {
local code=${1}
if [[ "${code}" == "running" ]]; then __crt__draw_running_status
elif [[ "${code}" == "int" ]]; then printf " ${__crt__txtred}%s${__crt__txtrst} " 'SIGINT'
elif [[ "${code}" == "skip" ]]; then printf " ${__crt__txtylw}%s${__crt__txtrst} " ' SKIP '
elif [[ "${code}" == "0" ]]; then printf " ${__crt__txtgrn}%s${__crt__txtrst} " ' OK '
else printf " ${__crt__txtred}%s${__crt__txtrst} " 'FAILED'
fi
}
__crt__draw_running_status() {
# shellcheck disable=SC2059
printf "${__crt__running_status_frames[${__crt__running_status_current_frame}]}"
}
__crt__draw_meta() {
local index=${1}
tput rc
__crt__move_cursor_to_index "${index}"
tput cuf 8 # move past status
printf "%s ${__crt__txtbld}%s${__crt__txtrst}${__crt__txtclr}" "${__crt__names[${index}]}" "${__crt__meta[${index}]}"
tput rc
}
__crt__update_running_status_frames() {
local i