From 2b26f23dde6e85f4782ff4f5ec826d2711765f7f Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sun, 7 Aug 2011 15:41:20 -0500 Subject: [PATCH] Trap failures and print an error message. Use file descriptors to avoid swallowing errors. --- bin/ruby-build | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/bin/ruby-build b/bin/ruby-build index fb9c9894..02e4258a 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -1,6 +1,7 @@ #!/bin/bash -set -e +set -E +exec 3<&2 # preserve original stderr at fd 3 abs_dirname() { local cwd="$(pwd)" @@ -16,12 +17,15 @@ abs_dirname() { cd "$cwd" } -log() { - if [ -z "$VERBOSE" ]; then - cat >>"$LOG_PATH" - else - tee -a "$LOG_PATH" - fi +build_failed() { + { echo + echo "BUILD FAILED" + echo + echo "Inspect or clean up the working tree at ${TEMP_PATH}" + echo "Results logged to ${LOG_PATH}" + echo + } >&3 + exit 1 } install_package() { @@ -47,14 +51,14 @@ download_package() { echo "Downloading ${package_url}..." >&2 { curl "$package_url" > "${package_name}.tar.gz" - } 2>&1 | log + } >&4 2>&1 } extract_package() { local package_name="$1" { tar xzvf "${package_name}.tar.gz" - } 2>&1 | log + } >&4 2>&1 } build_package() { @@ -80,14 +84,14 @@ build_package_standard() { { ./configure --prefix="$PREFIX_PATH" make -j 2 make install - } 2>&1 | log + } >&4 2>&1 } build_package_ruby() { local package_name="$1" { "$RUBY_BIN" setup.rb - } 2>&1 | log + } >&4 2>&1 } build_package_rbx() { @@ -95,7 +99,7 @@ build_package_rbx() { { ./configure --prefix="$PREFIX_PATH" rake install - } 2>&1 | log + } >&4 2>&1 } build_package_copy() { @@ -181,12 +185,20 @@ TEMP_PATH="/tmp/ruby-build.${SEED}" RUBY_BIN="${PREFIX_PATH}/bin/ruby" CWD="$(pwd)" +exec 4<> "$LOG_PATH" # open the log file at fd 4 +if [ -n "$VERBOSE" ]; then + tail -f "$LOG_PATH" & + trap "kill 0" SIGINT SIGTERM EXIT +fi + export LDFLAGS="-L'${PREFIX_PATH}/lib' ${LDFLAGS}" export CPPFLAGS="-I'${PREFIX_PATH}/include' ${CPPFLAGS}" unset RUBYOPT unset RUBYLIB +trap build_failed ERR mkdir -p "$TEMP_PATH" source "$DEFINITION_PATH" rm -fr "$TEMP_PATH" +trap - ERR