#!/bin/bash set -euo pipefail cd "$(dirname "$0")/.." if [[ ${PARCEL_REALITY:-0} != 1 ]]; then printf 'Skipped reality: set PARCEL_REALITY=1 or run mise run reality. No apps launched.\n' exit 0 fi no_build=0 arguments=() for argument in "$@"; do if [[ $argument == --no-build ]]; then no_build=1; else arguments+=("$argument"); fi done export PARCEL_BUNDLE_BUILD=0 for path in build build/reality-build-stamps; do [[ ! -L $path ]] || { printf 'Refusing a symlinked reality stamp path: %s\n' "$path" >&2; exit 1; } done if (( no_build )) && [[ ! -d build/Parcel.app ]]; then printf 'Reality --no-build: build/Parcel.app is missing. Run mise run reality without --no-build to build it. No apps launched.\n' >&2 exit 1 fi mkdir -p build/reality-build-stamps work=$(mktemp -d "$PWD/build/.reality-stamps.XXXXXX") trap 'rm -rf "$work"' EXIT # Hash contents and names, not timestamps; include link destinations in signed bundles. content_digest() { local path link destination digest : > "$work/files" : > "$work/records" for path in "$@"; do if [[ -d $path && ! -L $path ]]; then case "$path" in Sources/*|Tests/*|Plugins) find "$path" -type f ! -name '*.md' -print0 >> "$work/files" || return ;; *) find "$path" -type f -print0 >> "$work/files" || return ;; esac find "$path" -type d -print >> "$work/records" || return find "$path" -type l -print0 > "$work/links" || return while IFS= read -r -d '' link; do destination=$(readlink "$link") || return printf '%s -> %s\n' "$link" "$destination" >> "$work/records" done < "$work/links" elif [[ -f $path ]]; then printf '%s\0' "$path" >> "$work/files" elif [[ -e $path || -L $path ]]; then printf 'Unsupported reality build input: %s\n' "$path" >&2 return 1 else printf 'missing %s\n' "$path" >> "$work/records" fi done if [[ -s $work/files ]]; then xargs -0 shasum -a 256 < "$work/files" >> "$work/records" || return; fi LC_ALL=C sort "$work/records" > "$work/sorted" || return digest=$(shasum -a 256 "$work/sorted") || return printf '%s\n' "${digest%% *}" } { printf 'parcel-reality-build-v1\n' command -v swift swift --version xcrun --find swift xcrun --show-sdk-path xcodebuild -version printf '%s\n' "${DEVELOPER_DIR:-}" "${TOOLCHAINS:-}" "${SWIFT_EXEC:-}" "${SDKROOT:-}" } > "$work/common" common=$(shasum -a 256 "$work/common") common=${common%% *} bin_dir=$(swift build --show-bin-path) platform=(Sources/ParcelCore Sources/ParcelPlatform Sources/CPrivateAPI) localized=(Sources/ParcelConfig Sources/ParcelIPC Sources/ParcelEngine Sources/ParcelAnimation Sources/ParcelUI Sources/ParcelDev Sources/parcel-dev Plugins) signing_inputs() { local stage=$1 identities line local matches=() records=() identities=$(security find-identity -p codesigning "$HOME/Library/Keychains/login.keychain-db") || return while IFS= read -r line; do if [[ $line =~ ([[:xdigit:]]{40})[[:space:]]+\"Parcel\ Development\" ]]; then matches+=("${BASH_REMATCH[1]}") records+=("${line#*"${BASH_REMATCH[1]}"}") fi done <<< "$identities" if (( ${#matches[@]} != 1 )); then printf 'Reality requires one Parcel Development signing identity; cannot stamp or reuse %s. No apps launched.\n' "$stage" >&2 return 1 fi printf '%s\n%s\n' "${matches[0]}" "${records[0]}" # Explicit identities can be names or fingerprints; retain their resolution context. if [[ $stage == bundle && -n ${PARCEL_SIGN_IDENTITY:-} ]]; then printf '%s\n' "$identities"; fi } input_digest() { local stage=$1 digest shift { printf '%s\n%s\n' "$common" "$stage" content_digest Package.swift Package.resolved Scripts/test-reality.sh Scripts/swift-build.sh "$@" || return if [[ $stage == driver || $stage == bundle ]]; then signing_inputs "$stage" || return; fi if [[ $stage == bundle ]]; then printf '%s\n' "${PARCEL_BUNDLE_ID:-}" "${PARCEL_VERSION-0.1.0}" "${PARCEL_BUILD_NUMBER-1}" \ "${PARCEL_SIGN_IDENTITY:-}" "${PARCEL_DEV_BINARY:-}" "${PARCEL_SPARKLE_FRAMEWORK:-}" content_digest build/reality-helper/ParcelRealityDriver || return if [[ -n ${PARCEL_DEV_BINARY:-} ]]; then content_digest "$PARCEL_DEV_BINARY" || return; fi if [[ -n ${PARCEL_SPARKLE_FRAMEWORK:-} ]]; then content_digest "$PARCEL_SPARKLE_FRAMEWORK" || return; fi fi } > "$work/input" digest=$(shasum -a 256 "$work/input") || return printf '%s\n' "${digest%% *}" } ensure_stage() { local stage=$1 executable=$2 reason='' inputs outputs stamp output_digest resource shift 2 inputs=$(input_digest "$stage" "$@") stamp="build/reality-build-stamps/$stage" case "$stage" in bundle) outputs=(build/Parcel.app) ;; harness) outputs=("$executable") if [[ -d $bin_dir ]]; then find "$bin_dir" -maxdepth 1 -type d -name '*.bundle' -print0 > "$work/resources" while IFS= read -r -d '' resource; do outputs+=("$resource"); done < "$work/resources" fi ;; *) outputs=("$executable") ;; esac if [[ ! -x $executable || -L $executable ]]; then reason="missing or inappropriate $executable" elif [[ $stage == bundle && ( ! -x build/Parcel.app/Contents/Helpers/ParcelRealityDriver || ! -x build/Parcel.app/Contents/Resources/bin/prcl || -L build/Parcel.app ) ]]; then reason='bundle is not reality-enabled debug (helper or CLI missing)' elif [[ ! -f $stamp || -L $stamp ]]; then reason='no trusted content stamp' else output_digest=$(content_digest "${outputs[@]}") printf '%s\n%s\n' "$inputs" "$output_digest" > "$work/expected" if ! cmp -s "$work/expected" "$stamp"; then reason='stale inputs or changed output contents'; fi fi if [[ -z $reason ]]; then printf 'Reality: reusing %s (content stamp unchanged).\n' "$stage" return fi if (( no_build )); then printf 'Reality --no-build: cannot reuse %s: %s. Run mise run reality without --no-build to rebuild. No apps launched.\n' "$stage" "$reason" >&2 exit 1 fi printf 'Reality: rebuilding %s (%s).\n' "$stage" "$reason" rm -f "$stamp" case "$stage" in driver) Scripts/build-reality-driver.sh ;; bundle) Scripts/bundle.sh debug --reality ;; harness) Scripts/swift-build.sh --product ParcelRealityTests ;; fixture) Scripts/swift-build.sh --product FixtureApp ;; esac [[ -x $executable && ! -L $executable ]] || { printf 'Reality build did not produce %s.\n' "$executable" >&2; exit 1; } if [[ $stage == bundle && ( ! -x build/Parcel.app/Contents/Helpers/ParcelRealityDriver || ! -x build/Parcel.app/Contents/Resources/bin/prcl || -L build/Parcel.app ) ]]; then printf 'Reality build did not produce an appropriate bundle with an embedded driver and CLI.\n' >&2 exit 1 fi output_digest=$(input_digest "$stage" "$@") if [[ $inputs != "$output_digest" ]]; then printf 'Reality %s inputs changed during the build; rerun to establish a content stamp. No apps launched.\n' "$stage" >&2 exit 1 fi # Resource bundles can first appear during the harness build. if [[ $stage == harness ]]; then outputs=("$executable") find "$bin_dir" -maxdepth 1 -type d -name '*.bundle' -print0 > "$work/resources" while IFS= read -r -d '' resource; do outputs+=("$resource"); done < "$work/resources" fi output_digest=$(content_digest "${outputs[@]}") printf '%s\n%s\n' "$inputs" "$output_digest" > "$work/stamp" mv "$work/stamp" "$stamp" } ensure_stage driver build/reality-helper/ParcelRealityDriver "${platform[@]}" \ Tests/RealityDriver Tests/RealityDriverSupport Scripts/build-reality-driver.sh Scripts/designated-requirement.sh ensure_stage bundle build/Parcel.app/Contents/MacOS/Parcel "${platform[@]}" "${localized[@]}" \ Sources/ParcelApp Sources/prcl Sources/ParcelUpdate Resources Packaging/Sparkle-LICENSE \ Scripts/bundle.sh Scripts/product.env Scripts/version.sh Scripts/packaging-paths.sh \ Scripts/check-app-icons.sh Scripts/parcel-dev.sh Scripts/check-localization-pseudo.sh \ Scripts/designated-requirement.sh Scripts/bundle-preflight.sh ensure_stage harness "$bin_dir/ParcelRealityTests" "${platform[@]}" "${localized[@]}" \ Tests/Reality Tests/RealityDriverSupport ensure_stage fixture "$bin_dir/FixtureApp" Sources/FixtureApp export PARCEL_APP_PATH="$PWD/build/Parcel.app" export PARCEL_FIXTURE_PATH="$bin_dir/FixtureApp" export PARCEL_SCREEN_OWNER=reality rm -rf "$work" trap - EXIT exec caffeinate -dimsu Scripts/screen-lock.sh "$bin_dir/ParcelRealityTests" ${arguments[@]+"${arguments[@]}"}