Skip to content

Instantly share code, notes, and snippets.

@monkeydom
Created February 9, 2023 14:34
Show Gist options
  • Save monkeydom/511906d98f369b5712adec4004402bd6 to your computer and use it in GitHub Desktop.
Save monkeydom/511906d98f369b5712adec4004402bd6 to your computer and use it in GitHub Desktop.
extracting all the attachments of an .xcresults bundle with xcode tools being the only dependency
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
echo 'Usage: ./extractAttachments.sh <xcresult>
Extracts all the attachments of a .xcresult bundle into the current directory'
exit
fi
XCRESULT=$1
echo "Loooking through $XCRESULT"
ACTION_RESULTS=$(xcrun xcresulttool graph --path ${XCRESULT} | grep ActionTestSummary -A1 | grep Id)
echo "Results:"
IFS=$'\n'
for xcidline in $ACTION_RESULTS; do
echo "$xcidline"
xcid=${xcidline##* } # the last field of xcid line delimited by spaces
TEST_SUMMARY=$(xcrun xcresulttool get --format json --path ${XCRESULT} --id "$xcid")
#echo "$TEST_SUMMARY"
PLIST=$(plutil -convert xml1 - -o - <<<"$TEST_SUMMARY")
#echo "$PLIST"
SUMMARIES=$(plutil -extract activitySummaries._values xml1 - -o - <<<"$PLIST")
# echo "--- Summaries ---"
#echo "$SUMMARIES"
summary_count=$(plutil -convert raw - -o - <<<"$SUMMARIES")
#echo "Summary count: $summary_count"
for (( si=0; si<$summary_count; si++ )); do
# echo "si: $si"
attachments=$(plutil -extract ${si}.attachments._values xml1 - -o - <<<"$SUMMARIES")
# echo "attachments: $attachments"
attachment_count=$(plutil -convert raw - -o - <<<"$attachments")
# echo "ac: $attachment_count"
for (( ai=0; ai<$attachment_count; ai++ )); do
filename=$(plutil -extract ${ai}.filename._value raw - -o - <<<"$attachments")
name=$(plutil -extract ${ai}.name._value raw - -o - <<<"$attachments")
ref=$(plutil -extract ${ai}.payloadRef.id._value raw - -o - <<<"$attachments")
echo "Found attachment named: ${name}"
echo "${ref} -> ${filename}"
xcrun xcresulttool get --path ${XCRESULT} --id "$ref" >$filename
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment