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 \
# Handle exit hints -u \
-e "s/^exit \([[:digit:]]\+\)$/\1/; t end;" \
# Handle multiple occurrences of $pattern # Handle multiple occurrences of $pattern
-e "/$pattern/{ x; s/^x\{$(expr $patternCount - 1)\}$/\0/; t stuck; s/^\(x*\)$/\1x/; x; };" \ -e "/$pattern/{ x; s/^x\{$(expr $patternCount - 1)\}$/\0/; t stuck; s/^\(x*\)$/\1x/; x; };" \
# Delete output and return to start # Delete output and return to start
-e "d; b;" \ -e "b;" \
# Branch "stuck": delete output and exit with code 42 # Branch "stuck": delete output and exit with code 42
-e ":stuck s/.*//; q42;" \ -e ":stuck q42;" \
# Branch "end": exit with code 0 # Branch "end": exit with code 0
-e ":end q;")"; \ -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; \
exitCodeFile="$(mktemp)"; \
{ \
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; } \ | { [ "$DEBUG" -eq 1 ] && tee "$logFile" || cat; } \
# Pipe stdout and stderr to `handle_output` function # Pipe stdout and stderr to `handle_output` function
| handle_output & \ | 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