Skip to content

Commit 2b26f23

Browse files
committed
Trap failures and print an error message. Use file descriptors to avoid swallowing errors.
1 parent 1a8c143 commit 2b26f23

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

bin/ruby-build

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22

3-
set -e
3+
set -E
4+
exec 3<&2 # preserve original stderr at fd 3
45

56
abs_dirname() {
67
local cwd="$(pwd)"
@@ -16,12 +17,15 @@ abs_dirname() {
1617
cd "$cwd"
1718
}
1819

19-
log() {
20-
if [ -z "$VERBOSE" ]; then
21-
cat >>"$LOG_PATH"
22-
else
23-
tee -a "$LOG_PATH"
24-
fi
20+
build_failed() {
21+
{ echo
22+
echo "BUILD FAILED"
23+
echo
24+
echo "Inspect or clean up the working tree at ${TEMP_PATH}"
25+
echo "Results logged to ${LOG_PATH}"
26+
echo
27+
} >&3
28+
exit 1
2529
}
2630

2731
install_package() {
@@ -47,14 +51,14 @@ download_package() {
4751

4852
echo "Downloading ${package_url}..." >&2
4953
{ curl "$package_url" > "${package_name}.tar.gz"
50-
} 2>&1 | log
54+
} >&4 2>&1
5155
}
5256

5357
extract_package() {
5458
local package_name="$1"
5559

5660
{ tar xzvf "${package_name}.tar.gz"
57-
} 2>&1 | log
61+
} >&4 2>&1
5862
}
5963

6064
build_package() {
@@ -80,22 +84,22 @@ build_package_standard() {
8084
{ ./configure --prefix="$PREFIX_PATH"
8185
make -j 2
8286
make install
83-
} 2>&1 | log
87+
} >&4 2>&1
8488
}
8589

8690
build_package_ruby() {
8791
local package_name="$1"
8892

8993
{ "$RUBY_BIN" setup.rb
90-
} 2>&1 | log
94+
} >&4 2>&1
9195
}
9296

9397
build_package_rbx() {
9498
local package_name="$1"
9599

96100
{ ./configure --prefix="$PREFIX_PATH"
97101
rake install
98-
} 2>&1 | log
102+
} >&4 2>&1
99103
}
100104

101105
build_package_copy() {
@@ -181,12 +185,20 @@ TEMP_PATH="/tmp/ruby-build.${SEED}"
181185
RUBY_BIN="${PREFIX_PATH}/bin/ruby"
182186
CWD="$(pwd)"
183187

188+
exec 4<> "$LOG_PATH" # open the log file at fd 4
189+
if [ -n "$VERBOSE" ]; then
190+
tail -f "$LOG_PATH" &
191+
trap "kill 0" SIGINT SIGTERM EXIT
192+
fi
193+
184194
export LDFLAGS="-L'${PREFIX_PATH}/lib' ${LDFLAGS}"
185195
export CPPFLAGS="-I'${PREFIX_PATH}/include' ${CPPFLAGS}"
186196

187197
unset RUBYOPT
188198
unset RUBYLIB
189199

200+
trap build_failed ERR
190201
mkdir -p "$TEMP_PATH"
191202
source "$DEFINITION_PATH"
192203
rm -fr "$TEMP_PATH"
204+
trap - ERR

0 commit comments

Comments
 (0)