forked from hpi-swa/smalltalkCI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.sh
413 lines (328 loc) · 10.2 KB
/
helpers.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
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
################################################################################
# This file provides helper functions for smalltalkCI. It is used in the context
# of a smalltalkCI build and it is not meant to be executed by itself.
################################################################################
GITHUB_API="https://api.github.com"
COVERALLS_API="https://coveralls.io/api/v1/jobs"
ANSI_BOLD="\033[1m"
ANSI_RED="\033[31m"
ANSI_GREEN="\033[32m"
ANSI_YELLOW="\033[33m"
ANSI_BLUE="\033[34m"
ANSI_RESET="\033[0m"
ANSI_CLEAR="\033[0K"
print_info() {
printf "${ANSI_BOLD}${ANSI_BLUE}%s${ANSI_RESET}\n" "$1"
}
print_notice() {
printf "${ANSI_BOLD}${ANSI_YELLOW}%s${ANSI_RESET}\n" "$1"
}
print_success() {
printf "${ANSI_BOLD}${ANSI_GREEN}%s${ANSI_RESET}\n" "$1"
}
print_error() {
printf "${ANSI_BOLD}${ANSI_RED}%s${ANSI_RESET}\n" "$1" 1>&2
}
print_error_and_exit() {
local error_code="${2:-1}" # 2nd parameter, 1 if not set
print_error "$1"
report_build_metrics "${error_code}"
exit "${error_code}"
}
print_help() {
cat <<EOF
USAGE: $(basename -- $0) [options] /path/to/project/your_smalltalk.ston
This program prepares Smalltalk images/vms, loads projects and runs tests.
OPTIONS:
--clean Clear cache and delete builds.
-d | --debug Enable debug mode.
-h | --help Show this help text.
--headful Open vm in headful mode and do not close image.
--install Install symlink to this smalltalkCI instance.
--no-tracking Disable collection of anonymous build metrics (Travis CI & AppVeyor only).
-s | --smalltalk Overwrite Smalltalk image selection.
--uninstall Remove symlink to any smalltalkCI instance.
-v | --verbose Enable 'set -x'.
GEMSTONE OPTIONS:
--gs-BRANCH=<branch-SHA-tag>
Name of GsDevKit_home branch, SHA, or tag. Default is 'master'.
Environment variable GSCI_DEVKIT_BRANCH may be used to
specify <branch-SHA-tag>. Command line option overrides
value of environment variable.
--gs-HOME=<GS_HOME-path>
Path to an existing GsDevKit_home clone to be used
instead of creating a fresh clone.
--gs-DEVKIT_BRANCH option is ignored.
--gs-CLIENTS="<smalltalk-platform>..."
List of Smalltalk client versions to use as a GemStone client.
Environment variable GSCI_CLIENTS may also be used to
specify a list of <smalltalk-platform> client versions.
Command line option overrides value of environment variable.
If a client is specified, tests are run for both the client
and server based using the project .smalltalk.ston file.
EXAMPLE:
$(basename -- $0) -s "Squeak-trunk" --headfull /path/to/project/.smalltalk.ston
EOF
}
print_config() {
for var in ${!config_@}; do
echo "${var}=${!var}"
done
}
is_empty() {
local var=$1
[[ -z $var ]]
}
is_not_empty() {
local var=$1
[[ -n $var ]]
}
is_file() {
local file=$1
[[ -f $file ]]
}
is_dir() {
local dir=$1
[[ -d $dir ]]
}
is_nonzero() {
local status=$1
[[ "${status}" -ne 0 ]]
}
is_int() {
local value=$1
[[ $value =~ ^-?[0-9]+$ ]]
}
program_exists() {
local program=$1
[[ $(which "${program}" 2> /dev/null) ]]
}
is_travis_build() {
[[ "${TRAVIS:-}" = "true" ]]
}
is_appveyor_build() {
[[ "${APPVEYOR:-}" = "True" ]]
}
is_cygwin_build() {
[[ $(uname -s) = "CYGWIN_NT-"* ]]
}
is_spur_image() {
local image_path=$1
local image_format_number
# "[...] bit 5 of the format number identifies an image that requires Spur
# support from the VM [...]"
# http://forum.world.st/VM-Maker-ImageFormat-dtl-17-mcz-td4713569.html
local spur_bit=5
if is_empty "${image_path}"; then
print_error "Image not found at '${image_path}'."
return 0
fi
image_format_number="$(hexdump -n 4 -e '2/4 "%04d " "\n"' "${image_path}")"
[[ $((image_format_number>>(spur_bit-1) & 1)) -eq 1 ]]
}
is_headless() {
[[ "${config_headless}" = "true" ]]
}
debug_enabled() {
[[ "${config_debug}" = "true" ]]
}
conditional_debug_halt() {
if ! is_headless && debug_enabled; then
printf "self halt.\n"
fi
}
download_file() {
local url=$1
local target=$2
if is_empty "${url}" || is_empty "${target}"; then
print_error_and_exit "download_file() expects an URL and a target path."
fi
if program_exists "curl"; then
curl -f -s -L --retry 3 -o "${target}" "${url}" || print_error_and_exit \
"curl failed to download ${url} to '${target}'."
elif program_exists "wget"; then
wget -q -O "${target}" "${url}" || print_error_and_exit \
"wget failed to download ${url} to '${target}'."
else
print_error_and_exit "Please install curl or wget."
fi
}
resolve_path() {
local path=$1
if is_cygwin_build; then
echo $(cygpath -w "${path}")
else
echo "${path}"
fi
}
return_vars() {
(IFS="|"; echo "$*")
}
set_vars() {
local variables=(${@:1:(($# - 1))})
local values="${!#}"
IFS="|" read -r "${variables[@]}" <<< "${values}"
}
to_lowercase() {
echo $1 | tr "[:upper:]" "[:lower:]"
}
git_log() {
local format_value=$1
local output
output=$(git --no-pager log -1 --pretty=format:"${format_value}")
echo "${output/\"/\\\"}" # Escape double quotes
}
export_coveralls_data() {
local service_name
if is_travis_build; then
service_name="travis-ci"
elif is_appveyor_build; then
service_name="appveyor"
else
return 0 # Coverage testing only supported on TravisCI and AppVeyor
fi
cat >"${SMALLTALK_CI_BUILD}/coveralls_data.json" <<EOL
{
"git": {
"branch": "${TRAVIS_BRANCH:-${APPVEYOR_REPO_BRANCH:-}}",
"head": {
"author_email": "$(git_log "%ae")",
"author_name": "$(git_log "%aN")",
"committer_email": "$(git_log "%ce")",
"committer_name": "$(git_log "%cN")",
"id": "$(git_log "%H")",
"message": "$(git_log "%s")"
},
"remotes": [
{
"url": "https://github.com/${TRAVIS_REPO_SLUG:-${APPVEYOR_REPO_NAME:-}}.git",
"name": "origin"
}
]
},
"service_job_id": "${TRAVIS_JOB_ID:-${APPVEYOR_BUILD_ID:-}}",
"service_name": "${service_name}"
}
EOL
}
upload_coverage_results() {
local curl_status=0
local coverage_results="${SMALLTALK_CI_BUILD}/coveralls_results.json"
if is_file "${coverage_results}"; then
print_info "Uploading coverage results to Coveralls..."
curl -s -F json_file="@${coverage_results}" "${COVERALLS_API}" > /dev/null || curl_status=$?
if is_nonzero "${curl_status}"; then
print_error "Failed to upload coverage results (curl error code #${curl_status})"
fi
fi
}
report_build_metrics() {
local build_status=$1
local env_name
local project_slug
local api_url
local status_code
local duration=$(($(timer_nanoseconds)-$smalltalk_ci_start_time))
duration=$(echo "${duration}" | awk '{printf "%.3f\n", $1/1000000000}')
if [[ "${config_tracking}" != "true" ]]; then
return 0
fi
if is_travis_build; then
env_name="TravisCI"
elif is_appveyor_build; then
env_name="AppVeyor"
else
return 0 # Only report build metrics when running on TravisCI or AppVeyor
fi
project_slug="${TRAVIS_REPO_SLUG:-${APPVEYOR_REPO_NAME:-}}"
api_url="${GITHUB_API}/repos/${project_slug}"
status_code=$(curl -w %{http_code} -s -o /dev/null "${api_url}")
if [[ "${status_code}" != "200" ]]; then
return 0 # Not a public repository
fi
curl -s --header "X-BUILD-DURATION: ${duration}" \
--header "X-BUILD-ENV: ${env_name}" \
--header "X-BUILD-SMALLTALK: ${config_smalltalk}" \
--header "X-BUILD-STATUS: ${build_status}" \
"https://smalltalkci.fniephaus.com/api/" > /dev/null || true
}
################################################################################
# Travis-related helper functions (based on https://git.io/vzcTj).
################################################################################
timer_start() {
timer_start_time=$(timer_nanoseconds)
if is_travis_build; then
travis_timer_id=$(printf %08x $(( RANDOM * RANDOM )))
echo -en "travis_time:start:$travis_timer_id\r${ANSI_CLEAR}"
fi
}
timer_finish() {
local timer_end_time=$(timer_nanoseconds)
local duration=$(($timer_end_time-$timer_start_time))
if is_travis_build; then
echo -en "travis_time:end:$travis_timer_id:start=$timer_start_time,finish=$timer_end_time,duration=$duration\r${ANSI_CLEAR}"
else
duration=$(echo "${duration}" | awk '{printf "%.3f\n", $1/1000000000}')
printf "${ANSI_RESET}${ANSI_BLUE} > Time to run: %ss ${ANSI_RESET}\n" "${duration}"
fi
}
timer_nanoseconds() {
local cmd="date"
local format="+%s%N"
local os=$(uname)
if hash gdate > /dev/null 2>&1; then
cmd="gdate" # use gdate if available
elif [[ "${os}" = Darwin ]]; then
format="+%s000000000" # fallback to second precision on darwin (does not support %N)
fi
$cmd -u $format
}
travis_wait() {
local timeout="${SMALLTALK_CI_TIMEOUT:-}"
local cmd="$@"
if ! is_int "${timeout}"; then
$cmd
return $?
fi
$cmd &
local cmd_pid=$!
travis_jigger $! $timeout $cmd &
local jigger_pid=$!
local result
{
wait $cmd_pid 2>/dev/null
result=$?
ps -p$jigger_pid &>/dev/null && kill $jigger_pid
}
if [ $result -ne 0 ]; then
print_error_and_exit "The command $cmd exited with $result."
fi
return $result
}
travis_jigger() {
# helper method for travis_wait()
local cmd_pid=$1
shift
local timeout=$1 # in minutes
shift
local count=0
while [ $count -lt $timeout ]; do
count=$(($count + 1))
echo -e "Still running ($count of $timeout): $@"
sleep 60
done
echo -e "\n${ANSI_BOLD}${ANSI_RED}Timeout (${timeout} minutes) reached. Terminating \"$@\"${ANSI_RESET}\n"
kill -9 $cmd_pid
}
travis_fold() {
local action=$1
local name=$2
local title="${3:-}"
local prefix="${SMALLTALK_CI_TRAVIS_FOLD_PREFIX:-}"
if is_travis_build; then
echo -en "travis_fold:${action}:${prefix}${name}\r${ANSI_CLEAR}"
fi
if is_not_empty "${title}"; then
echo -e "${ANSI_BOLD}${ANSI_BLUE}${title}${ANSI_RESET}"
fi
}