SPB Git

Repository Public License

shot.sh 5.5 KB 119 lines shellscript

1#!/usr/bin/env bash2# ORIX Navigator — real screenshots, without ever activating the app or touching the mouse or keyboard.3#4#   scripts/shot.sh mac <name> [-w seconds] [-k] -- <launch arguments…>5#       Launches a separate background instance of the Debug app (open -g -n) on a throwaway data directory,6#       captures its window by window id (screencapture -l) and terminates that instance by PID.7#       -k keeps the data directory (printed) so that a later run can reuse it with -d <dir>.8#   scripts/shot.sh ios <iphone|ipad> <name> [-w seconds] [-o portrait|landscape] -- <launch arguments…>9#       Installs and launches the Debug app on the project simulator (which must be booted) and captures it.10#11# "@DATA@" in an argument is replaced by the data directory, which contains a copy of the fixture12# bookmarks.html (-ORIXImportBookmarks @DATA@/bookmarks.html).13# Images go to docs/screenshots/0.2/<name>.png.14set -euo pipefail1516ROOT="$(cd "$(dirname "$0")/.." && pwd)"17OUT="${ORIX_SHOT_DIR:-$ROOT/docs/screenshots/0.2}"18TOOLS="$ROOT/build/tools"19BUNDLE_ID="ai.spboucher.orix.navigator"20mkdir -p "$OUT" "$TOOLS"2122window_tool() {23  if [ ! -x "$TOOLS/windowid" ]; then24    cat > "$TOOLS/windowid.swift" <<'SWIFT'25import CoreGraphics26import Foundation27// Prints the id of the largest on-screen or off-screen window owned by the given process.28let pid = Int32(CommandLine.arguments[1]) ?? 029let list = (CGWindowListCopyWindowInfo([.optionAll], kCGNullWindowID) as? [[String: Any]]) ?? []30var best: (id: Int, area: Double)?31for window in list where (window[kCGWindowOwnerPID as String] as? Int32) == pid {32    guard (window[kCGWindowLayer as String] as? Int) == 0,33          let bounds = window[kCGWindowBounds as String] as? [String: Double],34          let id = window[kCGWindowNumber as String] as? Int else { continue }35    let area = (bounds["Width"] ?? 0) * (bounds["Height"] ?? 0)36    if area > 40_000, area > (best?.area ?? 0) { best = (id, area) }37}38if let best { print(best.id) } else { exit(1) }39SWIFT40    swiftc -O -o "$TOOLS/windowid" "$TOOLS/windowid.swift"41  fi42}4344platform="${1:-}"; shift || true45case "$platform" in46  mac)47    name="$1"; shift48    wait=6; keep=0; data=""49    while [ "${1:-}" != "--" ] && [ $# -gt 0 ]; do50      case "$1" in51        -w) wait="$2"; shift 2 ;;52        -k) keep=1; shift ;;53        -d) data="$2"; keep=1; shift 2 ;;54        *) echo "unknown option $1" >&2; exit 2 ;;55      esac56    done57    [ "${1:-}" = "--" ] && shift58    window_tool59    app="$ROOT/build/dd-${ORIX_BUILD_NAME:-shell}/Build/Products/Debug/ORIX Navigator.app"60    # The app is sandboxed: the throwaway data directory lives in its container's tmp, far from the real data.61    sandbox_tmp="$HOME/Library/Containers/$BUNDLE_ID/Data/tmp"62    mkdir -p "$sandbox_tmp"63    [ -n "$data" ] || data="$(mktemp -d "$sandbox_tmp/orix-shot.XXXXXX")"64    cp "$ROOT/Apps/UITests/Fixtures/bookmarks.html" "$data/bookmarks.html" 2>/dev/null || true65    args=(); for a in "$@"; do args+=("${a//@DATA@/$data}"); done; set -- ${args[@]+"${args[@]}"}66    open -g -n "$app" --args -ORIXDataDir "$data" -ORIXSkipOnboarding YES -ApplePersistenceIgnoreState YES "$@"67    pid=""68    for _ in $(seq 1 50); do69      pid="$(pgrep -f -- "-ORIXDataDir $data" | head -1 || true)"70      [ -n "$pid" ] && break71      sleep 0.272    done73    [ -n "$pid" ] || { echo "the app did not start" >&2; exit 1; }74    sleep "$wait"75    wid="$("$TOOLS/windowid" "$pid" || true)"76    status=077    if [ -n "$wid" ]; then screencapture -l "$wid" -x -o "$OUT/$name.png" || status=1; else echo "no window found" >&2; status=1; fi78    # SIGTERM makes the app quit normally (session saved); wait for it so the data directory is quiescent.79    kill "$pid" 2>/dev/null || true80    for _ in $(seq 1 40); do kill -0 "$pid" 2>/dev/null || break; sleep 0.1; done81    kill -0 "$pid" 2>/dev/null && kill -9 "$pid" 2>/dev/null || true82    if [ "$keep" = 1 ]; then83      echo "data: $data"84    else85      # Remove the WebKit store of the throwaway profile along with the directory.86      for store in $(sqlite3 "$data/Navigator.sqlite" "SELECT data_store_identifier FROM profiles" 2>/dev/null || true); do87        case "$store" in *[!0-9A-Fa-f-]*|"") ;; *) rm -rf "$HOME/Library/Containers/$BUNDLE_ID/Data/Library/WebKit/WebsiteDataStore/$store" ;; esac88      done89      rm -rf "$data"90    fi91    [ "$status" = 0 ] && echo "$OUT/$name.png"92    exit "$status" ;;93  ios)94    device="$1"; name="$2"; shift 295    wait=7; orientation=""96    while [ "${1:-}" != "--" ] && [ $# -gt 0 ]; do97      case "$1" in98        -w) wait="$2"; shift 2 ;;99        -o) orientation="$2"; shift 2 ;;100        *) echo "unknown option $1" >&2; exit 2 ;;101      esac102    done103    [ "${1:-}" = "--" ] && shift104    udid="$("$ROOT/scripts/build.sh" sim "$device")"105    app="$ROOT/build/dd-${ORIX_BUILD_NAME:-shell}/Build/Products/Debug-iphonesimulator/ORIX Navigator.app"106    xcrun simctl install "$udid" "$app"107    container="$(xcrun simctl get_app_container "$udid" "$BUNDLE_ID" data)"108    data="$container/tmp/shot-$name"109    rm -rf "$data"; mkdir -p "$data"110    cp "$ROOT/Apps/UITests/Fixtures/bookmarks.html" "$data/bookmarks.html"111    args=(); for a in "$@"; do args+=("${a//@DATA@/$data}"); done; set -- ${args[@]+"${args[@]}"}112    xcrun simctl launch --terminate-running-process "$udid" "$BUNDLE_ID" -ORIXDataDir "$data" -ORIXSkipOnboarding YES "$@" >/dev/null113    sleep "$wait"114    xcrun simctl io "$udid" screenshot "$OUT/$name.png" >/dev/null 2>&1115    xcrun simctl terminate "$udid" "$BUNDLE_ID" >/dev/null 2>&1 || true116    echo "$OUT/$name.png" ;;117  *) sed -n '2,13p' "$0"; exit 2 ;;118esac119
Files main
Menu

Theme