-
Notifications
You must be signed in to change notification settings - Fork 195
/
tool_install.sh
executable file
·280 lines (240 loc) · 5.85 KB
/
tool_install.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
#!/bin/bash
# Exit if an error or an unset variable
set -e -u
# make sure unmatched glob gives empty
shopt -s nullglob
root_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
tools_dir=${TOOLS_DIR:-${root_dir}/tools}
downloads_dir=${DL_DIR:-${root_dir}/downloads}
tool_overrides_dir=$root_dir/make/tool_install
batch=${BATCH:-false}
force=false
remove=false
includes=true
for arg in "${@:1}"
do
[ "$arg" = "-r" ] && remove=true
[ "$arg" = "-f" ] && force=true
[ "$arg" = "-n" ] && includes=false
done
tool=${@: -1}
uname=$(uname)
if [[ "$uname" != [LD]* ]]
then
uname=Windows
fi
# Batch mode
if $batch
then
CURL_OPTIONS=(--silent -L)
else
CURL_OPTIONS=(-L)
fi
################################################################################
# Helper functions
################################################################################
function exit_error
{
error=$?
echo "${@}"
exit $error
}
## Downloads a file if it doesn't exist
#1 URL
#2 Output filename (optional)
#3+ Additional options to pass to curl
## Sets:
#out_file: path of the downloaded file
function download_file
{
out_file="$downloads_dir/${2:-$(basename "$1")}"
if ! [ -f "$out_file" ]
then
mkdir -p "$downloads_dir" && \
cd "$downloads_dir" && \
echo "Downloading $1" && \
curl "${CURL_OPTIONS[@]}" "${@:3}" -o "$out_file" "$1"
fi
}
## Unzips a file
#1 The file to unzip
#2 The output directory
function zip_extract
{
unzip -q "$1" -d "$2"
}
## Extracts a 7zip file
#1 The file to extract
#2 The output directory
function sevenzip_extract
{
if [ "$uname" = Windows ]
then
7za.exe x -o"$2" "$1"
else
7zr x -o"$2" "$1"
fi
}
## Extracts a tar file
#1 The file to extract
#2 The output directory
function tar_extract
{
tar -xf "$1" -C "$2"
}
## Extracts a file
#1 File to extract
#2 Extract directory (optional)
no_extract=false # Optional
## Sets:
#out_dir: directory the file was extracted into
function extract_file
{
out_dir="${2:-.}"
echo "Extracting $1"
mkdir -p "$out_dir" && \
case "$1" in
*.zip)
zip_extract "$1" "$out_dir"
;;
*.7z)
sevenzip_extract "$1" "$out_dir"
;;
*.tar*)
tar_extract "$1" "$out_dir"
;;
*)
if $no_extract
then
cp "$1" "$out_dir"
else
return 1
fi
esac
}
## Verifies an md5 file
#1 md5 file
#2 file to check
function md5_verify_file
{
if [ "$uname" = Darwin ]
then
[[ "$(md5 "$2")" = *"$(awk '{print $1}' "$1")"* ]]
else
( cd "$downloads_dir" && md5sum -c "$1" )
fi
}
################################################################################
# Default functions
################################################################################
function validate_target { false; }
function remove
{
rm -rf "$tools_dir/$tool_install_name"
rm -f "$tools_dir/$tool".{sh,mk}
}
declare -a depends=()
function install_deps
{
# Workaround for set -u and empty array
for dep in "${depends[@]:+${depends}}"
do
BATCH="$batch" "${BASH_SOURCE[0]}" "$dep"
done && \
source_includes
}
## Downloads and verifies the tool
## Required:
#tool_url: the url to download the tool from
## Optional:
#tool_md5
#tool_md5_url
function download_and_verify
{
verified=true
download_file "$tool_url" && \
downloaded_file=$out_file && \
if [ -n "${tool_md5_url:-}" ]
then
download_file "$tool_md5_url" "$(basename "$downloaded_file").md5" --silent && \
if ! md5_verify_file "$out_file" "$downloaded_file"
then
mv -f "$downloaded_file"{,.rej} && \
mv -f "$downloaded_file".md5{,.rej} && \
verified=false
fi
elif [ -n "${tool_md5:-}" ]
then
if [[ "$tool_md5"* != "$(cd "$downloads_dir" && md5sum "$downloaded_file")" ]]
then
mv -f "$downloaded_file"{,.rej} && \
verified=false
fi
fi && \
$verified
}
function tool_is_installed { [ -e "$full_tool_install_name" ] || which "$tool" &>/dev/null; }
## Downloads and extracts the tool
## Required:
#tool_url: the url to download the tool from
#tool_install_name: the directory or file the tool will be installed as
## Optional:
#tool_extract_dir: Directory to extract into (useful if build required)
function download_and_extract
{
local full_tool_install_name="$tools_dir/$tool_install_name"
if ! tool_is_installed || $force
then
download_and_verify || exit_error "Failed to verify $downloaded_file"
rm -rf "$full_tool_install_name" && \
extract_file "$downloaded_file" "${tool_extract_dir:-$tools_dir}"
fi
}
function build_and_install { true; } # Most tools don't need this step
## Write modules that are included by this script and make
## Optional:
bin_dir=""
bin_subdir="" #
module_file=$tool
function write_modules
{
if [ -n "$bin_subdir" ]
then
bin_dir="$tools_dir/$tool_install_name/$bin_subdir"
fi
if [ -n "$bin_dir" ]
then
local new_path="$bin_dir"':${PATH}'
# Write shell module file
echo 'if [[ ":$PATH:" != *":'"$bin_dir"':"* ]]; then export PATH='"$new_path"'; fi' > "$tools_dir/$module_file".sh
# Write make module file
echo 'ifeq ($(findstring :'"$bin_dir"':,:$(PATH):),)' > "$tools_dir/$module_file".mk
echo "export PATH := $new_path" >> "$tools_dir/$module_file".mk
echo "endif" >> "$tools_dir/$module_file".mk
fi
}
function source_includes
{
if $includes
then
for module in "$tools_dir"/*.sh
do
source "$module"
done
fi
}
################################################################################
# Peform tool install
################################################################################
source_includes || exit_error "failed to source includes"
source "$tool_overrides_dir/${tool}.sh"
if $remove
then
remove || exit_error "Failed to remove ${tool}"
else
validate_target || exit_error "${tool} is not a valid target"
install_deps || exit_error "Failed to install dependencies for ${tool}"
download_and_extract || exit_error "Failed to download and extract ${tool}"
build_and_install || exit_error "Failed to build and install ${tool}"
write_modules || exit_error "Failed to write modules for ${tool}"
fi