-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoemaker.sh
executable file
·1864 lines (1495 loc) · 70.5 KB
/
shoemaker.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 sh
set -e
#/*
# * This script is based on TangoMan Shoe Shell Microframework
# *
# * This file is distributed under to the MIT license.
# *
# * Copyright (c) 2025 "Matthias Morin" <mat@tangoman.io>
# *
# * Permission is hereby granted, free of charge, to any person obtaining a copy
# * of this software and associated documentation files (the "Software"), to deal
# * in the Software without restriction, including without limitation the rights
# * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# * copies of the Software, and to permit persons to whom the Software is
# * furnished to do so, subject to the following conditions:
# *
# * The above copyright notice and this permission notice shall be included in all
# * copies or substantial portions of the Software.
# *
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# * SOFTWARE.
# *
# * Source code is available here: https://github.com/TangoMan75/shoe
# */
## TangoMan Shoemaker
##
## A versatile developement tool to split a script file into its components,
## build a script from a build.lst file, generate Makefile or Markdown
## documentation from a shoe script.
##
## @author "Matthias Morin" <mat@tangoman.io>
## @version 1.0.0
## @license MIT
## @link https://github.com/TangoMan75/shoe
## @update https://raw.githubusercontent.com/TangoMan75/shoe/refs/heads/main/shoemaker.sh
#--------------------------------------------------
# Place your constants after this line
#--------------------------------------------------
#--------------------------------------------------
# Place your options after this line
#--------------------------------------------------
## Source file /~?[a-zA-Z0-9./_-]+/
file=''
## Destination directory /~?[a-zA-Z0-9/._-]+/
destination=''
## Script type /(shell|bash)/
type=shell
#--------------------------------------------------
# Place your flags after this line
#--------------------------------------------------
## Document private elements
private=false
#--------------------------------------------------
# Place your private constants after this line
#--------------------------------------------------
#--------------------------------------------------
# Place your global variables after this line
#--------------------------------------------------
#--------------------------------------------------
# Place your functions after this line
#--------------------------------------------------
## Dump functions
dump() {
if [ ! -f "${file}" ]; then
echo_danger "error: \"${file}\" file not found\n"
return 1
fi
file="$(realpath "${file}")"
if [ -z "${destination}" ]; then
destination="$(_pwd)/dump/$(basename "${file}" .sh)"
fi
if [ ! -d "${destination}" ]; then
mkdir -p "${destination}"
fi
destination="$(realpath "${destination}")"
echo_success "Dumping functions from \"${file}\" to \"${destination}\"\n"
for name in $(_get_functions_names "${file}" true); do
printf "#!/bin/sh
\n\n" > "${destination}/${name}.sh"
_get_function "${file}" "${name}" >> "${destination}/${name}.sh"
done
}
## Dump "build.lst" file
list() {
if [ ! -f "${file}" ]; then
echo_danger "error: \"${file}\" file not found\n"
return 1
fi
file="$(realpath "${file}")"
if [ -z "${destination}" ]; then
destination="$(_pwd)/dump/$(basename "${file}" .sh)"
fi
destination="$(realpath "${destination}")"
if [ ! -d "${destination}" ]; then
mkdir -p "${destination}"
fi
echo_success "Listing functions from \"${file}\" to \"${destination}/$(basename "${file}" .sh).lst\"\n"
_get_functions_names "${file}" true | sed 's/$/.sh/g' > "${destination}/$(basename "${file}" .sh).lst"
}
## Build from given "build.lst" file
build() {
if [ ! -f "${file}" ]; then
echo_danger "error: \"${file}\" file not found\n"
return 1
fi
file="$(realpath "${file}")"
if [ -z "${destination}" ]; then
destination="$(_pwd)/build"
fi
destination="$(realpath "${destination}")"
if [ ! -d "${destination}" ]; then
mkdir -p "${destination}"
fi
_build "${file}" "${destination}" "${type}"
}
## Build all scripts
build_all() {
build_folder="$(_pwd)/build"
if [ ! -d "${build_folder}" ]; then
mkdir -p "${build_folder}"
fi
find "$(_pwd)/recipes" -type f -name '*.lst' | while read -r file_path
do
_build "${file_path}" "${build_folder}" "${type}"
done
}
##################################################
### Documentation
##################################################
## Generate Markdown documentation for provided shoe script
generate_doc() {
_generate_doc "$(realpath "${file}")" "${destination}" "$(basename "${file}" .sh).md" "${private}"
}
## Generate Markdown documentation for all scripts
generate_doc_all() {
doc_folder="$(_pwd)/doc"
if [ ! -d "${doc_folder}" ]; then
mkdir -p "${doc_folder}"
fi
find "$(_pwd)/build" -type f -name '*.sh' | while read -r file_path
do
_generate_doc "${file_path}" "${doc_folder}" "$(basename "${file_path}" .sh).md" "${private}"
done
}
##################################################
### Makefile
##################################################
## Generate Markdown documentation for provided shoe script
generate_makefile() {
_generate_makefile "$(realpath "${file}")" "${destination}" "$(basename "${file}" .sh).makefile"
}
## Generate Makefile for all scripts
generate_makefile_all() {
build_folder="$(_pwd)/build"
if [ ! -d "${build_folder}" ]; then
mkdir -p "${build_folder}"
fi
find "${build_folder}" -type f -name '*.sh' | while read -r file_path
do
_generate_makefile "${file_path}" "${build_folder}" "$(basename "${file_path}" .sh).makefile"
done
}
#--------------------------------------------------
# Place your private functions after this line
#--------------------------------------------------
# Build from given "build.lst" file
_build() {
# Synopsis: _build <FILE_PATH> <DESTINATION> [TYPE]
# FILE_PATH: The path to the input file.
# DESTINATION: The path to the destination folder.
# TYPE: (optional) The script type to build (bash or sh). Will default to "sh".
if [ -z "$1" ] || [ -z "$2" ]; then echo_danger 'error: _build: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 3 ]; then echo_danger "error: _build: too many arguments (${#})\n"; return 1; fi
if [ ! -f "$1" ]; then echo_danger "error: _build: \"$1\" file not found\n"; return 1; fi
if [ ! -d "$2" ]; then echo_danger "error: _build: \"$2\" folder not found\n"; return 1; fi
set -- "$(realpath "$1")" "$(realpath "$2")" "$3"
__output__="$2/$(basename "$1" .lst).sh"
alert_primary "Building $(basename "$1" .lst).sh"
echo_info "rm \"${__output__}\" || true\n"
rm "${__output__}" || true
# get all pathes from build.lst file ignoring comments and empty lines
# shellcheck disable=SC2094
< "$1" grep -Pv '^(#|\s*$)' | while read -r file_path;
do
# shellcheck disable=SC2094
__source_file__="$(dirname "$1")/${file_path}"
echo_info "${__source_file__}\n"
# append file content to "script" build
printf '%s\n' "$(cat "${__source_file__}")" >> "${__output__}"
done
# Remove all "#!/bin/bash" or "#!/bin/sh" from result file
echo_info "$(_sed_i) -r 's/^#!\/bin\/(bash|sh)$//g' \"${__output__}\"\n"
$(_sed_i) -r 's/^#!\/bin\/(bash|sh)$//g' "${__output__}"
# Prepend shebang
if [ "$3" = bash ]; then
echo_info "$(_sed_i) '1i#!\/usr\/bin\/env bash' \"${__output__}\"\n"
$(_sed_i) '1i#!\/usr\/bin\/env bash' "${__output__}"
else
echo_info "$(_sed_i) '1i#!\/usr\/bin\/env sh' \"${__output__}\"\n"
$(_sed_i) '1i#!\/usr\/bin\/env sh' "${__output__}"
fi
_collapse_blank_lines "${__output__}"
echo_success "\"${__output__}\" generated\n"
}
##################################################
### Self Install
##################################################
## Install script and enable completion
self_install() {
_install "$0" "${ALIAS}" "${global:-false}"
}
## Uninstall script from system
self_uninstall() {
_uninstall "$0" "${ALIAS}"
}
## Update script from @update
self_update() {
_annotations="$(_get_script_shoedoc "$0")"
_update_link="$(_get_annotation_tags "${_annotations}" 'update')"
if [ -z "${_update_link}" ]; then
echo_danger 'cannot update: "@update" missing\n'
return 1
fi
_update "$0" "${_update_link}" "${ALIAS}" "${global:-false}"
}
##################################################
### Help
##################################################
## Print this help
help() {
_help "$0"
}
#--------------------------------------------------
#_ Hooks
#--------------------------------------------------
# Place here commands you need executed by default (optional)
_default() {
help
}
# Place here commands you need executed first every time (optional)
_before() {
_check_installed awk
_check_installed sed
}
# Place here commands you need executed last every time (optional)
_after() {
return 0
}
###################################################
# TangoMan Shoe Shell Microframework
###################################################
#--------------------------------------------------
# Generated code : Do not edit below this line
#--------------------------------------------------
#--------------------------------------------------
#_ Bashdoc
#--------------------------------------------------
# Get shoedoc description
_get_shoedoc_description() {
# Synopsis: _get_shoedoc_description <TEXT>
# TEXT: The input shoedoc annotation bloc.
if [ -z "$1" ]; then echo_danger 'error: _get_shoedoc_description: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 1 ]; then echo_danger "error: _get_shoedoc_description: too many arguments (${#})\n"; return 1; fi
printf '%s' "$1" | awk '/^#.*/ {
if (substr($2,1,1) != "@") {
RESULT=substr($0,length($1)+2); # remove leading pound character(s)
count+=1;
if (count==2 && RESULT=="") next;
if (count>1) print RESULT
}
}'
}
# Get shoedoc
_get_shoedoc() {
# Synopsis: _get_shoedoc <TEXT>
# TEXT: The input shoedoc annotation bloc.
# Remove every line that does not start with a pound character or contains a tag
# Returns string without leading pound characters
if [ -z "$1" ]; then echo_danger 'error: _get_shoedoc: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 1 ]; then echo_danger "error: _get_shoedoc: too many arguments (${#})\n"; return 1; fi
printf '%s' "$1" | awk '/^#.*/ {
if (substr($2,1,1) != "@") {
RESULT=substr($0,length($1)+2); # remove leading pound character(s)
print RESULT
}
}'
}
# Return given tag values from shoedoc bloc
_get_shoedoc_tag() {
# Synopsis: _get_shoedoc_tag <TEXT> <TAG_NAME>
# TEXT: The input shoedoc annotation bloc.
# TAG_NAME: The name of tag to return.
if [ -z "$1" ] || [ -z "$2" ]; then echo_danger 'error: _get_shoedoc_tag: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 2 ]; then echo_danger "error: _get_shoedoc_tag: too many arguments (${#})\n"; return 1; fi
printf '%s' "$1" | awk -v TAG="$2" '/^#.*/ {
if ($2=="@"TAG) {
gsub(" +"," "); sub("^ +",""); sub(" +$",""); # trim input
RESULT=substr($0,length($1)+length($2)+3); # remove leading pound character(s)
print RESULT
}
}'
}
# Get shoedoc title
_get_shoedoc_title() {
# Synopsis: _get_shoedoc_title <TEXT>
# TEXT: The input shoedoc annotation bloc.
# Returns the first line that does not contain a tag
if [ -z "$1" ]; then echo_danger 'error: _get_shoedoc_title: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 1 ]; then echo_danger "error: _get_shoedoc_title: too many arguments (${#})\n"; return 1; fi
printf '%s' "$1" | awk '/^#.*/ {
if (substr($2,1,1) != "@") {
RESULT=substr($0,length($1)+2); # remove leading pound character(s)
print RESULT; exit
}
}'
}
# Get shoedoc bloc at the top the provided shoe script file
_get_script_shoedoc() {
# Synopsis: _get_script_shoedoc <FILE_PATH>
# FILE_PATH: The path to the input file.
# note: Each shoedoc should strictly start with two pound signs (##)
# Returns the first valid docbloc found in the provided file
if [ -z "$1" ]; then echo_danger 'error: _get_script_shoedoc: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 1 ]; then echo_danger "error: _get_script_shoedoc: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")"
if [ ! -f "$1" ]; then echo_danger "error: _get_script_shoedoc: \"$1\" file not found\n"; return 1; fi
awk '/^## */ {count+=1; print} !/^## */ {if (count>1) exit}' "$1"
}
#--------------------------------------------------
#_ Colors
#--------------------------------------------------
# shellcheck disable=SC2034
# PRIMARY: bright white text
PRIMARY='\033[97m'
# SECONDARY: bright blue text
SECONDARY='\033[94m'
# SUCCESS: bright green text
SUCCESS='\033[32m'
# DANGER: red text
DANGER='\033[31m'
# WARNING: orange text
WARNING='\033[33m'
# INFO: bright purple text
INFO='\033[95m'
# LIGHT: black text over white background
LIGHT='\033[47;90m'
# DARK: white text over black background
DARK='\033[40;37m'
# DEFAULT: reset formatting
DEFAULT='\033[0m'
# EOF: reset formatting and carriage return
EOL='\033[0m\n'
# shellcheck disable=SC2034
# ALERT_PRIMARY: bold white text over bright blue background
ALERT_PRIMARY='\033[1;104;97m'
# ALERT_SECONDARY: bold white text over bright purple background
ALERT_SECONDARY='\033[1;45;97m'
# ALERT_SUCCESS: bold white text over bright green background
ALERT_SUCCESS='\033[1;42;97m'
# ALERT_DANGER: bold white text over bright red background
ALERT_DANGER='\033[1;41;97m'
# ALERT_WARNING: bold white text over bright orange background
ALERT_WARNING='\033[1;43;97m'
# ALERT_INFO: bold white text over bright blue background
ALERT_INFO='\033[1;44;97m'
# ALERT_LIGHT: bold grey text over white background
ALERT_LIGHT='\033[1;47;90m'
# ALERT_DARK: bold white text over black background
ALERT_DARK='\033[1;40;37m'
# Print primary text with optional indentation and padding
echo_primary() {
# Synopsis: echo_primary <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
# note: Older versions of printf supports a more limited set of format specifiers (eg: "%-*b"),
# this is why we're calculating the PADDING length on each execution.
set -- "$1" "${2:-0}" "$((${3:-0}-${#1}))"
if [ "$3" -lt 0 ]; then set -- "$1" "$2" 0; fi
printf "%*s${PRIMARY}%b${DEFAULT}%*s" "$2" '' "$1" "$3" ''
}
# Print secondary text with optional indentation and padding
echo_secondary() {
# Synopsis: echo_secondary <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
set -- "$1" "${2:-0}" "$((${3:-0}-${#1}))"
if [ "$3" -lt 0 ]; then set -- "$1" "$2" 0; fi
printf "%*s${SECONDARY}%b${DEFAULT}%*s" "$2" '' "$1" "$3" ''
}
# Print success text with optional indentation and padding
echo_success() {
# Synopsis: echo_success <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
set -- "$1" "${2:-0}" "$((${3:-0}-${#1}))"
if [ "$3" -lt 0 ]; then set -- "$1" "$2" 0; fi
printf "%*s${SUCCESS}%b${DEFAULT}%*s" "$2" '' "$1" "$3" ''
}
# Print danger text with optional indentation and padding
echo_danger() {
# Synopsis: echo_danger <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
set -- "$1" "${2:-0}" "$((${3:-0}-${#1}))"
if [ "$3" -lt 0 ]; then set -- "$1" "$2" 0; fi
printf "%*s${DANGER}%b${DEFAULT}%*s" "$2" '' "$1" "$3" ''
}
# Print warning text with optional indentation and padding
echo_warning() {
# Synopsis: echo_warning <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
set -- "$1" "${2:-0}" "$((${3:-0}-${#1}))"
if [ "$3" -lt 0 ]; then set -- "$1" "$2" 0; fi
printf "%*s${WARNING}%b${DEFAULT}%*s" "$2" '' "$1" "$3" ''
}
# Print info text with optional indentation and padding
echo_info() {
# Synopsis: echo_info <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
set -- "$1" "${2:-0}" "$((${3:-0}-${#1}))"
if [ "$3" -lt 0 ]; then set -- "$1" "$2" 0; fi
printf "%*s${INFO}%b${DEFAULT}%*s" "$2" '' "$1" "$3" ''
}
# Print light text with optional indentation and padding
echo_light() {
# Synopsis: echo_light <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
set -- "$1" "${2:-0}" "$((${3:-0}-${#1}))"
if [ "$3" -lt 0 ]; then set -- "$1" "$2" 0; fi
printf "%*s${LIGHT}%b${DEFAULT}%*s" "$2" '' "$1" "$3" ''
}
# Print dark text with optional indentation and padding
echo_dark() {
# Synopsis: echo_dark <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
set -- "$1" "${2:-0}" "$((${3:-0}-${#1}))"
if [ "$3" -lt 0 ]; then set -- "$1" "$2" 0; fi
printf "%*s${DARK}%b${DEFAULT}%*s" "$2" '' "$1" "$3" ''
}
# Print primary alert
alert_primary() {
# Synopsis: alert_primary <STRING>
# STRING: Text to display.
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_PRIMARY}" '' "${ALERT_PRIMARY}" "$1" "${ALERT_PRIMARY}" ''
}
# Print secondary alert
alert_secondary() {
# Synopsis: alert_secondary <STRING>
# STRING: Text to display.
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_SECONDARY}" '' "${ALERT_SECONDARY}" "$1" "${ALERT_SECONDARY}" ''
}
# Print success alert
alert_success() {
# Synopsis: alert_success <STRING>
# STRING: Text to display.
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_SUCCESS}" '' "${ALERT_SUCCESS}" "$1" "${ALERT_SUCCESS}" ''
}
# Print danger alert
alert_danger() {
# Synopsis: alert_danger <STRING>
# STRING: Text to display.
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_DANGER}" '' "${ALERT_DANGER}" "$1" "${ALERT_DANGER}" ''
}
# Print warning alert
alert_warning() {
# Synopsis: alert_warning <STRING>
# STRING: Text to display.
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_WARNING}" '' "${ALERT_WARNING}" "$1" "${ALERT_WARNING}" ''
}
# Print info alert
alert_info() {
# Synopsis: alert_info <STRING>
# STRING: Text to display.
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_INFO}" '' "${ALERT_INFO}" "$1" "${ALERT_INFO}" ''
}
# Print light alert
alert_light() {
# Synopsis: alert_light <STRING>
# STRING: Text to display.
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_LIGHT}" '' "${ALERT_LIGHT}" "$1" "${ALERT_LIGHT}" ''
}
# Print dark alert
alert_dark() {
# Synopsis: alert_dark <STRING>
# STRING: Text to display.
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_DARK}" '' "${ALERT_DARK}" "$1" "${ALERT_DARK}" ''
}
#--------------------------------------------------
#_ System compatibility
#--------------------------------------------------
# Return sed -i system flavour
_sed_i() {
# Synopsis: _sed_i
if [ "$(uname)" = 'Darwin' ] && [ -n "$(command -v sed)" ] && [ -z "$(sed --version 2>/dev/null)" ]; then
echo "sed -i ''"
return 0
fi
echo 'sed -i'
}
#--------------------------------------------------
#_ Documentation
#--------------------------------------------------
# Generate Markdown documentation for provided shoe script
_generate_doc() {
# Synopsis: _generate_doc <INPUT_FILE_PATH> [DESTINATION] [OUTPUT_FILE_NAME] [GET_PRIVATE]
# INPUT_FILE_PATH: The path to the input file.
# DESTINATION: (optional) The path to the destination folder. Defaults to file parent.
# OUTPUT_FILE_NAME: (optional) The name for the documentation file. Defaults to "<BASENAME>.md".
# GET_PRIVATE: (Optional) If set to 'true', documents private constants, options, flags, and commands as well. Defaults to "false".
if [ -z "$1" ]; then echo_danger 'error: _generate_doc: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 4 ]; then echo_danger "error: _generate_doc: too many arguments (${#})\n"; return 1; fi
# set default values
set -- "$(realpath "$1")" "${2:-"$(realpath "$(dirname "$1")")"}" "${3:-"$(basename "$1" .sh).md"}" "${4:-false}"
if [ ! -f "$1" ]; then echo_danger "error: _generate_doc: \"$1\" file not found\n"; return 1; fi
if [ ! -d "$2" ]; then echo_danger "error: _generate_doc: \"$2\" folder not found\n"; return 1; fi
# check valid input file type
if [ "$(printf '%s' "$1" | grep -oE '\.[a-zA-Z0-9]+$')" != .sh ]; then echo_danger 'error: _generate_doc: file should be type ".sh"\n'; return 1; fi
alert_primary "Generating $3"
(
__annotations__=$(_get_script_shoedoc "$1")
printf '%s\n===\n\n' "$(_get_shoedoc_title "${__annotations__}")"
printf '## ℹ️ Infos\n\n'
printf '\055 author: %s\n' "$(_get_shoedoc_tag "${__annotations__}" 'author')"
printf '\055 version: %s\n' "$(_get_shoedoc_tag "${__annotations__}" 'version')"
printf '\055 link: %s\n' "$(_get_shoedoc_tag "${__annotations__}" 'link' )"
printf '\n\n'
printf '## 📑 Description\n\n'
printf '%s\n\n' "$(_get_shoedoc_description "${__annotations__}")"
printf '## 🔥 Usage\n\n'
printf '`sh %s [command] ' "$(basename "$1")"
awk -F '=' '/^[a-zA-Z0-9_]+=.+$/ {
if (substr(PREV,1,3) != "## " || $1 == toupper($1) || substr($1,1,1) == "_") next;
if ($2 == "false") {printf "(--%s) ",$1;next}
printf "(--%s %s) ",$1,$2
} {PREV = $0}' "$1" | sed 's/ $//'
printf '`\n\n'
if [ -n "$(_get_constants "$1")" ]; then
printf '## 🧱 Constants\n\n'
awk -v GET_PRIVATE="$4" -F '=' '/^[A-Z0-9_]+=.+$/ {
if (GET_PRIVATE == "false" && (substr(PREV,1,3) != "## " || substr($0,1,1) == "_")) next;
PRIVATE = "";
if (substr(PREV,1,3) != "## " || substr($1,1,1) == "_") PRIVATE = " (private)";
sub("^#+ ","",PREV);
printf "%d. **%s%s**\n> %s\n - Value: _%s_\n\n",++i,$1,PRIVATE,PREV,$2
} {PREV = $0}' "$1"
fi
if [ "$4" = true ] && [ -n "$(_get_options "$1" true)" ]; then
printf '## ⚙️ Global Variables\n\n'
awk -F '=' '/^[a-zA-Z0-9_]+=.+$/ {
if (substr($1,1,1) != "_" || $1 == toupper($1)) next;
printf "%d. **`%s` (private)**\n> %s\n - Default: _%s_\n\n",++i,$1,substr(PREV,4),$2
} {PREV = $0}' "$1"
fi
if [ -n "$(_get_flags "$1")" ]; then
printf '## 🚩 Flags\n\n'
awk -F '=' '/^[a-zA-Z0-9_]+=false$/ {
if (substr(PREV,1,3) != "## " || substr($1,1,1) == "_" || $1 == toupper($1)) next;
printf "%d. **`--%s`**\n> %s\n\n",++i,$1,substr(PREV,4)
} {PREV = $0}' "$1"
fi
if [ -n "$(_get_options "$1")" ]; then
printf '## ⚙️ Options\n\n'
awk -F '=' '/^[a-zA-Z0-9_]+=.+$/ {
if (substr(PREV,1,3) != "## " || substr($1,1,1) == "_" || $1 == toupper($1) || $2 == "false") next;
if (match(PREV,/ \/.+\//)) { # check option has constraint
CONSTRAINT=substr(PREV,RSTART+1,RLENGTH);
DESCRIPTION=substr(PREV,4,length(PREV)-length(CONSTRAINT)-3);
printf "%d. **`--%s`**\n> %s\n - Constraint: `%s`\n - Default: _%s_",++i,$1,DESCRIPTION,CONSTRAINT,$2
} else {
printf "%d. **`%s`**\n> %s\n - Default: _%s_",++i,$1,substr(PREV,4),$2
}
printf "\n\n"
} {PREV = $0}' "$1"
fi
printf '## 🤖 Commands\n\n'
awk -v GET_PRIVATE="$4" '/^#(##|_) / { # print section / divider
if (GET_PRIVATE == "false" && substr(PREV,1,4) != "### ") next;
sub("^#(##|_) ","");
i=0; printf"### ⚡ %s\n\n",$0
}
/^(function )?[a-zA-Z0-9_]+ *\(\) *\{/ { # find function
if (GET_PRIVATE == "false" && (substr(PREV,1,3) != "## " || substr($0,1,1) == "_")) next;
PRINT_SYNOPSIS="true";
sub("^function ",""); gsub("[ ()]","");
FUNCTION = substr($0,1,index($0,"{"));
sub("{$","",FUNCTION);
PRIVATE = "";
if (substr(PREV,1,3) != "## " || substr(FUNCTION,1,1) == "_") PRIVATE = " (private)";
if (match(PREV,/^#.+/)) { # print function
sub("^##? ","",PREV);
printf "#### %d. `%s`%s\n\n%s\n",++i,FUNCTION,PRIVATE,PREV
} else {
printf "#### %d. `%s`%s\n",++i,FUNCTION,PRIVATE
}
}
/^ +# .+/ { # print synopsis
if (PRINT_SYNOPSIS == "false") next;
sub("^ +# ",""); gsub("<","\<"); gsub(">","\>"); printf "> %s<br>\n",$0
}
/^$/ {
if (PRINT_SYNOPSIS == "true") {
printf "\n";
PRINT_SYNOPSIS="false"
}
} {PREV = $0}' "$1"
) > "$2/$3"
echo_success "Documentation generated : \"$2/$3\"\n"
}
#--------------------------------------------------
#_ Help
#--------------------------------------------------
# Print help for provider shoe script
_help() {
# Synopsis: _help <FILE_PATH>
# FILE_PATH: The path to the input file.
if [ -z "$1" ]; then echo_danger 'error: _help: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 1 ]; then echo_danger "error: _help: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")"
if [ ! -f "$1" ]; then echo_danger "error: _help: \"$1\" file not found\n"; return 1; fi
__padding__=$(_get_padding "$1")
__annotations__=$(_get_script_shoedoc "$1")
alert_primary "$(_get_shoedoc_title "${__annotations__}")"
_print_infos "$1"
_print_description "$(_get_shoedoc_description "${__annotations__}")"
_print_usage "$1"
if [ -n "$(_get_constants "$1")" ]; then
_print_constants "$1" "${__padding__}"
fi
if [ -n "$(_get_flags "$1")" ]; then
_print_flags "$1" "${__padding__}"
fi
if [ -n "$(_get_options "$1")" ]; then
_print_options "$1" "${__padding__}"
fi
_print_commands "$1" "${__padding__}"
}
# List commands of the provided shoe script (used by "help" command)
_print_commands() {
# Synopsis: _print_commands <FILE_PATH> [PADDING]
# FILE_PATH: The path to the input file.
# PADDING: (optional) Padding length (default: 12)
# note: "awk: %*x formats are not supported"
if [ -z "$1" ]; then echo_danger 'error: _print_commands: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 2 ]; then echo_danger "error: _print_commands: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")" "${2:-12}"
if [ ! -f "$1" ]; then echo_danger "error: _print_commands: \"$1\" file not found\n"; return 1; fi
echo_warning 'Commands:\n'
awk -v WARNING="${WARNING}" -v SUCCESS="${SUCCESS}" -v PRIMARY="${PRIMARY}" \
'/^### /{printf"\n%s%s:%s\n",WARNING,substr($0,5),PRIMARY}
/^## /{if (annotation=="") annotation=substr($0,4)}
/^(function *)?[a-zA-Z0-9_]+ *\(\)/{
function_name=substr($0,1,index($0,"(")-1); # truncate string at opening round bracket
sub("^function ","",function_name); # remove leading "function " if present
gsub(" +","",function_name); # trim whitespaces
if (annotation!="" && substr($0,1,1) != "_") # ignore private functions
printf "%s %-'"$2"'s %s%s\n",SUCCESS,function_name,PRIMARY,annotation
}
!/^## /{annotation=""}' "$1"
printf '\n'
}
# List constants of the provided shoe script (used by "help" command)
_print_constants() {
# Synopsis: _print_constants <FILE_PATH> [PADDING]
# FILE_PATH: The path to the input file.
# PADDING: (optional) Padding length (default: 12)
# note: "awk: %*x formats are not supported"
if [ -z "$1" ]; then echo_danger 'error: _print_constants: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 2 ]; then echo_danger "error: _print_constants: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")" "${2:-12}"
if [ ! -f "$1" ]; then echo_danger "error: _print_constants: \"$1\" file not found\n"; return 1; fi
echo_warning 'Constants:\n'
awk -F '=' -v SUCCESS="${SUCCESS}" -v PRIMARY="${PRIMARY}" -v INFO="${INFO}" -v WARNING="${WARNING}" -v EOL="${EOL}" \
'/^[A-Z0-9_]+=.+$/ {
if (substr(PREV,1,3) == "## " && substr($0,1,1) != "_")
printf "%s %-'"$2"'s %s%s%s (value: %s%s%s)%s",SUCCESS,$1,PRIMARY,substr(PREV,4),INFO,WARNING,$2,INFO,EOL
} { PREV = $0 }' "$1"
printf '\n'
}
# Print provided text formatted as a description (used by "help" command)
_print_description() {
# Synopsis: _print_description <DESCRIPTION>
# DESCRIPTION: A string containing script description.
echo_warning 'Description:\n'
echo_primary "$(printf '%s' "$1" | fold -w 64 -s)\n\n" 2
}
# List flags of the provided shoe script (used by "help" command)
_print_flags() {
# Synopsis: _print_flags <FILE_PATH> [PADDING]
# FILE_PATH: The path to the input file.
# PADDING: (optional) Padding length (default: 12)
# note: "awk: %*x formats are not supported"
if [ -z "$1" ]; then echo_danger 'error: _print_flags: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 2 ]; then echo_danger "error: _print_flags: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")" $((${2:-12}-2))
if [ ! -f "$1" ]; then echo_danger "error: _print_flags: \"$1\" file not found\n"; return 1; fi
echo_warning 'Flags:\n'
awk -F '=' -v SUCCESS="${SUCCESS}" -v PRIMARY="${PRIMARY}" '/^[a-zA-Z0-9_]+=false$/ {
if (substr(PREV, 1, 3) == "## " && $1 != toupper($1) && substr($0, 1, 1) != "_")
printf "%s --%-'"$2"'s %s%s\n",SUCCESS,$1,PRIMARY,substr(PREV,4)
} { PREV = $0 }' "$1"
printf '\n'
}
# Print infos of the provided shoe script (used by "help" command)
_print_infos() {
# Synopsis: _print_infos <FILE_PATH>
# FILE_PATH: The path to the input file.
if [ -z "$1" ]; then echo_danger 'error: _print_infos: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 1 ]; then echo_danger "error: _print_infos: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")"
if [ ! -f "$1" ]; then echo_danger "error: _print_infos: \"$1\" file not found\n"; return 1; fi
__annotations__=$(_get_script_shoedoc "$1")
echo_warning 'Infos:\n'
echo_success 'author' 2 8; echo_primary "$(_get_shoedoc_tag "${__annotations__}" 'author')\n"
echo_success 'version' 2 8; echo_primary "$(_get_shoedoc_tag "${__annotations__}" 'version')\n"
echo_success 'link' 2 8; echo_primary "$(_get_shoedoc_tag "${__annotations__}" 'link')\n"
printf '\n'
}
# List options of the provided shoe script (used by "help" command)
_print_options() {
# Synopsis: _print_options <FILE_PATH> [PADDING]
# FILE_PATH: The path to the input file.
# PADDING: (optional) Padding length (default: 12)
# note: "awk: %*x formats are not supported"
if [ -z "$1" ]; then echo_danger 'error: _print_options: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 2 ]; then echo_danger "error: _print_options: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")" $((${2:-12}-2))
if [ ! -f "$1" ]; then echo_danger "error: _print_options: \"$1\" file not found\n"; return 1; fi
echo_warning "Options:\n"
awk -F '=' -v WARNING="${WARNING}" -v SUCCESS="${SUCCESS}" -v INFO="${INFO}" -v DEFAULT="${DEFAULT}" -v EOL="${EOL}" \
'/^[a-zA-Z0-9_]+=.+$/ {
if (substr(PREV,1,3) == "## " && $1 != toupper($1) && $2 != "false" && substr($0,1,1) != "_") {
if (match(PREV,/ \/.+\//)) {
# if option has constaint
CONSTRAINT=substr(PREV,RSTART,RLENGTH);
ANNOTATION=substr(PREV,4,length(PREV)-length(CONSTRAINT)-3);
printf "%s --%-'"$2"'s %s%s%s %s%s (default: %s%s%s)%s",SUCCESS,$1,DEFAULT,ANNOTATION,SUCCESS,CONSTRAINT,INFO,WARNING,$2,INFO,EOL
} else {
ANNOTATION=substr(PREV,4);
printf "%s --%-'"$2"'s %s%s%s (default: %s%s%s)%s",SUCCESS,$1,DEFAULT,ANNOTATION,INFO,WARNING,$2,INFO,EOL
}
}
} { PREV = $0 }' "$1"
printf '\n'
}
# Print usage of the provided shoe script (used by "help" command)
_print_usage() {
# Synopsis: _print_usage <FILE_PATH>
# FILE_PATH: The path to the input file.
if [ -z "$1" ]; then echo_danger 'error: _print_usage: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 1 ]; then echo_danger "error: _print_usage: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")"
if [ ! -f "$1" ]; then echo_danger "error: _print_usage: \"$1\" file not found\n"; return 1; fi
echo_warning 'Usage:\n'
echo_info "sh $(basename "$1") <" 2; echo_success 'command'; echo_info '> '
# options
awk -F '=' -v INFO="${INFO}" -v SUCCESS="${SUCCESS}" -v WARNING="${WARNING}" -v DEFAULT="${DEFAULT}" \
'/^[a-zA-Z0-9_]+=.+$/ {
if (substr(PREV,1,3) != "## " || $1 == toupper($1) || substr($1,1,1) == "_") next;
if ($2 == "false") {printf "%s[%s--%s%s]%s ",INFO,SUCCESS,$1,INFO,DEFAULT;next}
printf "%s[%s--%s %s%s%s]%s ",INFO,SUCCESS,$1,WARNING,$2,INFO,DEFAULT
} {PREV = $0} END {print "\n"}' "$1"
}
#--------------------------------------------------
#_ Self Install
#--------------------------------------------------
# Install script via copy
_copy_install() {
# Synopsis: _copy_install <FILE_PATH> [ALIAS]
# FILE_PATH: The path to the input file.
# ALIAS: (optional) The alias of the script to install. Defaults to the basename of the provided file
# note: Creates a symbolic link in the /usr/local/bin/ directory.
if [ -z "$1" ]; then echo_danger 'error: _copy_install: some mandatory parameter is missing\n'; return 1; fi
if [ ${#} -gt 2 ]; then echo_danger "error: _copy_install: too many arguments (${#})\n"; return 1; fi
set -- "$(realpath "$1")" "${2:-"$(basename "$1" .sh)"}"
if [ ! -f "$1" ]; then echo_danger "error: _copy_install: \"$1\" file not found\n"; return 1; fi
echo_info "sudo cp -a \"$1\" \"/usr/local/bin/$2\"\n"
sudo cp -a "$1" "/usr/local/bin/$2"
}
# Generates an autocomplete script for the provided file
_generate_autocomplete() {
# Synopsis: _generate_autocomplete <FILE_PATH> [ALIAS]