-
Notifications
You must be signed in to change notification settings - Fork 1
/
tps.sh
1427 lines (1265 loc) · 45.5 KB
/
tps.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
#!/bin/bash
# A supplementary command as a tool used in TPS repositories
# Kian Mirjalali, Hamed Saleh, MohammadReza Maleki
# IOI 2017, Iran
readonly tps_version=1.6
set -e
function __tps__errcho__ {
>&2 echo "$@"
}
function __tps__variable_exists {
local -r varname="$1"; shift
declare -p "${varname}" &> "/dev/null"
}
function __tps__unified_sort__ {
local _sort
_sort="$(which -a "sort" | grep -iv "windows" | head -1)"
readonly _sort
if [ -n "${_sort}" ]; then
"${_sort}" -u "$@"
else
cat "$@"
fi
}
if [ $# -eq 0 ]; then
readonly __tps_help_mode__="true"
echo "\
TPS version ${tps_version}
Usage: tps <command> <arguments>...
"
function __tps__help_exit__ {
local -r message="$1"; shift
echo "${message}"
exit 1
}
else
readonly __tps_help_mode__="false"
__tps_command__="$1"; shift
fi
if ! "${__tps_help_mode__}" && [ "${__tps_command__}" == "--bash-completion" ]; then
readonly __tps_bash_completion_mode__="true"
function add_space_all {
local tmp
while read -r tmp; do
printf '%s \n' "${tmp}"
done
}
function add_space_options {
local tmp
while read -r tmp; do
if [[ ${tmp} != *= ]]; then
printf '%s \n' "${tmp}"
else
printf '%s\n' "${tmp}"
fi
done
}
function fix_file_endings {
local tmp
while read -r tmp; do
if [ -d "${tmp}" ]; then
printf '%s/\n' "${tmp}"
else
printf '%s \n' "${tmp}"
fi
done
}
function complete_with_files {
compgen -f -- "$1" | __tps__unified_sort__ | fix_file_endings || true
}
[ $# -gt 1 ] || exit 0
bc_index="$1"; shift
[ ${bc_index} -gt 0 ] || exit 0
readonly bc_cursor_offset="$1"; shift
[ ${bc_cursor_offset} -ge 0 ] || exit 0
# Removing the token 'tps'
shift
if [ "${bc_index}" -le $# ]; then
readonly bc_current_token="${!bc_index}"
else
readonly bc_current_token=""
fi
readonly bc_current_token_prefix="${bc_current_token:0:${bc_cursor_offset}}"
else
readonly __tps_bash_completion_mode__="false"
fi
function __tps__error_exit__ {
"${__tps_bash_completion_mode__}" && exit 0
local -r exit_code="$1"; shift
__tps__errcho__ -n "Error: "
__tps__errcho__ "$@"
exit "${exit_code}"
}
function __tps__define_python__ {
# Note: the implementation of this function should be kept synched with function __private__define_python__ in new_task_init
function __tps__check_py_cmd__ {
local -r py_cmd="$1"; shift
command -v "${py_cmd}" &> "/dev/null" || return 1
__tps__python__="${py_cmd}"
return 0
}
if __tps__variable_exists "PYTHON"; then
__tps__check_py_cmd__ "${PYTHON}" ||
__tps__error_exit__ 2 "Python command '${PYTHON}' set by environment variable 'PYTHON' does not exist."
else
__tps__check_py_cmd__ "python3" ||
__tps__check_py_cmd__ "python" ||
__tps__error_exit__ 2 "Environment variable 'PYTHON' is not set and neither of python commands 'python3' nor 'python' exists."
fi
}
function __tps_define_utility_functions__ {
# Note:
# The functions defined here
# are mostly based on the functions defined in 'scripts/internal/util.sh'
# and should be kept synched with them.
function errcho {
>&2 echo "$@"
}
# This function is not based on 'scripts/internal/util.sh'.
function error_exit {
local -r exit_code="$1"; shift
errcho -n "Error: "
errcho "$@"
exit "${exit_code}"
}
function variable_exists {
local -r varname="$1"; shift
declare -p "${varname}" &> "/dev/null"
}
function variable_not_exists {
local -r varname="$1"; shift
! variable_exists "${varname}"
}
function set_variable {
local -r var_name="$1"; shift
local -r var_value="$1"; shift
printf -v "${var_name}" '%s' "${var_value}"
}
function increment {
# Calling ((v++)) causes unexpected exit in some versions of bash if used with 'set -e'.
# Usage:
# v=3
# increment v
# increment v 2
local -r var_name="$1"; shift
if [ $# -gt 0 ]; then
local -r c="$1"; shift
else
local -r c=1
fi
set_variable "${var_name}" "$((${var_name}+c))"
}
function pushdq {
pushd "$@" > "/dev/null"
}
function popdq {
popd "$@" > "/dev/null"
}
function is_identifier_format {
local -r text="$1"; shift
local -r pattern='^[a-zA-Z_][a-zA-Z0-9_]*$'
[[ "${text}" =~ ${pattern} ]]
}
function is_unsigned_integer_format {
local -r text="$1"; shift
local -r pattern='^[0-9]+$'
[[ "${text}" =~ ${pattern} ]]
}
function is_signed_integer_format {
local -r text="$1"; shift
local -r pattern='^[+-]?[0-9]+$'
[[ "${text}" =~ ${pattern} ]]
}
function is_unsigned_decimal_format {
local -r text="$1"; shift
local -r pattern='^([0-9]+\.?[0-9]*|\.[0-9]+)$'
[[ "${text}" =~ ${pattern} ]]
}
function is_signed_decimal_format {
local -r text="$1"; shift
local -r pattern='^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)$'
[[ "${text}" =~ ${pattern} ]]
}
function command_exists {
local -r cmd_name="$1"; shift
command -v "${cmd_name}" &> "/dev/null"
}
# This is a commonly used function as an invalid_arg_callback for argument_parser.
# It assumes that a "usage" function is already defined and available during the argument parsing.
function invalid_arg_with_usage {
local -r curr_arg="$1"; shift
errcho "Error at argument '${curr_arg}':" "$@"
usage
exit 2
}
# Fetches the value of an option, while parsing the arguments of a command.
# ${curr} denotes the current token
# ${next} denotes the next token when ${next_available} is "true"
# the next token is allowed to be used when ${can_use_next} is "true"
function fetch_arg_value {
local -r __fav_local__variable_name="$1"; shift
local -r __fav_local__short_name="$1"; shift
local -r __fav_local__long_name="$1"; shift
local -r __fav_local__argument_name="$1"; shift
local __fav_local__fetched_arg_value
local __fav_local__is_fetched="false"
if [ "${curr}" == "${__fav_local__short_name}" ]; then
if "${can_use_next}" && "${next_available}"; then
__fav_local__fetched_arg_value="${next}"
__fav_local__is_fetched="true"
increment "arg_shifts"
fi
else
__fav_local__fetched_arg_value="${curr#${__fav_local__long_name}=}"
__fav_local__is_fetched="true"
fi
if "${__fav_local__is_fetched}"; then
set_variable "${__fav_local__variable_name}" "${__fav_local__fetched_arg_value}"
else
"${invalid_arg_callback}" "${curr}" "missing ${__fav_local__argument_name}"
fi
}
function fetch_nonempty_arg_value {
fetch_arg_value "$@"
local -r __fnav_local__variable_name="$1"; shift
local -r __fnav_local__short_name="$1"; shift
local -r __fnav_local__long_name="$1"; shift
local -r __fnav_local__argument_name="$1"; shift
[ -n "${!__fnav_local__variable_name}" ] ||
"${invalid_arg_callback}" "${curr}" "Given ${__fnav_local__argument_name} shall not be empty."
}
# Fetches the value of the next argument, while parsing the arguments of a command.
# ${curr} denotes the current token
# ${next} denotes the next token when ${next_available} is "true"
# the next token is allowed to be used when ${can_use_next} is "true"
function fetch_next_arg {
local -r __fna_local__variable_name="$1"; shift
local -r __fna_local__short_name="$1"; shift
local -r __fna_local__long_name="$1"; shift
local -r __fna_local__argument_name="$1"; shift
if "${can_use_next}" && "${next_available}"; then
increment "arg_shifts"
set_variable "${__fna_local__variable_name}" "${next}"
else
"${invalid_arg_callback}" "${curr}" "missing ${__fna_local__argument_name}"
fi
}
# This function parses the given arguments of a command.
# Three callback functions shall be given before passing the command arguments:
# * handle_positional_arg_callback: for handling the positional arguments
# arguments: the current command argument
# * handle_option_callback: for handling the optional arguments
# arguments: the current command argument (after separating the concatenated optional arguments, e.g. -abc --> -a -b -c)
# * invalid_arg_callback: for handling the errors in arguments
# arguments: the current command argument & error message
# Variables ${curr}, ${next}, ${next_available}, and ${can_use_next} are provided to callbacks.
function argument_parser {
local -r handle_positional_arg_callback="$1"; shift
local -r handle_option_callback="$1"; shift
local -r invalid_arg_callback="$1"; shift
local -i arg_shifts
local curr next_available next can_use_next concatenated_option_chars
while [ $# -gt 0 ]; do
arg_shifts=0
curr="$1"; shift
next_available="false"
if [ $# -gt 0 ]; then
next="$1"
next_available="true"
fi
if [[ "${curr}" == --* ]]; then
can_use_next="true"
"${handle_option_callback}" "${curr}"
elif [[ "${curr}" == -* ]]; then
if [ "${#curr}" == 1 ]; then
"${invalid_arg_callback}" "${curr}" "invalid argument"
else
concatenated_option_chars="${curr#-}"
while [ -n "${concatenated_option_chars}" ]; do
can_use_next="false"
if [ "${#concatenated_option_chars}" -eq 1 ]; then
can_use_next="true"
fi
curr="-${concatenated_option_chars:0:1}"
"${handle_option_callback}" "${curr}"
concatenated_option_chars="${concatenated_option_chars:1}"
done
fi
else
"${handle_positional_arg_callback}" "${curr}"
fi
shift "${arg_shifts}"
done
}
}
function new_task_init {
function find_file_in_given_path {
# Given a "file_name" and a colon-delimited "path",
# this function prints the first directory (in the "path") containing the specified file.
# Nothing is printed if none of the directories contain the specified file.
local -r file_name="$1"; shift
local -r path="$1"; shift
local directories
# Delimit path by colon
IFS=':' read -ra directories <<< "${path}"
local parent child
for parent in ${directories[@]+"${directories[@]}"}; do
child="${parent}/${file_name}"
if [ -n "${parent}" -a -d "${parent}" -a -e "${child}" ]; then
echo "${parent}"
return 0
fi
done
}
function union_files_in_given_path {
# Given a colon-delimited "path",
# this function prints the union of files/directories specified in the "path".
local -r path="$1"; shift
local directories
# Delimit path by colon
IFS=':' read -ra directories <<< "${path}"
local parent
for parent in ${directories[@]+"${directories[@]}"}; do
ls -1 "${parent}" 2> "/dev/null" || true
done | __tps__unified_sort__
}
if "${__tps_bash_completion_mode__}"; then
function bc_complete_templates {
local -r value_prefix="$1"; shift
__tps_define_utility_functions__
function bc_invalid_arg {
exit 0
}
function bc_handle_positional_arg {
:
}
function bc_handle_option {
local -r curr_arg="$1"; shift
local dummy
case "${curr_arg}" in
-t|--template=*) fetch_arg_value "dummy" "-t" "--template" "--"; ;;
-T|--templates-dir=*) fetch_arg_value "task_templates_dir" "-T" "--templates-dir" "--"; ;;
-D|--define) fetch_next_arg "dummy" "-D" "--define" "--"; ;;
*) ;;
esac
}
argument_parser "bc_handle_positional_arg" "bc_handle_option" "bc_invalid_arg" "$@" ""
local template_options
if variable_exists "task_templates_dir"; then
[ -d "${task_templates_dir}" ] || exit 0
template_options="$(ls -1 "${task_templates_dir}")"
else # Using TPS_TASK_TEMPLATES_PATH
variable_exists "TPS_TASK_TEMPLATES_PATH" || exit 0
template_options="$(union_files_in_given_path "${TPS_TASK_TEMPLATES_PATH}")"
fi
compgen -W "${template_options}" -- "${value_prefix}" | add_space_all || true
}
bc_init_command_options="\
--template=
--templates-dir=
--define"
if [[ ${bc_current_token_prefix} == --* ]]; then
if ! [[ ${bc_current_token_prefix} == *=* ]]; then
compgen -W "${bc_init_command_options}" -- "${bc_current_token_prefix}" | add_space_options || true
else
local -r bc_option_value_prefix="${bc_current_token_prefix#*=}"
case "${bc_current_token_prefix}" in
--template=*)
bc_complete_templates "${bc_option_value_prefix}" "$@"
;;
--templates-dir=*)
complete_with_files "${bc_option_value_prefix}"
;;
*) ;;
esac
fi
else
local -r prev_bc_index=$((bc_index-1))
local -r prev_arg="${!prev_bc_index}"
case "${prev_arg}" in
-t)
bc_complete_templates "${bc_current_token_prefix}" "$@"
;;
-T)
complete_with_files "${bc_current_token_prefix}"
;;
*) ;;
esac
fi
exit 0
fi
__tps_define_utility_functions__
function usage {
errcho -e "\
Usage: tps init <new-dir-name> [options]
\t<new-dir-name> is the name of the new directory created by this command.
\
Options:
\
-h, --help
\tShows this help.
\
-t, --template=<task-template-name>
\tSpecifies the task template name.
\tNote: Value 'default' is considered for <task-template-name> if this option is not specified.
\
-T, --templates-dir=<task-templates-dir>
\tSpecifies the directory where the task templates are stored.
\tNote:
\tIf this argument is provided, the template is searched only in the given directory.
\tOtherwise, the template is searched in the directories specified in the environment variable TPS_TASK_TEMPLATES_PATH.
\tTPS_TASK_TEMPLATES_PATH should be a colon-delimited list of paths containing task templates.
\tExample: /etc/tps/task-templates:/home/user/tps/task-templates
\
-D, --define <param-name>=<param-value>
\tPredefines the value of a task template parameter.
\tNote: This option can be used multiple times."
}
function handle_option {
local -r curr_arg="$1"; shift
case "${curr_arg}" in
-h|--help)
usage
exit 0
;;
-t|--template=*)
fetch_nonempty_arg_value "task_template_name" "-t" "--template" "task template name"
;;
-T|--templates-dir=*)
fetch_nonempty_arg_value "task_templates_dir" "-T" "--templates-dir" "task templates directory path"
;;
-D|--define)
local param_definition
fetch_next_arg "param_definition" "-D" "--define" "task template parameter value definition"
function error_param_def {
invalid_arg_with_usage "${curr_arg}" "Invalid format in task template parameter value definition '${param_definition}': $1"
}
[[ "${param_definition}" == *"="* ]] ||
error_param_def "The argument does not have the '=' sign."
local param_name param_value
IFS='=' read -r param_name param_value <<< "${param_definition}"
[ -n "${param_name}" ] ||
error_param_def "The parameter name shall not be empty."
( set_variable "${param_name}" "" &> "/dev/null" ) ||
error_param_def "The parameter name '${param_name}' is not a valid identifier."
local varname="__PREDEFINED_TPS_TEMPLATE_PARAMETER__${param_name}"
set_variable "${varname}" "${param_value}"
predefined_template_parameters+=( "${varname}" )
;;
*)
invalid_arg_with_usage "${curr_arg}" "undefined option"
;;
esac
}
function handle_positional_arg {
local -r curr_arg="$1"; shift
if variable_not_exists "output_dir_name"; then
output_dir_name="${curr_arg}"
return 0
fi
invalid_arg_with_usage "${curr_arg}" "meaningless argument"
}
argument_parser "handle_positional_arg" "handle_option" "invalid_arg_with_usage" "$@"
if variable_not_exists "output_dir_name"; then
errcho "<new-dir-name> is not specified."
usage
exit 2
fi
[ ! -e "${output_dir_name}" ] ||
error_exit 4 "Given output directory '${output_dir_name}' already exists."
function set_default_task_template_name_if_needed {
if variable_not_exists "task_template_name"; then
errcho "<task-template-name> is not specified. Using 'default' as the template."
task_template_name="default"
fi
}
if variable_exists "task_templates_dir"; then
[ -e "${task_templates_dir}" ] ||
error_exit 4 "Given task templates directory '${task_templates_dir}' does not exist."
[ -d "${task_templates_dir}" ] ||
error_exit 4 "Given task templates directory '${task_templates_dir}' is not a valid directory."
set_default_task_template_name_if_needed
task_template_dir="${task_templates_dir}/${task_template_name}"
[ -e "${task_template_dir}" ] ||
error_exit 4 "Task template '${task_template_name}' does not exist in '${task_templates_dir}'."
elif variable_exists "TPS_TASK_TEMPLATES_PATH"; then
set_default_task_template_name_if_needed
task_templates_dir="$(find_file_in_given_path "${task_template_name}" "${TPS_TASK_TEMPLATES_PATH}")"
[ -n "${task_templates_dir}" ] ||
error_exit 4 "Task template '${task_template_name}' does not exist in any of the directories specified in TPS_TASK_TEMPLATES_PATH='${TPS_TASK_TEMPLATES_PATH}'."
errcho "Found template '${task_template_name}' in '${task_templates_dir}'."
task_template_dir="${task_templates_dir}/${task_template_name}"
[ -e "${task_template_dir}" ] ||
error_exit 5 "[Illegal state] Task template '${task_template_name}' does not exist in '${task_templates_dir}'."
else
errcho "Neither <task-templates-dir> is specified nor TPS_TASK_TEMPLATES_PATH is set as an environment variable."
usage
exit 2
fi
[ -d "${task_template_dir}" ] ||
error_exit 4 "Task template '${task_template_name}' in '${task_templates_dir}' is not a valid directory."
# TTIS = task template instantiation script
TTIS_filename="task-template-instantiate.sh"
source_TTIS_file="${task_template_dir}/${TTIS_filename}"
[ -f "${source_TTIS_file}" ] ||
error_exit 14 "Task template directory '${task_template_dir}' does not contain '${TTIS_filename}' as the instantiation script."
# Preparing the environment to run TTIS
export task_template_dir output_dir_name TTIS_filename source_TTIS_file
local var
for var in ${predefined_template_parameters[@]+"${predefined_template_parameters[@]}"}; do
export "${var}"
done
export -f errcho \
error_exit \
variable_exists variable_not_exists \
set_variable increment \
pushdq popdq \
is_identifier_format \
is_unsigned_integer_format is_signed_integer_format \
is_unsigned_decimal_format is_signed_decimal_format \
command_exists
# Variables & functions defined from now on are exported & available to TTIS:
set -a
function __private__define_python__ {
# Note: the implementation of this function should be kept synched with function __tps__define_python__
function __tps__check_py_cmd__ {
local -r py_cmd="$1"; shift
command -v "${py_cmd}" &> "/dev/null" || return 1
__tps__python__="${py_cmd}"
return 0
}
if variable_exists "PYTHON"; then
__tps__check_py_cmd__ "${PYTHON}" ||
error_exit 2 "Python command '${PYTHON}' set by environment variable 'PYTHON' does not exist."
else
__tps__check_py_cmd__ "python3" ||
__tps__check_py_cmd__ "python" ||
error_exit 2 "Environment variable 'PYTHON' is not set and neither of python commands 'python3' nor 'python' exists."
fi
}
function run_python {
# Runs the python command, considering the existence of python3, python, and environment variable PYTHON
variable_exists "__tps__python__" || __private__define_python__
"${__tps__python__}" "$@"
}
function general_prompt {
# Validating function arguments
function _gperror_ {
errcho -e "\
Error in calling function '${FUNCNAME[1]}' (file: '${source_TTIS_file}', line #${BASH_LINENO[1]}): $1
Usage:
\t${FUNCNAME[1]} <var-name> <validation-command> <prompt-message> [<description>]
Prompts for a text that is validated and stored in variable <var-name>.
<var-name> must be in identifier format.
The <validation-command> is called with the user-input text as standard input
and should return 0 if the user-input is valid.
If the user-input is not valid,
the <validation-command> should write the error message in the standard error
(in addition to non-zero exit code).
Otherwise, it must write the valid form of user-input in the standard output.
So, any kind of conversion can be handled inside the <validation-command>.
The user prompt is skipped if the variable is defined using -D/--define in the command-line arguments.
The user prompt is repeated if the entered value (or the predefined) is not valid (according to the return value of <validation-function>).
The <prompt-message> is printed before each input trial.
If <description> is provided, it will also be shown to the user (only once)."
exit 11
}
[ $# -ge 3 -a $# -le 4 ] ||
_gperror_ "Incorrect number of arguments."
local -r var_name="$1"; shift
local -r validation_command="$1"; shift
local -r prompt_message="$1"; shift
local description=""
local has_description="false"
if [ $# -ge 1 ]; then
description="$1"; shift
has_description="true"
fi
readonly description
readonly has_description
is_identifier_format "${var_name}" ||
_gperror_ "Variable name '${var_name}' is not in a valid identifier format."
command_exists "${validation_command}" ||
_gperror_ "Validation command '${validation_command}' is not a callable command/function."
unset -f _gperror_
# Prompting and validating the variable value
local prompt_description
if "${has_description}"; then
prompt_description=" (${description})"
else
prompt_description=""
fi
errcho "Template parameter '${var_name}'${prompt_description}..."
local predef_var="__PREDEFINED_TPS_TEMPLATE_PARAMETER__${var_name}"
local predefined_value=""
local has_predefined_value="false"
if variable_exists "${predef_var}"; then
predefined_value="${!predef_var}"
has_predefined_value="true"
fi
local is_valid_var_value="false"
local input_var_value
local validated_var_value
until "${is_valid_var_value}"; do
if "${has_predefined_value}"; then
errcho "Parameter '${var_name}' has predefined value '${predefined_value}'."
input_var_value="${predefined_value}"
predefined_value=""
has_predefined_value="false"
else
errcho "${prompt_message}"
IFS= read -r input_var_value
fi
local validation_return_code=0
local validation_output
validation_output="$("${validation_command}" <<< "${input_var_value}")" || validation_return_code=$?
if [ "${validation_return_code}" -eq 0 ]; then
is_valid_var_value="true"
validated_var_value="${validation_output}"
fi
done
set_variable "${var_name}" "${validated_var_value}"
__PROMPT_CALLED__="true"
}
function prompt {
# Validating function arguments
function _perror_ {
errcho -e "\
Error in calling function '${FUNCNAME[1]}' (file: '${source_TTIS_file}', line #${BASH_LINENO[1]}): $1
Usage:
\t${FUNCNAME[1]} <type> <var-name> [<description>]
Prompts for a text of type <type> and stores it in variable <var-name>.
<var-name> must be in identifier format.
Valid variable types for <type>:
* string: any string of characters
* identifier: common identifier format in programming languages
* int, integer: signed integer format
* uint, unsigned_integer: unsigned integer format
* decimal: signed decimal format for real numbers
* udecimal, unsigned_decimal: unsigned decimal format for real numbers
* bool: boolean values, true (true,yes,y) and false (false,no,n)
* enum: enum value format.
The keyword 'enum' must be followed by the enum values in a format like 'enum:value1:value2:value3'.
The enum values must be in identifier format.
If <description> is provided, it will be shown to the user.
The user prompt is skipped if the variable is defined using -D/--define in the command-line arguments.
The user prompt is repeated if the entered value (or the predefined) is not valid (according to the type)."
exit 11
}
[ $# -ge 2 -a $# -le 3 ] ||
_perror_ "Incorrect number of arguments."
local -r var_type="$1"; shift
local -r var_name="$1"; shift
local -a _description_arr=()
if [ $# -ge 1 ]; then
_description_arr+=("$1"); shift
fi
readonly _description_arr
case "${var_type}" in
string|identifier|int|integer|uint|unsigned_integer|decimal|udecimal|unsigned_decimal|bool) ;;
enum:*)
local enum_values
IFS=':' read -ra enum_values <<< "${var_type}"
# Removing the 'enum' keyword from the array
enum_values=("${enum_values[@]:1}")
[ ${#enum_values[@]} -ge 1 ] ||
_perror_ "Enum types must have at least one value."
local enum_val
for enum_val in "${enum_values[@]}"; do
is_identifier_format "${enum_val}" ||
_perror_ "Enum value '${enum_val}' does not have a valid identifier format."
done
local enum_values_str
enum_values_str="$(sed -e 's/:/, /g' <<< "${var_type:5}")"
;;
enum) _perror_ "Enum values are required to be in a format like 'enum:value1:value2:value3'."; ;;
*) _perror_ "Unknown type: '${var_type}'"; ;;
esac
is_identifier_format "${var_name}" ||
_perror_ "Variable name '${var_name}' is not in a valid identifier format."
unset -f _perror_
local pmessage
case "${var_type}" in
enum:*) pmessage="Enter a value among {${enum_values_str}} for '${var_name}':"; ;;
*) pmessage="Enter a value of type '${var_type}' for '${var_name}':"; ;;
esac
local val_command
function _define_simple_val_command {
__simple_val_command__format_validation_func="$1"; shift
__simple_val_command__validation_error_msg="$1"; shift
function val_command {
local var_value
var_value="$(cat)"
if "${__simple_val_command__format_validation_func}" "${var_value}"; then
echo -n "${var_value}"
return 0
else
errcho "${__simple_val_command__validation_error_msg}"
return 1
fi
}
}
case "${var_type}" in
string)
function val_command {
cat
return 0
}
;;
identifier)
_define_simple_val_command "is_identifier_format" "Text not in identifier format."
;;
int|integer)
_define_simple_val_command "is_signed_integer_format" "Invalid signed integer value."
;;
uint|unsigned_integer)
_define_simple_val_command "is_unsigned_integer_format" "Invalid unsigned integer value."
;;
decimal)
_define_simple_val_command "is_signed_decimal_format" "Invalid signed decimal value."
;;
udecimal|unsigned_decimal)
_define_simple_val_command "is_unsigned_decimal_format" "Invalid unsigned decimal value."
;;
bool)
function val_command {
local var_value
var_value="$(cat)"
case "${var_value}" in
true|yes|y)
echo -n "true"
return 0
;;
false|no|n)
echo -n "false"
return 0
;;
*)
errcho "Invalid boolean value. Valid values: true, false, yes, no, y, n"
return 1
;;
esac
}
;;
enum:*)
function val_command {
local var_value
var_value="$(cat)"
local enum_val
for enum_val in "${enum_values[@]}"; do
if [ "${var_value}" == "${enum_val}" ]; then
echo -n "${var_value}"
return 0
fi
done
errcho "Invalid enum value. Valid values: ${enum_values_str}"
return 1
}
;;
*) error_exit 15 "[Illegal state] The program execution must not reach here!"; ;;
esac
general_prompt "${var_name}" "val_command" "${pmessage}" ${_description_arr[@]+"${_description_arr[@]}"}
}
function generate_random_string {
# Validating function arguments
function _gerror_ {
local -r exit_code="$1"; shift
local -r msg="$1"; shift
errcho -e "\
Error in calling function '${FUNCNAME[1]}' (file: '${source_TTIS_file}', line #${BASH_LINENO[1]}): ${msg}
Usage:
\t${FUNCNAME[1]} <string-length> <string-character-set> <random-seed>
Generates a string of length <string-length> with characters of <string-character-set>.
The parameter <string-length> must be a nonnegative integer.
The parameter <random-seed> can be any string."
exit "${exit_code}"
}
[ $# -eq 3 ] ||
_gerror_ 11 "Incorrect number of arguments."
local -r len="$1"; shift
local -r charset="$1"; shift
local -r random_seed="$1"; shift
is_unsigned_integer_format "${len}" ||
_gerror_ 11 "The string length is not a nonnegative integer."
local -r py_prog="\
import sys
import random
str_len = int(sys.argv[1])
str_charset = sys.argv[2]
rand_seed = sys.argv[3]
random.seed(rand_seed)
print(''.join(random.choice(str_charset) for _ in range(str_len)))
"
run_python -c "${py_prog}" "${len}" "${charset}" "${random_seed}"
}
function clone_template_directory {
variable_exists "__PROMPT_CALLED__" ||
errcho "Warning: No task template parameters are prompted in '${source_TTIS_file}'."
errcho "Copying task template '${task_template_dir}' to the new directory '${output_dir_name}'..."
cp -R "${task_template_dir}" "${output_dir_name}"
# Removing task template related files from '${output_dir_name}'
rm -f "${output_dir_name}/${TTIS_filename}"
errcho "Done."
errcho "Entering the new directory '${output_dir_name}'"
cd "${output_dir_name}"
__TPS_INIT_CLONE_CALLED__="true"
}
function __check_clone_template_directory_called__ {
[ "${__TPS_INIT_CLONE_CALLED__-false}" == "true" ] ||
error_exit 12 "Error in calling function '${FUNCNAME[1]}'
Function 'clone_template_directory' is not called yet."
}
function py_regex_replace_in_files {
__check_clone_template_directory_called__
# Validating function arguments
[ $# -ge 2 ] ||
error_exit 11 "Incorrect number of arguments in calling function '${FUNCNAME[0]}'
Usage: ${FUNCNAME[0]} <pattern> <substitute> <file-paths>..."
local pattern="$1"; shift
local substitute="$1"; shift
local -ra file_paths=("$@")
local file_path
for file_path in ${file_paths[@]+"${file_paths[@]}"}; do
[ -e "${file_path}" ] ||
error_exit 14 "Given file '${file_path}' does not exist."
[ -f "${file_path}" ] ||
error_exit 14 "Given file '${file_path}' is not an ordinary file."
done
# Adding an extra character to the arguments (and removing it in the python script)
# in order to suppress the wrong path translation that happens in some environments (like msys).
pattern="A${pattern}"
substitute="A${substitute}"
local -r py_prog="\
import sys
import re
pattern = sys.argv[1]
substitute = sys.argv[2]
file_path = sys.argv[3]
tmp_file_path = sys.argv[4]
pattern = pattern[1:]
substitute = substitute[1:]
with open(file_path, 'r') as infile:
content = infile.read()
with open(tmp_file_path, 'w', newline='') as outfile:
outfile.write(re.sub(pattern, substitute, content))
"
for file_path in ${file_paths[@]+"${file_paths[@]}"}; do
local tmp_file_path="${file_path}.replace_tmp"
run_python -c "${py_prog}" "${pattern}" "${substitute}" "${file_path}" "${tmp_file_path}"
mv "${tmp_file_path}" "${file_path}"
done
}
function __private__escape_sed_substitute_first_arg {
local -r first_arg="$1"; shift
# Source: https://stackoverflow.com/a/29613573
sed -e 's/[^^]/[&]/g' -e 's/\^/\\^/g' <<< "${first_arg}"
# Alternative: sed -e 's/[]\/$*.^[]/\\&/g' <<< "${first_arg}"
}
function __private__escape_sed_substitute_second_arg {
local -r second_arg="$1"; shift
sed -e 's/[&\/]/\\&/g' <<< "${second_arg}"
}
function __private__replace_in_text__ {
local -r old_text="$1"; shift
local -r new_text="$1"; shift
local -r text_to_change="$1"; shift
local escaped_old_text
escaped_old_text="$(__private__escape_sed_substitute_first_arg "${old_text}")"
readonly escaped_old_text
local escaped_new_text
escaped_new_text="$(__private__escape_sed_substitute_second_arg "${new_text}")"
readonly escaped_new_text
sed -e "s/${escaped_old_text}/${escaped_new_text}/g" <<< "${text_to_change}"
}
function __private__replace_in_file_content__ {
local -r old_text="$1"; shift
local -r new_text="$1"; shift
local -r file_path="$1"; shift
local escaped_old_text
escaped_old_text="$(__private__escape_sed_substitute_first_arg "${old_text}")"
readonly escaped_old_text
local escaped_new_text
escaped_new_text="$(__private__escape_sed_substitute_second_arg "${new_text}")"
readonly escaped_new_text
# Do not try to delete the backup removal code by omitting '.sed_tmp'.