#!/bin/bash # shellcheck disable=SC2016 set -euo pipefail cd "$(dirname "$0")/../.." work=$(mktemp -d "${TMPDIR:-/tmp}/parcel-reality-driver.XXXXXX") work=$(CDPATH='' cd "$work" && pwd -P) trap 'rm -rf "$work"' EXIT mkdir -p "$work/Scripts" "$work/bin" "$work/executable" cp Scripts/build-reality-driver.sh "$work/Scripts/" printf '#!/bin/sh\nset -eu\nexec swift build "$@"\n' > "$work/Scripts/swift-build.sh" printf 'binary\n' > "$work/executable/ParcelRealityDriver" printf '#!/bin/bash\nprintf "%%s\\n" "${IDENTITIES:-}"\n' > "$work/bin/security" cat > "$work/bin/swift" <<'MOCK' #!/bin/bash set -euo pipefail printf 'build %s BUNDLE=%s\n' "$*" "${PARCEL_BUNDLE_BUILD:-}" >> "$EVENTS" [[ $1 == build ]] shift scratch="$PWD/.build" system=native configuration=debug reality=0 product='' show=0 while (( $# )); do case $1 in --scratch-path) scratch=$2; shift 2 ;; --build-system) system=$2; shift 2 ;; -c) configuration=$2; shift 2 ;; --product) product=$2; shift 2 ;; --show-bin-path) show=1; shift ;; -DPARCEL_REALITY) reality=1; shift ;; *) shift ;; esac done variant="$system:${PARCEL_BUNDLE_BUILD:-0}:$reality" bin_dir="$scratch/mock-products/$configuration" mkdir -p "$bin_dir/Parcel_ParcelUI.bundle/en.lproj" if [[ -f $scratch/variant && $(< "$scratch/variant") != "$variant" ]]; then printf 'Mixed build variants in one resource-copy cache.\n' >&2 exit 31 fi printf '%s\n' "$variant" > "$scratch/variant" printf '%s\n' "$variant" > "$bin_dir/Parcel_ParcelUI.bundle/en.lproj/Localizable.strings" if (( show )); then printf '%s\n' "$bin_dir" elif [[ $product == ParcelRealityDriver ]]; then cp "$EXECUTABLE_DIR/ParcelRealityDriver" "$bin_dir/$product" else printf '%s:%s\n' "$variant" "$configuration" > "$bin_dir/$product" fi MOCK printf '#!/bin/bash\nprintf "sign %%s\\n" "$*" >> "$EVENTS"\nif [[ $1 == --verify ]]; then exit "${VERIFY_STATUS:-0}"; fi\nexit "${SIGN_STATUS:-0}"\n' > "$work/bin/codesign" printf '#!/bin/bash\nset -euo pipefail\n[[ $1 == build/reality-helper/ParcelRealityDriver && -f $1 && -f $2 ]]\nprintf "requirement\\n" >> "$EVENTS"\nexit "${REQUIREMENT_STATUS:-0}"\n' > "$work/Scripts/designated-requirement.sh" chmod +x "$work/Scripts/"*.sh "$work/bin/"* "$work/executable/ParcelRealityDriver" export PATH="$work/bin:$PATH" EVENTS="$work/events" EXECUTABLE_DIR="$work/executable" status=0 bash "$work/Scripts/build-reality-driver.sh" > "$work/no-identity.log" 2>&1 || status=$? test "$status" = 1 grep -q 'requires one Parcel Development identity' "$work/no-identity.log" test ! -e "$EVENTS" export IDENTITIES='1) 0123456789012345678901234567890123456789 "Parcel Development"' PARCEL_BUNDLE_BUILD=1 bash "$work/Scripts/build-reality-driver.sh" > "$work/build.log" grep -Fq "build build --scratch-path $work/.build/reality-driver --build-system native -c debug --product ParcelRealityDriver BUNDLE=0" "$EVENTS" grep -Fq "build build --scratch-path $work/.build/reality-driver --build-system native -c debug --show-bin-path BUNDLE=0" "$EVENTS" grep -q -- '--options runtime --timestamp=none' "$EVENTS" grep -q 'certificate leaf = H"0123456789012345678901234567890123456789"' "$EVENTS" grep -q -- '--identifier dev.vinnymac.parcel.reality-driver' "$EVENTS" grep -q -- '--verify --strict' "$EVENTS" helper="$work/build/reality-helper/ParcelRealityDriver" test -f "$helper" test -x "$helper" test ! -e "$work/build/ParcelRealityDriver.app" cmp "$work/executable/ParcelRealityDriver" "$helper" printf 'rebuilt binary\n' > "$work/executable/ParcelRealityDriver" bash "$work/Scripts/build-reality-driver.sh" > "$work/rebuild.log" grep -q 'requirement' "$EVENTS" cmp "$work/executable/ParcelRealityDriver" "$helper" cp "$helper" "$work/before-failure" printf 'unpublished binary\n' > "$work/executable/ParcelRealityDriver" for failure in REQUIREMENT_STATUS=17 SIGN_STATUS=18 VERIFY_STATUS=19; do status=0 env "$failure" bash "$work/Scripts/build-reality-driver.sh" > "$work/changed.log" 2>&1 || status=$? test "$status" = "${failure#*=}" cmp "$helper" "$work/before-failure" done cp "$EVENTS" "$work/before-unsafe-output" for path in build build/reality-helper build/reality-helper/ParcelRealityDriver .build .build/reality-driver; do mv "$work/$path" "$work/held-output" ln -s "$work/held-output" "$work/$path" status=0 bash "$work/Scripts/build-reality-driver.sh" > "$work/symlink.log" 2>&1 || status=$? test "$status" = 1 grep -q 'Refusing a symlinked reality output' "$work/symlink.log" cmp "$EVENTS" "$work/before-unsafe-output" rm "$work/$path" mv "$work/held-output" "$work/$path" done mv "$helper" "$work/held-output" mkdir "$helper" status=0 bash "$work/Scripts/build-reality-driver.sh" > "$work/directory.log" 2>&1 || status=$? test "$status" = 1 grep -q 'must be a regular executable' "$work/directory.log" rmdir "$helper" mv "$work/held-output" "$helper" export IDENTITIES="$IDENTITIES 2) 1123456789012345678901234567890123456789 \"Parcel Development\"" status=0 bash "$work/Scripts/build-reality-driver.sh" > "$work/ambiguous.log" 2>&1 || status=$? test "$status" = 1 # Exercise bundle build/path selection without compiling, signing, or launching an app. cp Scripts/bundle.sh Scripts/product.env Scripts/version.sh Scripts/packaging-paths.sh Scripts/check-app-icons.sh "$work/Scripts/" mkdir -p "$work/Packaging/AlternateIcons" "$work/Sources/ParcelUI/Resources" cp Packaging/AlternateIcons/IconTool.swift "$work/Packaging/AlternateIcons/" ditto Sources/ParcelUI/Resources/AppIcons "$work/Sources/ParcelUI/Resources/AppIcons" mkdir -p "$work/Resources" "$work/Packaging" "$work/.build/apple/Products/Debug/Parcel_ParcelUI.bundle/en.lproj" cp Resources/Info.plist "$work/Resources/" cp Packaging/Sparkle-LICENSE "$work/Packaging/" printf 'existing default-cache localization\n' > "$work/.build/apple/Products/Debug/Parcel_ParcelUI.bundle/en.lproj/marker" cp "$work/.build/apple/Products/Debug/Parcel_ParcelUI.bundle/en.lproj/marker" "$work/default-marker" cat > "$work/Scripts/parcel-dev.sh" <<'MOCK' #!/bin/bash set -euo pipefail case $1 in --path) printf '%s\n' "$PWD/Scripts/parcel-dev.sh" ;; update-metadata) [[ $2 == configure && -f $3 && $4 == "${EXPECTED_VARIANT##*:}" ]] contents=${3%/Info.plist} [[ $(< "$contents/MacOS/Parcel") == "$EXPECTED_VARIANT" ]] [[ $(< "$contents/Resources/bin/prcl") == "$EXPECTED_VARIANT" ]] if [[ $EXPECTED_VARIANT == swiftbuild:1:1:* ]]; then cmp build/reality-helper/ParcelRealityDriver "$contents/Helpers/ParcelRealityDriver" else [[ ! -e $contents/Helpers ]] fi printf 'staged %s\n' "$EXPECTED_VARIANT" >> "$EVENTS" ;; *) exit 32 ;; esac MOCK printf '#!/bin/bash\n[[ $# == 4 && $2 == -verify_arch && $3 == arm64 && $4 == x86_64 ]]\n' > "$work/bin/lipo" chmod +x "$work/Scripts/parcel-dev.sh" "$work/bin/lipo" unset PARCEL_DEV_BINARY PARCEL_BUNDLE_ID PARCEL_VERSION PARCEL_BUILD_NUMBER PARCEL_SPARKLE_FRAMEWORK for mode in normal reality normal inspectable release; do configuration=debug lane=normal expected=swiftbuild:1:0:debug args=(debug) case $mode in reality) lane=reality; expected=swiftbuild:1:1:debug; args+=(--reality) ;; inspectable) args+=(--inspectable) ;; release) configuration=release; expected=swiftbuild:1:0:release; args=(release) ;; esac cache="$work/.build/bundle/$lane" : > "$EVENTS" status=0 EXPECTED_VARIANT="$expected" PARCEL_SIGN_IDENTITY=0123456789012345678901234567890123456789 \ bash "$work/Scripts/bundle.sh" "${args[@]}" > "$work/bundle.log" 2>&1 || status=$? test "$status" = 1 # Deliberately stop before framework assembly; all three Swift calls must agree. grep -Fxq "Sparkle framework not found: $cache/artifacts/sparkle/Sparkle/Sparkle.xcframework/macos-arm64_x86_64/Sparkle.framework" "$work/bundle.log" grep -F "build build --scratch-path $cache --build-system swiftbuild -c $configuration " "$EVENTS" > "$work/build-events" test "$(wc -l < "$work/build-events" | tr -d ' ')" = 3 for product in ParcelApp prcl; do grep -Fq -- "--product $product BUNDLE=1" "$work/build-events" done grep -Fq -- '--show-bin-path BUNDLE=1' "$work/build-events" if [[ $lane == reality ]]; then test "$(grep -c -- '-DPARCEL_REALITY' "$work/build-events")" = 3 else if grep -q -- '-DPARCEL_REALITY' "$work/build-events"; then exit 1; fi fi grep -Fxq "staged $expected" "$EVENTS" cmp "$work/.build/apple/Products/Debug/Parcel_ParcelUI.bundle/en.lproj/marker" "$work/default-marker" test -z "$(find "$work/build" -maxdepth 1 -name '.bundle.*' -print)" done : > "$EVENTS" status=0 EXPECTED_VARIANT=swiftbuild:1:0:debug PARCEL_SPARKLE_FRAMEWORK="$work/override.framework" \ PARCEL_SIGN_IDENTITY=0123456789012345678901234567890123456789 \ bash "$work/Scripts/bundle.sh" > "$work/override.log" 2>&1 || status=$? test "$status" = 1 grep -Fxq "Sparkle framework not found: $work/override.framework" "$work/override.log" for path in .build .build/bundle .build/bundle/normal .build/bundle/reality; do args=(debug) if [[ $path == .build/bundle/reality ]]; then args+=(--reality); fi mv "$work/$path" "$work/held-cache" ln -s "$work/held-cache" "$work/$path" : > "$EVENTS" status=0 PARCEL_SIGN_IDENTITY=0123456789012345678901234567890123456789 \ bash "$work/Scripts/bundle.sh" "${args[@]}" > "$work/cache-symlink.log" 2>&1 || status=$? test "$status" = 1 grep -q 'Refusing a symlinked bundle cache' "$work/cache-symlink.log" test ! -s "$EVENTS" rm "$work/$path" mv "$work/held-cache" "$work/$path" done printf 'Reality build checks passed: signed helper, protected rebuild, isolated native/normal/reality caches, variant return, matching build/path calls.\n'