From 946cd8f3224d97a5bed77de3d452f105b2434f59 Mon Sep 17 00:00:00 2001 From: Unrud Date: Sun, 5 Jun 2022 14:07:24 +0200 Subject: [PATCH] Scripts: Set errexit to catch errors --- configure-system-libraries.sh | 2 + fetch-geoip.sh | 4 +- launch-dedicated.sh | 4 +- launch-game.sh | 14 ++++--- packaging/functions.sh | 49 ++++++++++++++-------- packaging/linux/AppRun.in | 1 + packaging/linux/buildpackage.sh | 3 +- packaging/linux/openra-server.appimage.in | 2 + packaging/linux/openra-server.in | 2 + packaging/linux/openra-utility.appimage.in | 1 + packaging/linux/openra.appimage.in | 14 ++++--- packaging/linux/openra.in | 14 ++++--- packaging/macos/buildpackage.sh | 3 +- packaging/package-all.sh | 4 +- packaging/source/buildpackage.sh | 4 +- packaging/windows/buildpackage.sh | 2 +- utility.sh | 2 + 17 files changed, 84 insertions(+), 41 deletions(-) diff --git a/configure-system-libraries.sh b/configure-system-libraries.sh index 6fcd28b3b4..a25db6ae83 100755 --- a/configure-system-libraries.sh +++ b/configure-system-libraries.sh @@ -6,6 +6,8 @@ # Copy-paste the entire script into http://shellcheck.net to check. #### +set -o errexit || exit $? + patch_config() { LABEL=$1 diff --git a/fetch-geoip.sh b/fetch-geoip.sh index 423fc53fd9..3cc3805e2a 100755 --- a/fetch-geoip.sh +++ b/fetch-geoip.sh @@ -6,12 +6,14 @@ # Copy-paste the entire script into http://shellcheck.net to check. #### +set -o errexit || exit $? + # Set the working directory to the location of this script cd "$(dirname "$0")" || exit 1 # Database does not exist or is older than 30 days. if [ -z "$(find . -path ./IP2LOCATION-LITE-DB1.IPV6.BIN.ZIP -mtime -30 -print)" ]; then - rm -f IP2LOCATION-LITE-DB1.IPV6.BIN.ZIP + rm -f IP2LOCATION-LITE-DB1.IPV6.BIN.ZIP || : echo "Downloading IP2Location GeoIP database." if command -v curl >/dev/null 2>&1; then curl -s -L -O https://github.com/OpenRA/GeoIP-Database/releases/download/monthly/IP2LOCATION-LITE-DB1.IPV6.BIN.ZIP || echo "Warning: Download failed" diff --git a/launch-dedicated.sh b/launch-dedicated.sh index cbcb50a523..ff399617fc 100755 --- a/launch-dedicated.sh +++ b/launch-dedicated.sh @@ -6,6 +6,8 @@ # $ Mod="d2k" ./launch-dedicated.sh # Launch a dedicated server with default settings but override the Mod # Read the file to see which settings you can override +set -o errexit || exit $? + ENGINEDIR=$(dirname "$0") if command -v mono >/dev/null 2>&1 && [ "$(grep -c .NETCoreApp,Version= "${ENGINEDIR}/bin/OpenRA.Server.dll")" = "0" ]; then RUNTIME_LAUNCHER="mono --debug" @@ -50,5 +52,5 @@ while true; do Server.EnableLintChecks="$EnableLintChecks" \ Server.ShareAnonymizedIPs="$ShareAnonymizedIPs" \ Server.JoinChatDelay="$JoinChatDelay" \ - Engine.SupportDir="$SupportDir" + Engine.SupportDir="$SupportDir" || : done diff --git a/launch-game.sh b/launch-game.sh index 084b912a17..bef79bb19a 100755 --- a/launch-game.sh +++ b/launch-game.sh @@ -1,4 +1,6 @@ #!/bin/sh +set -o errexit || exit $? + ENGINEDIR=$(dirname "$0") if command -v mono >/dev/null 2>&1 && [ "$(grep -c .NETCoreApp,Version= "${ENGINEDIR}/bin/OpenRA.dll")" = "0" ]; then RUNTIME_LAUNCHER="mono --debug" @@ -32,10 +34,10 @@ then fi # Launch the engine with the appropriate arguments -${RUNTIME_LAUNCHER} "${ENGINEDIR}/bin/OpenRA.dll" Engine.EngineDir=".." Engine.LaunchPath="${LAUNCHPATH}" ${MODARG} "$@" +${RUNTIME_LAUNCHER} "${ENGINEDIR}/bin/OpenRA.dll" Engine.EngineDir=".." Engine.LaunchPath="${LAUNCHPATH}" ${MODARG} "$@" && rc=0 || rc=$? # Show a crash dialog if something went wrong -if [ $? != 0 ] && [ $? != 1 ]; then +if [ "${rc}" != 0 ] && [ "${rc}" != 1 ]; then if [ "$(uname -s)" = "Darwin" ]; then LOGS="${HOME}/Library/Application Support/OpenRA/Logs/" else @@ -45,12 +47,14 @@ if [ $? != 0 ] && [ $? != 1 ]; then fi fi - test -d Support/Logs && LOGS="${PWD}/Support/Logs" + if [ -d Support/Logs ]; then + LOGS="${PWD}/Support/Logs" + fi ERROR_MESSAGE=$(printf "%s has encountered a fatal error.\nPlease refer to the crash logs and FAQ for more information.\n\nLog files are located in %s\nThe FAQ is available at http://wiki.openra.net/FAQ" "OpenRA" "${LOGS}") if command -v zenity > /dev/null; then - zenity --no-wrap --error --title "OpenRA" --no-markup --text "${ERROR_MESSAGE}" 2> /dev/null + zenity --no-wrap --error --title "OpenRA" --no-markup --text "${ERROR_MESSAGE}" 2> /dev/null || : elif command -v kdialog > /dev/null; then - kdialog --title "OpenRA" --error "${ERROR_MESSAGE}" + kdialog --title "OpenRA" --error "${ERROR_MESSAGE}" || : else echo "${ERROR_MESSAGE}" fi diff --git a/packaging/functions.sh b/packaging/functions.sh index 23fab85d33..8fb31b84e1 100755 --- a/packaging/functions.sh +++ b/packaging/functions.sh @@ -23,7 +23,9 @@ # Mod SDK Windows packaging # Mod SDK macOS packaging # Mod SDK Linux AppImage packaging -install_assemblies() { +install_assemblies() ( + set -o errexit || exit $? + SRC_PATH="${1}" DEST_PATH="${2}" TARGETPLATFORM="${3}" @@ -37,8 +39,8 @@ install_assemblies() { if [ "${RUNTIME}" = "mono" ]; then echo "Building assemblies" - rm -rf "${SRC_PATH}/OpenRA."*/obj - rm -rf "${SRC_PATH:?}/bin" + rm -rf "${SRC_PATH}/OpenRA."*/obj || : + rm -rf "${SRC_PATH:?}/bin" || : msbuild -verbosity:m -nologo -t:Build -restore -p:Configuration=Release -p:TargetPlatform="${TARGETPLATFORM}" if [ "${TARGETPLATFORM}" = "unix-generic" ]; then @@ -81,7 +83,7 @@ install_assemblies() { dotnet publish -c Release -p:TargetPlatform="${TARGETPLATFORM}" -p:CopyGenericLauncher="${COPY_GENERIC_LAUNCHER}" -p:CopyCncDll="${COPY_CNC_DLL}" -p:CopyD2kDll="${COPY_D2K_DLL}" -r "${TARGETPLATFORM}" -o "${DEST_PATH}" --self-contained true fi cd "${ORIG_PWD}" || exit 1 -} +) # Copy the core engine and specified mod data to the target directory # Arguments: @@ -96,7 +98,9 @@ install_assemblies() { # Mod SDK Linux AppImage packaging # Mod SDK macOS packaging # Mod SDK Windows packaging -install_data() { +install_data() ( + set -o errexit || exit $? + SRC_PATH="${1}" DEST_PATH="${2}" shift 2 @@ -125,7 +129,7 @@ install_data() { shift done -} +) # Compile and publish (using Mono) a windows launcher with the specified mod details to the target directory # Arguments: @@ -140,8 +144,9 @@ install_data() { # Used by: # Windows packaging # Mod SDK Windows packaging -install_windows_launcher() -{ +install_windows_launcher() ( + set -o errexit || exit $? + SRC_PATH="${1}" DEST_PATH="${2}" TARGETPLATFORM="${3}" @@ -150,14 +155,14 @@ install_windows_launcher() MOD_NAME="${6}" FAQ_URL="${7}" - rm -rf "${SRC_PATH}/OpenRA.WindowsLauncher/obj" + rm -rf "${SRC_PATH}/OpenRA.WindowsLauncher/obj" || : dotnet publish "${SRC_PATH}/OpenRA.WindowsLauncher/OpenRA.WindowsLauncher.csproj" -c Release -r "${TARGETPLATFORM}" -p:LauncherName="${LAUNCHER_NAME}" -p:TargetPlatform="${TARGETPLATFORM}" -p:ModID="${MOD_ID}" -p:DisplayName="${MOD_NAME}" -p:FaqUrl="${FAQ_URL}" -o "${DEST_PATH}" --self-contained true # NET 6 is unable to customize the application host for windows when compiling from Linux, # so we must patch the properties we need in the PE header. # Setting the application icon requires an external tool, so is left to the calling code python3 "${SRC_PATH}/packaging/windows/fixlauncher.py" "${DEST_PATH}/${LAUNCHER_NAME}.exe" -} +) # Write a version string to the engine VERSION file # Arguments: @@ -171,11 +176,13 @@ install_windows_launcher() # Mod SDK Linux AppImage packaging # Mod SDK macOS packaging # Mod SDK Windows packaging -set_engine_version() { +set_engine_version() ( + set -o errexit || exit $? + VERSION="${1}" DEST_PATH="${2}" echo "${VERSION}" > "${DEST_PATH}/VERSION" -} +) # Write a version string to a list of specified mod.yamls # Arguments: @@ -189,7 +196,9 @@ set_engine_version() { # Mod SDK Linux AppImage packaging # Mod SDK macOS packaging # Mod SDK Windows packaging -set_mod_version() { +set_mod_version() ( + set -o errexit || exit $? + VERSION="${1}" shift while [ -n "${1}" ]; do @@ -199,7 +208,7 @@ set_mod_version() { rm "${MOD_YAML_PATH}.tmp" shift done -} +) # Copy launch wrappers, application icons, desktop, and MIME files to the target directory # Arguments: @@ -212,7 +221,9 @@ set_mod_version() { # MOD [MOD...]: One or more mod ids to copy (cnc, d2k, ra) # Used by: # Makefile (install-linux-shortcuts target for local installs and downstream packaging) -install_linux_shortcuts() { +install_linux_shortcuts() ( + set -o errexit || exit $? + SRC_PATH="${1}" BUILD_PATH="${2}" OPENRA_PATH="${3}" @@ -270,7 +281,7 @@ install_linux_shortcuts() { shift done -} +) # Copy AppStream metadata to the target directory # Arguments: @@ -280,7 +291,9 @@ install_linux_shortcuts() { # MOD [MOD...]: One or more mod ids to copy (cnc, d2k, ra) # Used by: # Makefile (install-linux-appdata target for local installs and downstream packaging) -install_linux_appdata() { +install_linux_appdata() ( + set -o errexit || exit $? + SRC_PATH="${1}" BUILD_PATH="${2}" SHARE_PATH="${3}" @@ -315,4 +328,4 @@ install_linux_appdata() { shift done -} +) diff --git a/packaging/linux/AppRun.in b/packaging/linux/AppRun.in index e2db84d4bb..f8b6b67161 100755 --- a/packaging/linux/AppRun.in +++ b/packaging/linux/AppRun.in @@ -1,4 +1,5 @@ #!/bin/sh +set -o errexit || exit $? HERE="$(dirname "$(readlink -f "${0}")")" diff --git a/packaging/linux/buildpackage.sh b/packaging/linux/buildpackage.sh index c8d030525d..d19888e7e7 100755 --- a/packaging/linux/buildpackage.sh +++ b/packaging/linux/buildpackage.sh @@ -1,6 +1,7 @@ #!/bin/bash # OpenRA packaging script for Linux (AppImage) -set -e + +set -o errexit || exit $? command -v tar >/dev/null 2>&1 || { echo >&2 "Linux packaging requires tar."; exit 1; } command -v curl >/dev/null 2>&1 || command -v wget > /dev/null 2>&1 || { echo >&2 "Linux packaging requires curl or wget."; exit 1; } diff --git a/packaging/linux/openra-server.appimage.in b/packaging/linux/openra-server.appimage.in index 7ea13ffa98..c4857691b9 100755 --- a/packaging/linux/openra-server.appimage.in +++ b/packaging/linux/openra-server.appimage.in @@ -1,4 +1,6 @@ #!/bin/sh +set -o errexit || exit $? + HERE="$(dirname "$(readlink -f "${0}")")" cd "${HERE}/../lib/openra" || exit 1 diff --git a/packaging/linux/openra-server.in b/packaging/linux/openra-server.in index b869d2a478..12aecf5348 100755 --- a/packaging/linux/openra-server.in +++ b/packaging/linux/openra-server.in @@ -1,4 +1,6 @@ #!/bin/sh +set -o errexit || exit $? + cd "{GAME_INSTALL_DIR}" mono {DEBUG} OpenRA.Server.dll Game.Mod={MODID} "$@" diff --git a/packaging/linux/openra-utility.appimage.in b/packaging/linux/openra-utility.appimage.in index b40563cbc5..55130d43cf 100755 --- a/packaging/linux/openra-utility.appimage.in +++ b/packaging/linux/openra-utility.appimage.in @@ -1,4 +1,5 @@ #!/bin/sh +set -o errexit || exit $? # OpenRA.Utility relies on keeping the original working directory, so don't change directory HERE="$(dirname "$(readlink -f "${0}")")" diff --git a/packaging/linux/openra.appimage.in b/packaging/linux/openra.appimage.in index 2d8e4e8335..bab1354412 100755 --- a/packaging/linux/openra.appimage.in +++ b/packaging/linux/openra.appimage.in @@ -1,4 +1,6 @@ #!/bin/sh +set -o errexit || exit $? + LAUNCHER=$(readlink -f "${0}") HERE="$(dirname "${LAUNCHER}")" cd "${HERE}/../lib/openra" || exit 1 @@ -43,21 +45,23 @@ fi # Run the game export SDL_VIDEO_X11_WMCLASS="openra-{MODID}-{TAG}" -./OpenRA Game.Mod={MODID} Engine.LaunchPath="${LAUNCHER}" "${JOIN_SERVER}" "$@" +./OpenRA Game.Mod={MODID} Engine.LaunchPath="${LAUNCHER}" "${JOIN_SERVER}" "$@" && rc=0 || rc=$? # Show a crash dialog if something went wrong -if [ $? != 0 ] && [ $? != 1 ]; then +if [ "${rc}" != 0 ] && [ "${rc}" != 1 ]; then LOGS="${XDG_CONFIG_HOME:-${HOME}/.config}/openra/Logs" if [ ! -d "${LOGS}" ] && [ -d "${HOME}/.openra/Logs" ]; then LOGS="${HOME}/.openra/Logs" fi - test -d Support/Logs && LOGS="${PWD}/Support/Logs" + if [ -d Support/Logs ]; then + LOGS="${PWD}/Support/Logs" + fi ERROR_MESSAGE=$(printf "%s has encountered a fatal error.\nPlease refer to the crash logs and FAQ for more information.\n\nLog files are located in %s\nThe FAQ is available at http://wiki.openra.net/FAQ" "{MODNAME}" "${LOGS}") if command -v zenity > /dev/null; then - zenity --no-wrap --error --title "{MODNAME}" --no-markup --text "${ERROR_MESSAGE}" 2> /dev/null + zenity --no-wrap --error --title "{MODNAME}" --no-markup --text "${ERROR_MESSAGE}" 2> /dev/null || : elif command -v kdialog > /dev/null; then - kdialog --title "{MODNAME}" --error "${ERROR_MESSAGE}" + kdialog --title "{MODNAME}" --error "${ERROR_MESSAGE}" || : elif "${HERE}/gtk-dialog.py" test > /dev/null; then "${HERE}/gtk-dialog.py" error --title "{MODNAME}" --text "${ERROR_MESSAGE}" 2> /dev/null else diff --git a/packaging/linux/openra.in b/packaging/linux/openra.in index 3068693425..4669c9cae5 100755 --- a/packaging/linux/openra.in +++ b/packaging/linux/openra.in @@ -1,4 +1,6 @@ #!/bin/sh +set -o errexit || exit $? + cd "{GAME_INSTALL_DIR}" # Search for server connection @@ -9,21 +11,23 @@ if [ "${1#${PROTOCOL_PREFIX}}" != "${1}" ]; then fi # Run the game -mono {DEBUG} OpenRA.dll Game.Mod={MODID} Engine.LaunchPath="{BIN_DIR}/openra-{MODID}" "${JOIN_SERVER}" "$@" +mono {DEBUG} OpenRA.dll Game.Mod={MODID} Engine.LaunchPath="{BIN_DIR}/openra-{MODID}" "${JOIN_SERVER}" "$@" && rc=0 || rc=$? # Show a crash dialog if something went wrong -if [ $? != 0 ] && [ $? != 1 ]; then +if [ "${rc}" != 0 ] && [ "${rc}" != 1 ]; then LOGS="${XDG_CONFIG_HOME:-${HOME}/.config}/openra/Logs" if [ ! -d "${LOGS}" ] && [ -d "${HOME}/.openra/Logs" ]; then LOGS="${HOME}/.openra/Logs" fi - test -d Support/Logs && LOGS="${PWD}/Support/Logs" + if [ -d Support/Logs ]; then + LOGS="${PWD}/Support/Logs" + fi ERROR_MESSAGE=$(printf "%s has encountered a fatal error.\nPlease refer to the crash logs and FAQ for more information.\n\nLog files are located in %s\nThe FAQ is available at http://wiki.openra.net/FAQ" "{MODNAME}" "${LOGS}") if command -v zenity > /dev/null; then - zenity --no-wrap --error --title "{MODNAME}" --no-markup --text "${ERROR_MESSAGE}" 2> /dev/null + zenity --no-wrap --error --title "{MODNAME}" --no-markup --text "${ERROR_MESSAGE}" 2> /dev/null || : elif command -v kdialog > /dev/null; then - kdialog --title "{MODNAME}" --error "${ERROR_MESSAGE}" + kdialog --title "{MODNAME}" --error "${ERROR_MESSAGE}" || : else echo "${ERROR_MESSAGE}" fi diff --git a/packaging/macos/buildpackage.sh b/packaging/macos/buildpackage.sh index 99dceed51c..6800ea2097 100755 --- a/packaging/macos/buildpackage.sh +++ b/packaging/macos/buildpackage.sh @@ -13,7 +13,8 @@ # MACOS_DEVELOPER_USERNAME: Email address for the developer account # MACOS_DEVELOPER_PASSWORD: App-specific password for the developer account # -set -e + +set -o errexit || exit $? MONO_TAG="osx-launcher-20201222" diff --git a/packaging/package-all.sh b/packaging/package-all.sh index 847417d68b..9ce2104256 100755 --- a/packaging/package-all.sh +++ b/packaging/package-all.sh @@ -1,6 +1,8 @@ #!/bin/bash # OpenRA master packaging script +set -o errexit || exit $? + if [ $# -ne "2" ]; then echo "Usage: ${0##*/} version outputdir." exit 1 @@ -19,14 +21,12 @@ function build_package() ( } #trap function executes on any error in the following commands trap "on_build $1" ERR - set -e echo "Building $1 package(s)." cd "$1" ./buildpackage.sh "${GIT_TAG}" "${BUILD_OUTPUT_DIR}" ) #exit on any non-zero exited (failed) command -set -e if [[ "$OSTYPE" == "darwin"* ]]; then build_package macos else diff --git a/packaging/source/buildpackage.sh b/packaging/source/buildpackage.sh index b83f975e9e..746e316333 100755 --- a/packaging/source/buildpackage.sh +++ b/packaging/source/buildpackage.sh @@ -1,6 +1,8 @@ #!/bin/bash # OpenRA packaging script for versioned source tarball +set -o errexit || exit $? + if [ $# -ne "2" ]; then echo "Usage: $(basename "$0") tag outputdir" exit 1 @@ -19,7 +21,7 @@ make version VERSION="${TAG}" # The output from `git ls-tree` is too long to fit in a single command (overflows MAX_ARG_STRLEN) # so `xargs` will automatically split the input across multiple `tar` commands. # Use the amend flag (r) to prevent each call erasing the output from earlier calls. -rm "${OUTPUTDIR}/OpenRA-${TAG}-source.tar" +rm "${OUTPUTDIR}/OpenRA-${TAG}-source.tar" || : git ls-tree HEAD --name-only -r -z | xargs -0 tar vrf "${OUTPUTDIR}/OpenRA-${TAG}-source.tar" bzip2 "${OUTPUTDIR}/OpenRA-${TAG}-source.tar" diff --git a/packaging/windows/buildpackage.sh b/packaging/windows/buildpackage.sh index b69b897a34..5d17e8ee73 100755 --- a/packaging/windows/buildpackage.sh +++ b/packaging/windows/buildpackage.sh @@ -1,7 +1,7 @@ #!/bin/bash # OpenRA packaging script for Windows -set -e +set -o errexit || exit $? command -v curl >/dev/null 2>&1 || command -v wget > /dev/null 2>&1 || { echo >&2 "Windows packaging requires curl or wget."; exit 1; } command -v makensis >/dev/null 2>&1 || { echo >&2 "Windows packaging requires makensis."; exit 1; } diff --git a/utility.sh b/utility.sh index 6c1886cb21..00ce9d467e 100755 --- a/utility.sh +++ b/utility.sh @@ -1,4 +1,6 @@ #!/bin/sh +set -o errexit || exit $? + ENGINEDIR=$(dirname "$0") if command -v mono >/dev/null 2>&1 && [ "$(grep -c .NETCoreApp,Version= "${ENGINEDIR}/bin/OpenRA.Utility.dll")" = "0" ]; then RUNTIME_LAUNCHER="mono --debug"