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))"; \
# The amount of times the pattern should occur for the process to be considered stuck
patternCount=2; \
output="$( \
sed \
# Handle exit hints
-e "s/^exit \([[:digit:]]\+\)$/\1/; t end;" \
-u \
# Handle multiple occurrences of $pattern
-e "/$pattern/{ x; s/^x\{$(expr $patternCount - 1)\}$/\0/; t stuck; s/^\(x*\)$/\1x/; x; };" \
# Delete output and return to start
-e "d; b;" \
-e "b;" \
# Branch "stuck": delete output and exit with code 42
-e ":stuck s/.*//; q42;" \
-e ":stuck q42;" \
# Branch "end": exit with code 0
-e ":end q;")"; \
-e ":end q;"; \
exitCode="$?"; \
# Kill installer if it got stuck (according to console output)
[ "$exitCode" -eq 42 ] && pkill -9 MDK537.exe; \
# Use `sed`s exit code if non-zero
[ "$exitCode" -ne 0 ] && exit $exitCode; \
# Use `sed`s output otherwise
[ "$exitCode" -eq 0 ] && exit $output; \
[ "$exitCode" -ne 0 ] && exit "$exitCode" || \
# Return true otherwise
true; \
}; \
# Run actual MDK537 installer
install_keil() { \
@ -82,11 +80,22 @@ RUN \
logFile="$(mktemp)"; \
# 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; } \
# 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="$!"; \
# Continuously display screenshots and iteration number