-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge2run.sh
executable file
·137 lines (108 loc) · 2.78 KB
/
merge2run.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
#!/bin/bash
#
# Copyright (c) 2017-2020 Roland Kujundzic <[email protected]>
#
#--
# Compute merge list.
#
# @export OUT MERGE_SH
# @param $@
# shellcheck disable=SC1090
#--
function merge_list {
OUT="run.sh"
MERGE_SH=
if test -s "sh/run/merge2$1"; then
OUT="$1"
shift
fi
if [[ "$#" -gt 0 && -s "$1" ]]; then
MERGE_SH="$*"
elif test -s "sh/run/merge2$OUT"; then
echo "load sh/run/merge2$OUT"
source "sh/run/merge2$OUT"
local a
for a in $MERGE2RUN; do
MERGE_SH="$MERGE_SH sh/run/$a.sh"
done
fi
test -z "$MERGE_SH" && _syntax "path/func1.sh path/func2.sh ... path/main.sh"
test -z "$MERGE2RUN_OUTPUT" || OUT="$MERGE2RUN_OUTPUT"
}
#--
# Compute include list.
#
# @export INCLUDE
# @global RKBASH_INC RKBASH_INC_NUM
# shellcheck disable=SC2086,SC2153
#--
function include_list {
echo "Scan for rkbashlib functions"
INCLUDE=
local a
for a in $MERGE_SH; do
echo -n "in $a ... "
_rkbash_inc "$a" #>/dev/null
echo "found $RKBASH_INC_NUM"
INCLUDE="$RKBASH_INC $INCLUDE"
done
INCLUDE=$(_sort $INCLUDE)
}
#--
# Join INCLUDE and MERGE_SH snipplets into OUT.
#
# @global RKBASH_SRC INCLUDE MERGE_SH OUT
# shellcheck disable=SC2034,SC2086
#--
function join_include {
local a src_inc scheck
echo -e "Include: $INCLUDE\nCreate $OUT"
for a in $INCLUDE; do
src_inc="$src_inc $RKBASH_SRC/${a:1}.sh"
done
scheck=$(grep -E '^# shellcheck disable=' $src_inc $MERGE_SH | \
sed -E 's/.+ disable=(.+)$/\1/g' | tr ',' ' ' | xargs -n1 | sort -u | xargs | tr ' ' ',')
test -z "$scheck" || RKS_HEADER_SCHECK="shellcheck disable=SC1091,$scheck"
_rks_header "$OUT"
for a in $src_inc; do
tail -n +2 "$a" | grep -E -v '^\s*#' >> "$OUT"
done
for a in $MERGE_SH; do
tail -n +2 "$a" >> "$OUT"
done
_add_abort_linenum "$OUT"
chmod 755 "$OUT"
}
#--
# M A I N
#--
APP=$0
# shellcheck disable=SC2034
CWD="$PWD"
APP_DESC="Merge shell code snipplets"
export APP_PID="$APP_PID $$"
echo -e "\n$APP_DESC"
# Load necessary src/* functions - don't put in function (declare -A will be lost)
if test -z "$RKBASH_SRC"; then
RKBASH_SRC="$(realpath "$APP" | xargs dirname)/src"
fi
load_func="abort add_abort_linenum chmod confirm cp find log md5
merge_sh mkdir msg mv parse_arg require_dir require_file require_owner
require_priv require_program rkbash_inc rks_header rm rsync sort sudo
syntax"
for a in $load_func; do
source "$RKBASH_SRC/$a.sh"
done
_parse_arg "$@"
if [[ -n "${ARG[scan]}" && -s "${ARG[scan]}" ]]; then
_rkbash_inc "${ARG[scan]}" #>/dev/null
echo "found $RKBASH_INC_NUM: $RKBASH_INC"
elif [[ -n "${ARG[self_update]}" && -f "${ARG[self_update]}" && -d "${ARG[self_update]}_" ]]; then
echo "_merge_sh '${ARG[self_update]}_' '${ARG[self_update]}'"
_merge_sh "${ARG[self_update]}_" "${ARG[self_update]}"
else
merge_list "$@"
include_list
join_include
fi
echo -e "done.\n"