forked from icy/pacapt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.sh
executable file
·247 lines (194 loc) · 5.9 KB
/
compile.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
#!/usr/bin/env bash
# Purpose: `Compile` libraries from `lib/*` to create `pacapt` script.
# Author : Anh K. Huynh
# License: Fair license (http://www.opensource.org/licenses/fair)
# Source : http://github.com/icy/pacapt/
# Copyright (C) 2010 - 2015 Anh K. Huynh et al.
#
# Usage of the works is permitted provided that this instrument is
# retained with the works, so that any entity that uses the works is
# notified of this instrument.
#
# DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
set -u
set -e
unset GREP_OPTIONS
VERSION="${VERSION:-$(git log --pretty="%h" -1 2>/dev/null || true)}"
VERSION="${VERSION:-unknown}"
if [[ "${VERSION}" == "unknown" ]]; then
echo >&2 ":: Unable to get version information."
echo >&2 ":: You may to install or reconfigure your 'git' package."
exit 1
fi
: "${PACAPT_STATS=}" # List implemented operations to STDERR
: "${GREP:=grep}" # Need to update on SunOS
: "${AWK:=awk}" # Need to update on SunOS
# At compile time, `_sun_tools_init` is not yet defined.
if [[ -f "lib/sun_tools.sh" ]]; then
source "lib/sun_tools.sh" :
_sun_tools_init
fi
export GREP AWK VERSION PACAPT_STATS
########################################################################
# Print the shebang and header
########################################################################
cat <<EOF
#!/usr/bin/env bash
#
# Purpose: A wrapper for all Unix package managers
# License: Fair license (http://www.opensource.org/licenses/fair)
# Source : http://github.com/icy/pacapt/
# Version: $VERSION
# Authors: Anh K. Huynh et al.
# Copyright (C) 2010 - $(date +%Y) \\
$( \
< README.md \
sed -e '1,/AUTHORS/d' \
| $GREP '*' \
| sed -e 's,*,# |,g')
#
# Usage of the works is permitted provided that this instrument is
# retained with the works, so that any entity that uses the works is
# notified of this instrument.
#
# DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
#
export PACAPT_VERSION='$VERSION'
EOF
########################################################################
# Create help method
########################################################################
cat <<'EOS'
_help() {
cat <<'EOF'
EOS
# The body of help message
cat ./lib/help.txt
cat <<'EOS'
EOF
}
EOS
########################################################################
# Print the source of all library files, except (zz_main.sh)
########################################################################
for L in ./lib/*.sh; do
bash -n "$L" || exit 1
[[ "${L##*/}" != "zz_main.sh" ]] \
|| continue
$GREP -v '^#' "$L"
done
########################################################################
# Create the `_validate_operation` method.
# Detect all supported operations.
########################################################################
_operations=()
echo "_validate_operation() {"
echo " case \"\$1\" in"
for L in ./lib/*.sh; do
_PKGNAME="${L##*/}"
_PKGNAME="${_PKGNAME%.*}"
case "$_PKGNAME" in
"zz_main"|"00_core") continue ;;
esac
while read F; do
echo " \"$F\") ;;"
_operations+=( "$F" )
done < \
<(
$GREP -hE "^${_PKGNAME}_[^ \t]+\(\)" "$L" \
| $AWK -F '(' '{print $1}'
)
done
echo " *) return 1 ;;"
echo " esac"
echo "}"
########################################################################
# Print the source of `zz_main.sh`.
# `zz_main` doesn't contain only Bash function.
# It should be included in the last part of the script.
########################################################################
$GREP -v '^#' lib/zz_main.sh
########################################################################
# Stop here, or continue...
########################################################################
if [[ -z "${PACAPT_STATS:-}" ]]; then
echo >&2 "pacapt version '$VERSION' has been generated"
exit
fi
########################################################################
# For developers only
#
# PxO Q Qi Qs ...
# dpkg x o o ...
# yum o o o ...
#
########################################################################
_soperations="$(
echo "${_operations[@]}" \
| sed -e 's# #\n#g' \
| sed -e 's#^.*_\([A-Z][a-z]*\)#\1#g' \
| sort -u
)"
# Print the headers
_ret="$(printf "| %9s " "")"
for _sopt in $_soperations; do
_size="$(( ${#_sopt} + 1))"
_ret="$(printf "%s%${_size}s" "$_ret" "$_sopt")"
done
printf >&2 "%s\n" "$_ret"
i=0 # index
rs=0 # restart
_operations+=( "xxx_yyy" )
while :; do
_ret=""
[[ "$i" -lt "${#_operations[@]}" ]] \
|| break
_cur_pkg="${_operations[$i]}"
_cur_pkg="${_cur_pkg%_*}"
for _sopt in $_soperations; do
# Detect flag for this secondary option
_flag="."
# Start from the #rs index,
# go to boundary of the next package name.
# xx_Qi, xx_Qs,... yy_Qi, yy_Qs,...
#
i=$rs
while [[ "$i" -lt "${#_operations[@]}" ]]; do
_opt="${_operations[$i]}"
_cur2_opt="${_opt##*_}"
_cur2_pkg="${_opt%_*}"
# echo >&2 "(cur_pkg = $_cur_pkg, look up $_sopt [from $rs], found $_cur2_opt)"
# Reach the boundary of the next package name
if [[ "$_cur2_pkg" != "$_cur_pkg" ]]; then
break
else
if [[ "$_cur2_opt" == "$_sopt" ]]; then
_flag="y"
break
else
(( i ++ )) ||:
fi
fi
done
_size="$(( ${#_sopt} + 1))"
_ret="$(printf "%s%${_size}s" "$_ret" "$_flag")"
done
# Detect the next #restart index
i=$rs
while [[ "$i" -lt "${#_operations[@]}" ]]; do
_opt="${_operations[$i]}"
_cur2_pkg="${_opt%_*}"
if [[ "$_cur2_pkg" != "$_cur_pkg" ]]; then
rs=$i
break
fi
(( i ++ )) ||:
done
if [[ "$_cur_pkg" != "xxx" ]]; then
printf >&2 "| %9s %s\n" "$_cur_pkg" "$_ret"
fi
done
########################################################################
# Print statistics and the fancy table
########################################################################
echo >&2 "pacapt version '$VERSION' has been generated"