-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.bash.inc
293 lines (247 loc) · 6.02 KB
/
install.bash.inc
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
# vi:syntax=sh
#
# install-cli
#
# See install.example for usage.
#
#
# start installation lib
#
# extended glob syntax needed by require()
shopt -s extglob
#
# messaging
icli_BASECONTEXT="install"
icli_CONTEXT=$icli_BASECONTEXT
icli::set_context() {
icli_CONTEXT=$1
}
icli::unset_context() {
icli_CONTEXT=$icli_BASECONTEXT
}
readonly T_0=$(tput sgr0)
readonly T_DIM=$(tput dim)
readonly T_UNDER=$(tput smul)
readonly T_NO_UNDER=$(tput rmul)
readonly T_FCYAN=$(tput setaf 6)
readonly T_FGREEN=$(tput setaf 2)
readonly T_FMAGENTA=$(tput setaf 5)
readonly T_FRED=$(tput setaf 1)
readonly T_FYELLOW=$(tput setaf 3)
icli::message() {
echo -e "$T_DIM($icli_CONTEXT)$T_0" $@ $T_0
}
icli::context_message() {
icli::set_context "$1"
icli::message "$2"
icli::unset_context
}
#
# misc helpers
icli::defn_terse() {
# NOTE: We were doing the following, in order to trim the function
# declaration's enclosing brackets:
#
# declare -f $1 | tail -n +3 | head -n -1 | ...
#
# However, not all implementations of `head` support negative line
# numbers.
#
# And, rather than any of the crazy sed/awk/etc. that would be
# required to reproduce this functionality, we'll just leave the
# enclosing brackets now!
#
# declare -f $1 | tail -n +3 | head -n -1 | sed 's/^[[:blank:]]*//' | paste -s -d ' '
# NOTE: POSIX paste requires specification of file ("-" for stdin)
declare -f $1 | tail -n +2 | sed 's/^[[:blank:]]*//' | sed 's/[[:blank:]]*$//'| paste -s -d ' ' -
}
icli::join_by() {
local IFS="$1"
shift
echo "$*"
}
icli::vercomp() {
# compare two version specs
#
# returns:
#
# 0: identical
# 1: first is greater
# 2: second is greater
#
if [[ $1 == $2 ]]; then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++)); do
if [[ -z ${ver2[i]} ]]; then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
return 2
fi
done
return 0
}
#
# require()
readonly icli_REQUIRE_ARGS=(name checker installer fail-prefix fail-message fail-with)
readonly icli_REQUIRE_OPTIONS=(${icli_REQUIRE_ARGS[@]:2})
readonly icli_OPTION_PATTERN="@($(icli::join_by "|" ${icli_REQUIRE_OPTIONS[@]}))"
icli_STARTED=false
require() {
# if require() is ever called,
# print the "begin" message;
# but, only once:
$icli_STARTED || {
icli_STARTED=true
icli::message "${T_FCYAN}begin"
}
echo
# parse positional arguments and options
# (Bash doesn't support --long-options, and Darwin doesn't supply
# GNU getopt by default)
local -i index=0
local pair key value
while [ "${1+defn}" ]
do
case "$1" in
--$icli_OPTION_PATTERN=*)
pair=${1:2} # strip dashes
key=${pair%=*} # split key and value
value=${pair#*=}
;;
-*)
icli::message "${T_FRED}error: require has no such option '$1'"
return 1
;;
*)
[ $index -eq ${#icli_REQUIRE_ARGS[@]} ] && {
icli::message "${T_FRED}error: require received too many arguments"
return 1
}
key=${icli_REQUIRE_ARGS[$index]}
value="$1"
((index++))
;;
esac
declare -r ${key//-/_}="$value" 2> /dev/null || {
icli::message "${T_FRED}error: argument '$key' specified multiple times"
return 1
}
shift
done
if [ -z "$name" ] || [ -z "$checker" ]
then
icli::message "${T_FRED}error: require received too few arguments"
return 1
fi
local result
# check (and install)
icli::set_context $name
if $checker
then
[ -z "$installer" ] && local success_word="check passed" || local success_word="installed"
icli::message "${T_FGREEN}${success_word} ✓"
result=0
elif [ -z "$installer" ]
then
if [ -n "$fail_message" ]; then
icli::message "$T_FRED$fail_message"
else
icli::message ${fail_prefix:+$T_FRED$fail_prefix –} "${T_FRED}check failed ✗"
fi
result=1
else
if [ -n "$fail_message" ]; then
icli::message "$T_FYELLOW$fail_message"
else
icli::message ${fail_prefix:+$T_FYELLOW$fail_prefix –} "${T_FYELLOW}install?"
fi
echo -n $T_DIM # hack the select menu
select _yn in "${T_0}${T_FYELLOW}${T_UNDER}yes, install$T_0 $T_DIM$(icli::defn_terse $installer)" \
"${T_0}${T_FMAGENTA}no, ignore$T_0$T_DIM"
do
case $REPLY in
1)
echo -n $T_0 # reset from hack (in case of installer standard output)
$installer
local install_success=$?
if [ "$checker" != "icli::always_install" ]
then
$checker
install_success=$?
fi
if [ $install_success -eq 0 ]
then
icli::message "${T_FGREEN}installed ✓"
result=0
else
icli::message "${T_FRED}installation failed ✗"
result=1
fi
break;;
2)
icli::message "${T_FYELLOW}will not install ✗"
result=1
break;;
esac
done
fi
if [ -n "$fail_with" ] && [ $result = 1 ]; then
icli::message "${T_FYELLOW}$fail_with"
exit 1
fi
icli::unset_context
}
#
# helpers intended for user scripts
icli::always_install() {
return 1
}
icli::check_command() {
command -v "$1" > /dev/null
}
icli::check_envvars() {
local var
for var in "$@"
do
[ -z "${!var}" ] && return 1
done
return 0
}
#
# exit handling
# if require() was ever called,
# print the "done" message, on exit
icli_FINISHED=false
icli::finish() {
if [[ $icli_STARTED = true && $icli_FINISHED = false ]]
then
echo
icli::unset_context
icli::message "${T_FCYAN}${1}"
icli_FINISHED=true
fi
}
icli::finish_int() {
icli::finish interrupted
exit 130
}
icli::finish_exit() {
icli::finish done
}
trap icli::finish_int INT
trap icli::finish_exit EXIT
#
# end installation lib
#