This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebera
executable file
·1473 lines (1179 loc) · 43.3 KB
/
webera
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
# shellcheck disable=SC2016
#
# name : webera
# description : a handy static website generator
# repository : /~https://github.com/andamira/webera.sh
# author : José Luis Cruz © 2016-2017
# version : 0.2.0
# license : MIT
__WEBERA_VERSION=0.2.0
#-------------------------------------------------------------------------------
# ::main
#
# > $@ : all the arguments passed to the script
#
webera::main() {
## PRE-SETUP
webera::check-compatibility
local -ir __Time_stamp="$($__DATE '+%s%3N')" # [INC02]
webera::setup-debug
local Options_webera # the list of all webera options
local __options_pre __options_post # temporary options storage
# shellcheck disable=SC2034
__options_pre=$(set -o posix ; set | $__AWK -F= '{print $1"="}' )
## CONFIGURABLE GLOBAL OPTIONS
# 1. Variables prefixed with `Opt_` are user configurable options.
# 2. Options starting with an underscore can't be configured from
# the config file or string; only using script flag arguments.
# 3. Options without an associated argument can only be modified
# by the user from the configuration.
# 4. Options have to be referenced from the configuration removing
# the `Opt_` prefix. Note that case sensitivity is not enforced.
# statuses
local _Opt_operational=false # -trawn
local _Opt_do_process_templates=false # -ta
local _Opt_do_process_templates=false # -ta
local _Opt_do_process_resources=false # -ra
local _Opt_do_preview_in_browser=false # -w
local _Opt_do_generate_config_file=false # -n
# paths
local Opt_dir_templates=tem # -T
local Opt_dir_resources=res # -R
local Opt_dir_output=out # -O
local Opt_dir_build=build # -B
local _Opt_config_file=.weberarc # -F
local _Opt_config_string="" # -S
# delete output & build directories
local _Opt_do_delete_dir_output=true # -d
local Opt_do_delete_dir_build=true
# log
local Opt_log_file=log.txt # -G
local _Opt_do_clear_log=false # -c
local -i Opt_log_level=0 # -L [0-3]
# web browser
local Opt_web_browser="firefox" # -W
local Opt_server_type="python" # -S
local Opt_server_host="localhost" # -H
local -i Opt_server_port=8192 # -P
local Opt_server_start=""
local Opt_server_stop=""
# misc
local _Opt_do_display_usage=false # -h
local _Opt_do_print_vars=false # -_
local -i Opt_nesting_max=8
## POST-SETUP
# shellcheck disable=SC2034
__options_post="$(set -o posix ; set | $__SED '/^'\''/d' )"
# save the list of the default options
webera::setup-options __options_pre __options_post Options_webera
## NON-CONFIGURABLE GLOBAL DATA
# arrays
local -A Options_args__ # the options received as script arguments
local -A Defined_commands__ # the custom commands defined in configuration
local -i Nesting_level=0 # current nesting level, when including files
local Config_whole # all the configuration is saved here after being read
local Log_presaved # log messages are saved here before logfile is ready
## STRING CONSTANTS
local -r __Date_start="$($__DATE '+%Y-%m-%d %H:%M:%S' \
-d @"${__Time_stamp:0:10}")" # [INC03]
# whitespace indentation and headline
local -r __In=" " __In2=" " __In3=" "
local -r __Hl='------------------------------------------------------'
# reusable regexp patterns
local -r __Ws="[[:space:]]" # whitespace
local -r __Sed_trim_ws="s/^$__Ws*//;s/$__Ws*$//"
local -r __Sed_del_comments="/^$__Ws*#.*/d" # starts with #
local -r __Sed_del_empty_lines="/^$__Ws*$/d"
local -r __Sed_join_split_lines=':x; /\\$/ {N; s/\\\n//; tx}' # ends with \
## MAIN LOGIC
webera::log "\n===============[$__Date_start]==============@\n"
webera::parse-arguments "$@"
# read the configuration files & string
[[ $_Opt_config_file != "/etc/weberarc" ]] && webera::read-config file "/etc/weberarc"
[[ $_Opt_config_file != "$HOME/.weberarc" ]] && webera::read-config file "$HOME/.weberarc"
webera::read-config file "$_Opt_config_file"
webera::read-config string '_Opt_config_string'
webera::parse-config-options
if [[ $_Opt_do_print_vars == true ]]; then
webera::print-options; exit 0
elif [[ $_Opt_do_display_usage == true || "$_Opt_operational" == false ]]; then
webera::display-info usage
fi
webera::setup-log
[[ $_Opt_operational == false ]] && exit 1
webera::delete-directories
webera::read-config-commands
[[ $_Opt_do_process_resources == true ]] && webera::process-resources
[[ $_Opt_do_process_templates == true ]] && webera::process-templates
webera::log "Total elapsed time: $(webera::elapsed-time true)"
[[ $_Opt_do_preview_in_browser == true ]] && webera::preview-website
exit 0
} # ::main
#-------------------------------------------------------------------------------
# ::display-info
#
# Displays usage or version information.
#
# > $1 [usage|version]
# ^ ::parse-arguments ::parse-config-options
# < human readable usage info or version info
#
webera::display-info() {
case "$1" in
usage)
cat -- <<-ENDUSAGE
Usage: $(basename "$0") -[trawn] [other options]
> a handy static website generator <
INSTRUCTIONS
1. At least one OPERATIVE FLAG is needed in order for the script to run.
2. Mandatory arguments to long options, are mandatory for short options too.
OPERATIVE FLAGS ...
-t, --process-templates process the templates
-r, --process-resources process the resources
-a, --process-all process both the templates and the resources
-w --preview preview website in the web browser
-n --new-config generate a new configuration file
OPTION FLAGS ... (default value)
-F, --file-config=FILE indicate the configuration file ($_Opt_config_file)
-C, --config=STRING pass the configuration as a string ($_Opt_config_string)
-T, --dir-templates=DIR indicate the templates directory ($Opt_dir_templates)
-R, --dir-resources=DIR indicate the resources directory ($Opt_dir_resources)
-O, --dir-output=DIR indicate the output directory ($Opt_dir_output)
-B, --dir-build=DIR indicate the build directory ($Opt_dir_build)
-d, --dont-delete-output don't delete the output dir ($_Opt_do_delete_dir_output)
-L, --log-level=LEVEL log level [0=none|1|2|3] ($Opt_log_level)
-c, --clear-log clear the previous log file ($_Opt_do_clear_log)
-G, --logfile=FILE log file ($Opt_log_file)
-W, --browser-bin=BIN web browser binary ($Opt_web_browser)
-S, --server-type=TYPE server type [none|php|python|custom] ($Opt_server_type)
-H, --server-host=HOST server host ($Opt_server_host)
-P, --server-port=PORT port number ($Opt_server_port)
-h, --help display this help and exit
--version show version info and exit
ENDUSAGE
;;
version)
cat -- <<-ENDVERSION
webera v$__WEBERA_VERSION
</~https://github.com/andamira/webera.sh>
Copyright (C) 2016-2017 José Luis Cruz
Released under the MIT license.
Dependencies found:
$__SED, $__AWK, $__GREP, $__DATE
ENDVERSION
;;
esac
} # ::display-info
#-------------------------------------------------------------------------------
# ::parse-arguments
#
# Parses the arguments received by the script and sets/saves the options.
#
# > $@ : all the arguments passed to the script
# ^ ::main
#
webera::parse-arguments() {
local OPTIND option value
local optspec=':trawnF:C:T:R:O:B:dcL:G:W:S:H:P:_h-:'
while getopts "$optspec" option; do
case "$option" in
-) # long option flags: http://stackoverflow.com/a/7680682/940200
case "$OPTARG" in
process-templates) webera::set-arg process-templates ;;
process-resources) webera::set-arg process-resources ;;
process-all) webera::set-arg process-all ;;
preview) webera::set-arg preview ;;
new-config) webera::set-arg new-config ;;
file-config=*)
webera::set-arg file-config "${OPTARG#*=}" ;;
config=*)
webera::set-arg config "${OPTARG#*=}" ;;
dir-templates=*)
webera::set-arg dir-templates "${OPTARG#*=}" ;;
dir-resources=*)
webera::set-arg dir-resources "${OPTARG#*=}" ;;
dir-output=*)
webera::set-arg dir-output "${OPTARG#*=}" ;;
dir-build=*)
webera::set-arg dir-build "${OPTARG#*=}" ;;
dont-delete-output) webera::set-arg dont-delete-output ;;
clear-log) webera::set-arg clear-log ;;
log-level=*)
webera::set-arg log-level "${OPTARG#*=}" ;;
log-file=*)
webera::set-arg log-file "${OPTARG#*=}" ;;
browser-bin=*)
webera::set-arg browser-bin "${OPTARG#*=}" ;;
server-type=*)
webera::set-arg server-type "${OPTARG#*=}" ;;
server-host=*)
webera::set-arg server-host "${OPTARG#*=}" ;;
server-port=*)
webera::set-arg server-port "${OPTARG#*=}" ;;
help) webera::set-arg help ;;
version) webera::set-arg version ;;
*) [[ $OPTERR == 1 ]] && webera::set-arg "--$OPTARG" ;;
esac ;;
# short option flags
t) webera::set-arg process-templates ;;
r) webera::set-arg process-resources ;;
a) webera::set-arg process-all ;;
w) webera::set-arg preview ;;
n) webera::set-arg new-config ;;
F) webera::set-arg file-config "$OPTARG" ;;
C) webera::set-arg config "$OPTARG" ;;
T) webera::set-arg dir-templates "$OPTARG" ;;
R) webera::set-arg dir-resources "$OPTARG" ;;
O) webera::set-arg dir-output "$OPTARG" ;;
B) webera::set-arg dir-build "$OPTARG" ;;
d) webera::set-arg dont-delete-output ;;
c) webera::set-arg clear-log ;;
L) webera::set-arg log-level "$OPTARG" ;;
G) webera::set-arg log-file "$OPTARG" ;;
W) webera::set-arg browser-bin "$OPTARG" ;;
S) webera::set-arg server-type "$OPTARG" ;;
H) webera::set-arg server-host "$OPTARG" ;;
P) webera::set-arg server-port "$OPTARG" ;;
h) webera::set-arg help ;;
_) webera::set-arg _ ;; # hidden print-options argument
*) [[ $OPTERR != 1 || ${optspec:0:1} == ":" ]] &&
webera::set-arg "-$OPTARG" ;;
esac
done
shift $((OPTIND-1))
} # ::parse-arguments
#-------------------------------------------------------------------------------
# ::set-arg
#
# Sets both short and long arguments, in a centralize manner.
#
# > $1 : the common long name of the argument
# > $2 : (optional) the value of the argument
# x 255 : unknown argument
# ^ ::parse-arguments
#
webera::set-arg() {
local argument="$1" value="$2"
case "$argument" in
# OPERATIONAL flags
process-templates)
_Opt_operational=true
webera::push-arg _Opt_do_process_templates true ;;
process-resources)
_Opt_operational=true
webera::push-arg _Opt_do_process_resources true ;;
process-all)
_Opt_operational=true
webera::push-arg _Opt_do_process_resources true
webera::push-arg _Opt_do_process_templates true ;;
preview)
_Opt_operational=true
webera::push-arg _Opt_do_preview_in_browser true ;;
new-config)
_Opt_operational=true
_Opt_do_generate_config_file=true
webera::push-arg _Opt_do_generate_config_file true ;;
## OPTION FLAGS
# Options whose name starts with an underscore `_` can only be set
# via script arguments; they are not read from any configuration.
file-config)
_Opt_config_file="$value"
webera::push-arg _Opt_config_file "$value" ;;
config)
_Opt_config_string="$value"
webera::push-arg _Opt_config_string "$value" ;;
clear-log)
webera::push-arg _Opt_do_clear_log true ;;
dont-delete-output)
webera::push-arg _Opt_do_delete_dir_output false ;;
# The following options can also be defined in configuration file,
# but passing them as arguments to the script has a higher priority.
dir-templates)
webera::push-arg Opt_dir_templates "$value" ;;
dir-resources)
webera::push-arg Opt_dir_resources "$value" ;;
dir-output)
webera::push-arg Opt_dir_output "$value" ;;
dir-build)
webera::push-arg Opt_dir_build "$value" ;;
log-level)
webera::push-arg Opt_log_level "$value" ;;
log-file)
webera::push-arg Opt_log_file "$value" ;;
browser-bin)
webera::push-arg Opt_web_browser "$value" ;;
server-type)
webera::push-arg Opt_server_type "$value" ;;
server-host)
webera::push-arg Opt_server_host "$value" ;;
server-port)
webera::push-arg Opt_server_port "$value" ;;
help)
_Opt_do_display_usage=true ;;
version)
webera::display-info version; exit 0 ;;
_)
_Opt_do_print_vars=true ;;
*)
webera::log error "ERROR: unknow argument '$argument'"
exit 255 ;; # TODO send to log
esac
} # ::set-arg
# ::push-arg
#
# Pushes a key to the array of options received as arguments.
#
# > $1 option key
# > $2 value received
# ^ ::set-arg
#
webera::push-arg() { Options_args__[$1]="$2"; }
#-------------------------------------------------------------------------------
# ::setup-log
#
# Sets up the log file and then writes all the pre-saved logs.
#
# ^ ::main
#
webera::setup-log() {
local parsed_prelog
if [[ $_Opt_do_clear_log == true ]]; then
webera::log 3 warn "\nWarning: deleting previous log file '$Opt_log_file'"
rm -- "$Opt_log_file" 2>/dev/null;
fi
# Restore the pre-saved log messages
#
# 1. filter out the lines with greater than current log level
# 2. leave only the log message, discard the rest. TODO: restore warn|error
# 3. bring back the newlines previously converted to BEL.
# 4. replace the last `@` char, in the date header line. with the log level.
#
# shellcheck disable=SC2059
parsed_prelog=$(printf "$Log_presaved" \
| $__AWK -v log_level=$Opt_log_level -F '|' '$1<=log_level' \
| cut -d'|' -f3- \
| tr '\a' '\n' \
| $__SED "s/====@$/====$Opt_log_level/" \
)
Log_presaved=false
[[ -n $parsed_prelog ]] && webera::log "$parsed_prelog" # print what's left
} # ::setup-log
#-------------------------------------------------------------------------------
# ::log
#
# Prints a message to the log file. Errors and warnings also to STDERR.
#
# > $1 : (optional) the minimum level
# > $2 : (optional) log_type [warn|error]
# > $3+ : log message
# ^ {many functions}
#
# shellcheck disable=SC2059
webera::log() {
local log_level_needed="$1" # First (optional) parameter
local -ir log_level_min=1 log_level_max=3 # log level boundaries
local -i log_level
# check it is an integer number
if [[ $log_level_needed =~ ^[0-9]+$ ]]; then
if [[ $log_level_needed -lt $log_level_min ]]; then
log_level="$log_level_min"
elif [[ $log_level_needed -gt $log_level_max ]]; then
log_level="$log_level_max"
else
log_level="$log_level_needed"
fi
shift # prepare next positional parameter
else # if it's not a number, use the default value
log_level="$log_level_min"
fi
local log_type="$1" # Second (optional) parameter
# check if it is the log type
case "$log_type" in
error|warn)
shift # prepare next positional parameter
;;
*) log_type="" ;;
esac
local __ifs="$IFS"; IFS=$''
local log_msg="$*" # All that remains is the log message
IFS="$__ifs"
if [[ $Log_presaved == false ]]; then
if [[ $log_level -le $Opt_log_level ]]; then
printf "$log_msg\n" >> "$Opt_log_file"
fi
else
# Pre-save this log message as a single line
#
# 1. replace the newlines with a BEL
# 2. put the log level and type at the beginning
local log_msg_1line; log_msg_1line=$(printf "$log_msg" | tr '\n' '\a') || true
Log_presaved+="\n$log_level|$log_type|$log_msg_1line"
fi
# Print errors and warnings to stderr [OPTIMIZE]
if [[ $log_type == "error" || $log_type == "warn" ]]; then
# 1. trim whitespace
# 2. delete newlines
# 3. print to STDERR
printf "$log_msg" \
| $__SED -e "$__Sed_trim_ws" \
| tr -d '\n' \
| $__AWK '{ print }' - >&2
fi
} # ::log
#-------------------------------------------------------------------------------
# ::preview-website
#
# Prepares the server commands, for previewing in the browser.
#
# x 1 : Server type not recognized
# ^ ::main (when the script received the `-r` flag)
#
webera::preview-website() {
case "$Opt_server_type" in
python)
Opt_server_start='pushd "$Opt_dir_output"; '
Opt_server_start+='python -m SimpleHTTPServer $Opt_server_port'
Opt_server_stop='kill $(pgrep -f "python -m SimpleHTTPServer")'
;;
php)
Opt_server_start='php -S "$Opt_server_host:$Opt_server_port"'
Opt_server_start+=' -t "$Opt_dir_output"'
Opt_server_stop='kill $(pgrep -f "php -S $Opt_server_host")'
;;
none) # useful when not using `dir/index.html` structure
Opt_server_start=""
Opt_server_stop=""
;;
custom) # for when a server is already running, or custom Opt_server_start
;;
*)
webera::log 2 error "ERROR: Not recognized "
"Opt_server_type='$Opt_server_type'"
exit 1
;;
esac
printf 'Loading website in "%s"...\n' "$Opt_web_browser"
local cmd_browser
if [[ $Opt_server_type == "none" ]]; then
cmd_browser="$Opt_web_browser file://$($__READLINK -f $Opt_dir_output) &"
else
cmd_browser="$Opt_web_browser http://$Opt_server_host:$Opt_server_port &"
fi
sleep 1s && eval "$cmd_browser" >&2 2>/dev/null
if [[ -n $Opt_server_start ]]; then
printf '(Use CTRL+C to stop the "%s" web server)\n' \
"$Opt_server_type"
printf 'Opt_server_start=%s' "$Opt_server_start"
eval "$Opt_server_stop >&2 2>/dev/null"; eval "$Opt_server_start"
fi
} # ::preview-website
#-------------------------------------------------------------------------------
# ::delete-directories
#
# Deletes Opt_dir_build, and Opt_dir_output if there is pending processing.
#
# ^ ::main
#
webera::delete-directories() {
[[ $Opt_do_delete_dir_build == true ]] &&
rm -rf -- "$Opt_dir_build" 2>/dev/null
if [[ $_Opt_do_delete_dir_output == true ]] &&
[[ $_Opt_do_process_resources == true \
|| $_Opt_do_process_templates == true ]]; then
rm -rf -- "$Opt_dir_output" 2>/dev/null
fi
} # ::delete-directories
#-------------------------------------------------------------------------------
# ::read-config
#
# Reads the configuration from a file or a string,
# and joins it all together into $Config_whole.
#
# > $1 : [file|string] the origin of the configuration.
# > $2 : $config_pointer is the file name when the origin is a file,
# or the variable name when the origin is a string.
# x 1 : unknown config type
# ^ ::main
#
webera::read-config() {
local config_orig="$1" config_pointer="$2"
local read_config # the configuration read from $config_pointer
if [[ $config_orig == "string" ]]; then
if [[ -n ${!config_pointer} ]]; then # string's not empty
webera::log 3 "Reading settings passed as an argument."
read_config="$(printf '%s' "${!config_pointer}" \
| $__SED -e "$__Sed_trim_ws" \
| $__SED -e "$__Sed_del_comments" -e "$__Sed_del_empty_lines" \
| $__SED -e "$__Sed_join_split_lines" \
)"$'\n'
Config_whole+="$read_config"
webera::store-options-from-config strn "" "$read_config"
fi
elif [[ $config_orig == "file" ]]; then
if [[ -f $config_pointer ]]; then # the file exists
if [[ $_Opt_do_generate_config_file == true ]] ; then
webera::log 2 warn "Warning: automatic override of an existing " \
"config file ('$config_pointer') is not permitted."
return
fi
webera::log 3 "Reading settings from file '$config_pointer'"
read_config="$(sed -e "$__Sed_trim_ws" "$config_pointer" \
| $__SED -e "$__Sed_del_comments" -e "$__Sed_del_empty_lines" \
| $__SED -e "$__Sed_join_split_lines" \
)"$'\n'
Config_whole+="$read_config"
webera::store-options-from-config file "$config_pointer" "$read_config"
else # config file doesn't exist
# it's the config file of the project
if [[ $config_pointer == "$_Opt_config_file" ]]; then
# stop if printing debug info
[[ $_Opt_do_print_vars == true ]] && return
if [[ $_Opt_do_generate_config_file == true ]]; then
webera::generate-config-file
else
webera::log 3 warn "Warning: project's config file " \
"'$_Opt_config_file' doesn't exist."
fi
fi
fi
else
webera::log error "Error: unknown config type"
exit 1
fi
} # ::read-config
#-------------------------------------------------------------------------------
# ::parse-config-options
#
# Parses the configuration options and overrides the default options with:
# 1 - the joint configuration and 2 - the passed arguments, in that order.
#
# ^ ::main
#
webera::parse-config-options() {
local settings_list
local setting
local option_name
local setting_value
local setting_previous
# Create a list of all the 'config' operations
settings_list="$(printf '%s' "$Config_whole" \
| $__GREP "^$__Ws*config$__Ws*:" )"
if [[ -n $settings_list ]]; then
webera::log "\nConfiguring settings...\n$__Hl"
local __ifs="$IFS"; IFS=$'\n'
for setting in $settings_list; do
# TODO: sanitize received data
option_name=$(printf '%s' "$setting" \
| cut -d':' -f2 \
| $__SED -e "$__Sed_trim_ws")
setting_value=$(printf '%s' "$setting" \
| cut -d':' -f3- \
| $__SED -e "$__Sed_trim_ws")
[[ -n ${!option_name} ]] &&
setting_previous="(previous=${!option_name})"
webera::log "${__In}setting: $option_name=$setting_value " \
"$setting_previous"
# Read (only public) options from file
if [[ $setting != _* ]]; then
# Override the global option with the parsed config option
printf -v "Opt_${option_name,,}" '%s' "$setting_value"
fi
done
IFS="$__ifs"
fi
local opt_list # temporary storage for the argument options
local opt; for opt in "${!Options_args__[@]}"; do
# Override the global option with the parsed argument
printf -v "$opt" '%s' "${Options_args__[$opt]}"
# Format the line appropriately
opt_list+="$opt=${Options_args__[$opt]}"$'\n'
done
# Mass-sanitize and append the parsed arguments to the global options list
Options_webera+=$'\n'"$(webera::sanitize-options "$opt_list" args)"
} # ::parse-config-options
#-------------------------------------------------------------------------------
# ::generate-config-file
#
# Generates a config file, writing down the options modified via arguments.
#
# ^ ::main (when the script received the `-n` flag)
#
webera::generate-config-file() {
local option; for option in "${!Options_args__[@]}"; do
if [[ $option != _* ]]; then # Only write public options
local opt=${option#Opt_} # remove `Opt_` prefix
# Save current option to the new config file (UPPERCASE)
printf '%s\n' \
"config : ${opt^^} : ${Options_args__[$option]}" \
>> "$_Opt_config_file"
fi
done
touch "$_Opt_config_file" # make sure (even an empty) file is created
} # ::generate-config-file
#-------------------------------------------------------------------------------
# ::read-config-commands
#
# Reads the configuration commands for processing templates and resources.
#
# ^ ::main
#
webera::read-config-commands() {
local commands_list
local commands_num
local cmd
local command_name
local command_action
commands_list="$(printf '%s' "$Config_whole" | $__GREP "^$__Ws*command$__Ws*:" )"
if [[ -n $commands_list ]]; then
commands_num=$(printf '%s\n' "$commands_list" | wc -l )
webera::log "\nParsing commands...\n$__Hl"
webera::log "${__In}Found $commands_num defined command:"
else return; fi
local __ifs="$IFS"; IFS=$'\n'
for cmd in $commands_list; do # [OPTIMIZE]
command_name=$(printf '%s' "$cmd" \
| cut -d':' -f2 \
| $__SED -e "$__Sed_trim_ws" )
command_action=$(printf '%s' "$cmd" \
| cut -d':' -f3- \
| $__SED -e "$__Sed_trim_ws" )
Defined_commands__["$command_name"]="$command_action"
webera::log 2 "${__In}$command_name='$command_action'"
done
IFS="$__ifs"
} # ::read-config-commands
#-------------------------------------------------------------------------------
# ::process-resources
#
# Parses the configuration and executes the resource operations.
#
# r 1 : no resources directory found
# ^ ::main (when the script received the `-r` flag)
#
webera::process-resources() {
local oper_list_res # list of operations over resources
local oper_num # the number of operations in $oper_list_res
local oper # single operation from $oper_list_res
local oper_type # the type of operation $oper is
local oper_orig # the operation origin, tipically the origin file
local oper_targ # the operation target, tipically the target file
local oper_cmd # the command if $oper_type is a custom operation
webera::log "\nProcessing resources...\n$__Hl"
if [[ ! -d $Opt_dir_resources ]]; then
webera::log error "No resources dir '$Opt_dir_resources' found."
return 1
fi
oper_list_res="$(printf '%s' "$Config_whole" \
| $__GREP "^$__Ws*resource$__Ws*:" )"
oper_num=$(printf '%s\n' "$oper_list_res" \
| $__SED "$__Sed_del_empty_lines" \
| wc -l )
webera::log "${__In}Found $oper_num operations on resources:"
local __ifs="$IFS"; IFS=$'\n'
for oper in $oper_list_res; do # [OPTIMIZE]
oper_type=$(printf '%s' "$oper" \
| cut -d':' -f2 \
| $__SED -e "$__Sed_trim_ws" )
oper_orig=$(printf '%s' "$oper" \
| cut -d':' -f3 \
| $__SED -e "$__Sed_trim_ws" )
oper_targ=$(printf '%s' "$oper" \
| cut -d':' -f4 \
| $__SED -e "$__Sed_trim_ws" )
webera::log "${__In}$oper_type: $oper_orig > $oper_targ"
if [[ ! -e "$Opt_dir_resources/$oper_orig" ]]; then
webera::log error "${__In}ERROR: '$oper_orig' don't exist"
continue # XXX do break instead?
fi
case "$oper_type" in
"copy")
mkdir -p -- "$Opt_dir_output/$Opt_dir_resources/$(dirname "$oper_targ")"
cp -r -- "$Opt_dir_resources/$oper_orig" \
"$Opt_dir_output/$Opt_dir_resources/$oper_targ"
;;
*)
# On custom cmds, {PATHS} tags are adapted for resources
if [[ -n ${Defined_commands__[$oper_type]} ]]; then
oper_cmd=$(printf '%s' "${Defined_commands__[$oper_type]}" \
| $__SED "s|{ORIGIN}|$Opt_dir_resources/$oper_orig|g" \
| $__SED "s|{BUILD}|$Opt_dir_build/$oper_orig|g" \
| $__SED "s|{TARGET}|$Opt_dir_output/$Opt_dir_resources/$oper_targ|g")
# Create target paths
if [[ ${Defined_commands__[$oper_type]} == *"{BUILD}"* ]]; then
mkdir -p -- "$(dirname "$Opt_dir_build/$oper_targ")"
fi
if [[ ${Defined_commands__[$oper_type]} == *"{TARGET}"* ]]; then
mkdir -p -- "$(dirname \
"$Opt_dir_output/$Opt_dir_resources/$oper_targ")"
fi
webera::log 3 "${__In}Executing: $oper_cmd"
eval "$oper_cmd"
else
webera::log error \
"${__In}ERROR: operation '$oper_type' not recognized"
fi
;;
esac
done
IFS="$__ifs"
} # ::process-resources
#-------------------------------------------------------------------------------
# ::process-templates
#
# Parses the configuration and executes the template operations.
#
# r 1 : no templates directory found
# ^ ::main (when the script received the `-t` flag)
#
webera::process-templates() {
local oper_list_tem # list of operations over templates
local oper_num # the number of operations in $oper_list_tem
local oper # single operation from $oper_list_tem
local oper_type # the type of operation $oper is
local oper_orig # the operation origin, tipically the origin file
local oper_targ # the operation target, tipically the target file
local oper_cmd # the command if $oper_type is a custom operation
webera::log "\nProcessing templates...\n$__Hl"
if [[ ! -d $Opt_dir_templates ]]; then
webera::log error "No templates dir '$Opt_dir_templates' found."
return 1
fi
oper_list_tem="$(printf '%s' "$Config_whole" \
| $__GREP "^$__Ws*template$__Ws*:" )"
oper_num=$(printf '%s\n' "$oper_list_tem" \
| $__SED "$__Sed_del_empty_lines" \
| wc -l )
webera::log "${__In}Found $oper_num operations on templates:"
local __ifs="$IFS"; IFS=$'\n'
for oper in $oper_list_tem; do
oper_type=$(printf '%s' "$oper" \
| cut -d':' -f2 \
| $__SED -e "$__Sed_trim_ws" )
oper_orig=$(printf '%s' "$oper" \
| cut -d':' -f3 \
| $__SED -e "$__Sed_trim_ws" )
oper_goal=$(printf '%s' "$oper" \
| cut -d':' -f4 \
| $__SED -e "$__Sed_trim_ws" )
webera::log "${__In}$oper_type: $oper_orig > $oper_goal"
case "$oper_type" in
"route")
if [[ -n $oper_orig && -n $oper_goal ]]; then
# A path ending in `/`, will be a dir_name/index.html
if [[ $oper_goal == */ ]]; then
mkdir -p -- "${Opt_dir_output}/${oper_goal}"
webera::render-template "$oper_orig" > \
"${Opt_dir_output}/${oper_goal}/index.html"
else
mkdir -p -- "$(dirname "${Opt_dir_output}/${oper_goal}")"
webera::render-template "$oper_orig" > \
"${Opt_dir_output}/${oper_goal}"
fi
else
webera::log error "${__In}ERROR: missing arguments for" \
"'template:route' operation"
fi
;;
*)
# On custom cmds, the {PATHS} tags are adapted for templates
if [[ -n ${Defined_commands__[$oper_type]} ]]; then
oper_cmd=$(printf '%s' "${Defined_commands__[$oper_type]}" \
| $__SED "s|{ORIGIN}|$Opt_dir_templates/$oper_orig|g" \
| $__SED "s|{BUILD}|$Opt_dir_build/$oper_orig|g" \
| $__SED "s|{TARGET}|$Opt_dir_output/$oper_goal|g" )
# Create target paths
[[ ${Defined_commands__[$oper_type]} == *"{BUILD}"* ]] &&
mkdir -p -- "$(dirname "$Opt_dir_build/$oper_goal")"
[[ ${Defined_commands__[$oper_type]} == *"{TARGET}"* ]] &&
mkdir -p -- \
"$(dirname "$Opt_dir_output/$Opt_dir_resources/$oper_goal")"
webera::log 3 "${__In}Executing: $oper_cmd"
eval "$oper_cmd"
else
webera::log error \
"${__In}ERROR: operation '$oper_type' not recognized"
fi
;;
esac
done
IFS="$__ifs"
} # ::process-templates