Refactor exit code handling and logging

This commit is contained in:
Manuel Thalmann 2023-03-25 14:13:41 +01:00
parent d64491e574
commit e75d4fadaa

View file

@ -55,25 +55,23 @@ RUN \
pattern="fixme:win:NtUserLockWindowUpdate ((nil))"; \ pattern="fixme:win:NtUserLockWindowUpdate ((nil))"; \
# The amount of times the pattern should occur for the process to be considered stuck # The amount of times the pattern should occur for the process to be considered stuck
patternCount=2; \ patternCount=2; \
output="$( \ sed \
sed \ -u \
# Handle exit hints # Handle multiple occurrences of $pattern
-e "s/^exit \([[:digit:]]\+\)$/\1/; t end;" \ -e "/$pattern/{ x; s/^x\{$(expr $patternCount - 1)\}$/\0/; t stuck; s/^\(x*\)$/\1x/; x; };" \
# Handle multiple occurrences of $pattern # Delete output and return to start
-e "/$pattern/{ x; s/^x\{$(expr $patternCount - 1)\}$/\0/; t stuck; s/^\(x*\)$/\1x/; x; };" \ -e "b;" \
# Delete output and return to start # Branch "stuck": delete output and exit with code 42
-e "d; b;" \ -e ":stuck q42;" \
# Branch "stuck": delete output and exit with code 42 # Branch "end": exit with code 0
-e ":stuck s/.*//; q42;" \ -e ":end q;"; \
# Branch "end": exit with code 0
-e ":end q;")"; \
exitCode="$?"; \ exitCode="$?"; \
# Kill installer if it got stuck (according to console output) # Kill installer if it got stuck (according to console output)
[ "$exitCode" -eq 42 ] && pkill -9 MDK537.exe; \ [ "$exitCode" -eq 42 ] && pkill -9 MDK537.exe; \
# Use `sed`s exit code if non-zero # Use `sed`s exit code if non-zero
[ "$exitCode" -ne 0 ] && exit $exitCode; \ [ "$exitCode" -ne 0 ] && exit "$exitCode" || \
# Use `sed`s output otherwise # Return true otherwise
[ "$exitCode" -eq 0 ] && exit $output; \ true; \
}; \ }; \
# Run actual MDK537 installer # Run actual MDK537 installer
install_keil() { \ install_keil() { \
@ -82,11 +80,22 @@ RUN \
logFile="$(mktemp)"; \ logFile="$(mktemp)"; \
# Run installer asynchronously # Run installer asynchronously
{ \ { \
{ 2>&1 WINEDEBUG=+all,trace-all,warn-all /usr/bin/entrypoint wine MDK537.exe --batch-install || echo "exit $?"; } \ { \
# Write output to `log.txt` local exitCode; \
| { [ "$DEBUG" -eq 1 ] && tee "$logFile" || cat; } \ exitCodeFile="$(mktemp)"; \
# Pipe stdout and stderr to `handle_output` function { \
| handle_output & \ 2>&1 WINEDEBUG=+all,trace-all,warn-all /usr/bin/entrypoint wine MDK537.exe --batch-install; \
echo "$?" > "$exitCodeFile"; \
} \
# Write output to a log file when debugging
| { [ "$DEBUG" -eq 1 ] && tee "$logFile" || cat; } \
# Pipe stdout and stderr to `handle_output` function
| handle_output; \
outputHandlerCode="$?"; \
exitCode="$(cat "$exitCodeFile")"; \
rm "$exitCodeFile"; \
bash -c "exit $([ "$outputHandlerCode" -ne 0 ] && "$outputHandlerCode" || "$exitCode")"; \
} & \
}; \ }; \
pid="$!"; \ pid="$!"; \
# Continuously display screenshots and iteration number # Continuously display screenshots and iteration number