From 944421e47e326846e04b40cd263d548b3c6a4020 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 18 Oct 2017 15:03:52 +0200 Subject: [PATCH] cc.sh/run.sh code review --- cc.sh | 40 ++++++++++++++++++++++++---------------- run.sh | 50 +++++++++++++++----------------------------------- 2 files changed, 39 insertions(+), 51 deletions(-) diff --git a/cc.sh b/cc.sh index fd5eb1929..74ecabdab 100755 --- a/cc.sh +++ b/cc.sh @@ -1,38 +1,41 @@ #!/bin/bash set -eu -PROJECT="ArchiSteamFarm" +SOLUTION="ArchiSteamFarm.sln" +CONFIGURATION="Release" OUT="out/source" -SOLUTION="${PROJECT}.sln" -CONFIGURATION="Release" +PROJECTS=("ArchiSteamFarm") CLEAN=0 +TEST=1 -PRINT_USAGE() { - echo "Usage: $0 [--clean] [debug/release]" - exit 1 -} +cd "$(dirname "$(readlink -f "$0")")" for ARG in "$@"; do case "$ARG" in release|Release) CONFIGURATION="Release" ;; debug|Debug) CONFIGURATION="Debug" ;; --clean) CLEAN=1 ;; - *) PRINT_USAGE + --no-test) TEST=0 ;; + *) echo "Usage: $0 [--clean] [--no-test] [debug/release]"; exit 1 esac done -if ! hash dotnet &>/dev/null; then +if [[ "$TEST" -eq 1 ]]; then + PROJECTS+=("ArchiSteamFarm.Tests") +fi + +trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT + +if ! hash dotnet 2>/dev/null; then echo "ERROR: dotnet CLI tools are not installed!" exit 1 fi dotnet --info -cd "$(dirname "$(readlink -f "$0")")" - -if [[ -d ".git" ]] && hash git &>/dev/null; then +if [[ -d ".git" ]] && hash git 2>/dev/null; then git pull || true fi @@ -42,14 +45,19 @@ if [[ ! -f "$SOLUTION" ]]; then fi if [[ "$CLEAN" -eq 1 ]]; then - dotnet clean -c "$CONFIGURATION" -o "$OUT" - rm -rf "ArchiSteamFarm/${OUT}" "ArchiSteamFarm.Tests/${OUT}" + dotnet clean "${PROJECTS[@]}" -c "$CONFIGURATION" -o "$OUT" + + for PROJECT in "${PROJECTS[@]}"; do + rm -rf "${PROJECT:?}/${OUT}" + done fi dotnet restore +dotnet build "${PROJECTS[@]}" -c "$CONFIGURATION" -o "$OUT" --no-restore /nologo -dotnet build -c "$CONFIGURATION" -o "$OUT" --no-restore /nologo -dotnet test ArchiSteamFarm.Tests -c "$CONFIGURATION" -o "$OUT" --no-build --no-restore +if [[ "$TEST" -eq 1 ]]; then + dotnet test ArchiSteamFarm.Tests -c "$CONFIGURATION" -o "$OUT" --no-build --no-restore +fi echo echo "Compilation finished successfully! :)" diff --git a/run.sh b/run.sh index c5d7cec43..1ef2b2502 100755 --- a/run.sh +++ b/run.sh @@ -4,49 +4,29 @@ set -eu PROJECT="ArchiSteamFarm" OUT="out/source" -BINARY="${PROJECT}/${OUT}/${PROJECT}.dll" +cd "$(dirname "$(readlink -f "$0")")" -ASF_ARGS=("") -UNTIL_CLEAN_EXIT=0 +if [[ -z "${ASF_ARGS-}" ]]; then + ASF_ARGS="" +fi -PRINT_USAGE() { - echo "Usage: $0 [--until-clean-exit] [--cryptkey=] [--path=] [--server]" - exit 1 -} +ASF_ARGS+=" $*" -for ARG in "$@"; do - case "$ARG" in - --cryptkey=*) ASF_ARGS+=("$ARG") ;; - --path=*) ASF_ARGS+=("$ARG") ;; - --server) ASF_ARGS+=("$ARG") ;; - --until-clean-exit) UNTIL_CLEAN_EXIT=1 ;; - *) PRINT_USAGE - esac -done +# Kill underlying ASF process on shell process exit +trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT -if ! hash dotnet &>/dev/null; then +if ! hash dotnet 2>/dev/null; then echo "ERROR: dotnet CLI tools are not installed!" exit 1 fi dotnet --info -cd "$(dirname "$(readlink -f "$0")")" - -if [[ ! -f "$BINARY" ]]; then - echo "ERROR: $BINARY could not be found!" - exit 1 +if grep -Eq '"Headless":\s+?true' "${PROJECT}/${OUT}/config/ASF.json"; then + # We're running ASF in headless mode so we don't need STDIN + dotnet exec "${PROJECT}/${OUT}/${PROJECT}.dll" $ASF_ARGS & # Start ASF in the background, trap will work properly due to non-blocking call + wait $! # This will forward dotnet error code, set -e will abort the script if it's non-zero +else + # We're running ASF in non-headless mode, so we need STDIN to be operative + dotnet exec "${PROJECT}/${OUT}/${PROJECT}.dll" $ASF_ARGS # Start ASF in the foreground, trap won't work until process exit fi - -if [[ "$UNTIL_CLEAN_EXIT" -eq 0 ]]; then - dotnet exec "$BINARY" "${ASF_ARGS[@]}" - exit $? # In this case $? can only be 0 because otherwise set -e terminates the script -fi - -while [[ -f "$BINARY" ]]; do - if dotnet exec "$BINARY" "${ASF_ARGS[@]}"; then - break - fi - - sleep 1 -done