diff --git a/Scripts/Common/Functions.sh b/Scripts/Common/Functions.sh index b9cd35ffe..0f7c05881 100644 --- a/Scripts/Common/Functions.sh +++ b/Scripts/Common/Functions.sh @@ -16,6 +16,30 @@ #along with this program. If not, see . umask 0022; +_fetchError(){ + local last_status="$1"; + local error_line_number="$2"; + local last_func="$3"; + local file=$(echo "$4" | sed "s#$DOS_WORKSPACE_ROOT#\$DOS_WORKSPACE_ROOT#g"); + + # ignore when pressing TAB or sim. + if [[ "$file" =~ .*bash_completion ]];then return; fi + case $last_func in + command_not_found_handle|_filedir) return;; + esac + + if [ ! -z "$last_func" ] && [ ! -z "$file" ];then + echo -e "\e[0;31mERROR: $file -> ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m"; + elif [ ! -z "$last_func" ];then + echo -e "\e[0;31mERROR: ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m"; + else + echo -e "\e[0;31mERROR: last command ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m"; + fi + export TR_ERR=$last_status + _exit_report +} +export -f _fetchError; + startPatcher() { java -jar "$DOS_BINARY_PATCHER" patch workspace "$DOS_BUILD_BASE" "$DOS_WORKSPACE_ROOT""Patches/Linux/" "$DOS_SCRIPTS_CVES" $1; } @@ -61,7 +85,7 @@ enterAndClear() { export -f enterAndClear; gitReset() { - git add -A && git reset --hard; + (git add -A && git reset --hard) || true; } export -f gitReset; @@ -75,16 +99,22 @@ applyPatchReal() { git format-patch -1 HEAD --zero-commit --no-signature --output="$currentWorkingPatch"; fi; fi; + else + echo "Applying (git am): $currentWorkingPatch - FAILED" + git am --abort || true + echo "Applying (patch fallback): $currentWorkingPatch" + patch -r - --no-backup-if-mismatch --forward --ignore-whitespace --verbose -p1 < $currentWorkingPatch fi; else - git apply "$@"; echo "Applying (as diff): $currentWorkingPatch"; + git apply "$@"; fi; } export -f applyPatchReal; applyPatch() { currentWorkingPatch=$1; + set -E if [ -f "$currentWorkingPatch" ]; then if git apply --check "$@" &> /dev/null; then applyPatchReal "$@"; @@ -97,11 +127,13 @@ applyPatch() { echo "Applied (as 3way): $currentWorkingPatch"; else echo -e "\e[0;31mERROR: Cannot apply: $currentWorkingPatch\e[0m"; + false fi; fi; fi; else echo -e "\e[0;31mERROR: Patch doesn't exist: $currentWorkingPatch\e[0m"; + false fi; } export -f applyPatch; @@ -380,7 +412,7 @@ processRelease() { echo "$INCREMENTAL_ID" > "$OUT_DIR/$PREFIX-target_files.zip.id"; #Image - unzip -l $OUT_DIR/$PREFIX-target_files.zip | grep -q recovery.img; + unzip -l $OUT_DIR/$PREFIX-target_files.zip | grep -q recovery.img || true; local hasRecoveryImg="$?"; unzip -l $OUT_DIR/$PREFIX-target_files.zip | grep -q dtbo.img; local hasDtboImg="$?"; diff --git a/Scripts/Common/Post.sh b/Scripts/Common/Post.sh index a3c914725..ca257417e 100644 --- a/Scripts/Common/Post.sh +++ b/Scripts/Common/Post.sh @@ -58,3 +58,5 @@ sed -i '/android.software.managed_users/s/notLowRam="true"//' frameworks/native/ cd "$DOS_BUILD_BASE"; echo -e "\e[0;32m[SCRIPT COMPLETE] Post tweaks complete\e[0m"; + +kill -USR2 $TR_PID diff --git a/Scripts/init.sh b/Scripts/init.sh index 1d06d163d..a0167bf33 100644 --- a/Scripts/init.sh +++ b/Scripts/init.sh @@ -117,6 +117,78 @@ export DOS_THEME_700="E64A19"; #Deep Orange 700 umask 0022; +export TR_ERR=0 +export TR_PID=$$ +unset nokill +if [ -z "$UNATTENDED_PATCHING" ];then export UNATTENDED_PATCHING=1;fi + +set -E; #required for resetEnv() +resetEnv(){ + trap - ERR EXIT USR2 SIGINT SIGHUP TERM + echo -e "\n\e[0;32mThe environment has been reset.\e[0m\nRemember to always '\e[0;31msource ../../Scripts/init.sh\e[0m' before building.\n" + set +E +f +}; export -f resetEnv + +# print result +# will also ensure the corresponding status code gets returned properly +_errorReport(){ + if [ "$TR_ERR" -ne 0 ];then + echo -e "\n\e[0;31m[FINAL RESULT] Serious error(s) found!!!\nSummary error code was: $TR_ERR. Check & fix all error lines above\e[0m" + else + echo -e "\n\e[0;32m[FINAL RESULT] No error detected (please check the above output nevertheless!)\e[0m" + fi + return $TR_ERR +}; export -f _errorReport + +# exit +_exit(){ + if [ "$1" == "noreset" ] || [ $TR_ERR -eq 0 ] ;then + echo -e "Ended with $TR_ERR.\nThe shell env has NOT been reset, type: resetEnv if needed.\n" + else + if [ -z "$nokill" ];then nokill=0;fi + resetEnv + echo -e "\nExecution has been STOPPED (TR_ERR=$TR_ERR)." + if [ "$UNATTENDED_PATCHING" -eq 1 ];then + echo -e "\n\e[0;31mPressing any key or waiting 10s will close this shell (set UNATTENDED_PATCHING=0 to disable auto-close)!\e[0m" + read -t 10 -p "- press any key to exit the shell NOW (auto closes after 10s) -" DUMMY || true + else + read -p "- press any key to exit the shell NOW -" DUMMY || true + fi + _SPIDS=$(ps -s $TR_PID -o pid= | tr '\n' ' ') + if [ -z "$_SPIDS" ];then + echo -e "... ok, no childs running (I am: $TR_PID)" + else + echo -e "... killing childs: $_SPIDS" + kill -9 $_SPIDS + fi + if [ $nokill -eq 0 ];then + echo "... killing shell: $TR_PID" + kill -9 $TR_PID + fi + fi +}; export -f _exit + +# exit & reset & report +_exit_report(){ + _errorReport + _exit +}; export -f _exit_report + +# exit without reset/kill +_exit_sigint(){ + echo -e "\n\nCTRL+C pressed or process has been terminated.." + _exit noreset +}; export _exit_sigint + +# trap and print errors +# ERR: needed to fetch aborts when set -e is set +trap 'E=$?; \ + [ $E -ne 0 ] && _fetchError $E $LINENO $FUNCNAME $BASH_SOURCE \ + && export TR_ERR=$((TR_ERR + $E))' EXIT ERR + +trap _exit_report SIGUSR2 USR2 +trap _exit_sigint SIGINT SIGHUP TERM + gpgVerifyGitHead() { if [ -r "$DOS_TMP_GNUPG/pubring.kbx" ]; then if git -C "$1" verify-commit HEAD &>/dev/null; then @@ -203,5 +275,5 @@ source "$DOS_SCRIPTS_COMMON/Functions.sh"; source "$DOS_SCRIPTS_COMMON/Tag_Verifier.sh"; source "$DOS_SCRIPTS/Functions.sh"; -[[ -f "$DOS_BUILD_BASE/.repo/local_manifests/roomservice.xml" ]] && echo "roomservice manifest found! Please fix your manifests before continuing!"; -[[ -f "$DOS_BUILD_BASE/DOS_PATCHED_FLAG" ]] && echo "NOTE: THIS WORKSPACE IS ALREADY PATCHED, PLEASE RESET BEFORE PATCHING AGAIN!"; +[[ -f "$DOS_BUILD_BASE/.repo/local_manifests/roomservice.xml" ]] && echo "roomservice manifest found! Please fix your manifests before continuing!" || true; +[[ -f "$DOS_BUILD_BASE/DOS_PATCHED_FLAG" ]] && echo "NOTE: THIS WORKSPACE IS ALREADY PATCHED, PLEASE RESET BEFORE PATCHING AGAIN!" || true;