|
| 1 | +#!/bin/zsh |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +fail() { |
| 6 | + print -u2 -- "xcode-hygiene: $1" |
| 7 | + exit 1 |
| 8 | +} |
| 9 | + |
| 10 | +usage() { |
| 11 | + cat <<'EOF' |
| 12 | +Usage: ./xcode-hygiene.sh [options] |
| 13 | +
|
| 14 | +Preview is the default. Nothing is deleted unless --apply is present. |
| 15 | +
|
| 16 | +Options: |
| 17 | + --apply Perform the selected cleanup actions. |
| 18 | + --clean-derived-data Delete contents of Xcode DerivedData. |
| 19 | + --delete-unavailable-simulators Delete simulator devices unavailable to Xcode. |
| 20 | + --prune-simulators Delete available simulators except keep-names. |
| 21 | + --keep-simulator NAME Preserve every simulator with this exact name. |
| 22 | + Repeat for more than one name. |
| 23 | + -h, --help Show this help. |
| 24 | +EOF |
| 25 | +} |
| 26 | + |
| 27 | +apply_changes=false |
| 28 | +clean_derived_data=false |
| 29 | +delete_unavailable=false |
| 30 | +prune_simulators=false |
| 31 | +typeset -a keep_simulator_names=() |
| 32 | + |
| 33 | +while (( $# > 0 )); do |
| 34 | + case "$1" in |
| 35 | + --apply) |
| 36 | + apply_changes=true |
| 37 | + shift |
| 38 | + ;; |
| 39 | + --clean-derived-data) |
| 40 | + clean_derived_data=true |
| 41 | + shift |
| 42 | + ;; |
| 43 | + --delete-unavailable-simulators) |
| 44 | + delete_unavailable=true |
| 45 | + shift |
| 46 | + ;; |
| 47 | + --prune-simulators) |
| 48 | + prune_simulators=true |
| 49 | + shift |
| 50 | + ;; |
| 51 | + --keep-simulator) |
| 52 | + (( $# >= 2 )) || fail "--keep-simulator requires an exact device name" |
| 53 | + [[ -n "$2" ]] || fail "simulator keep-name cannot be empty" |
| 54 | + keep_simulator_names+=("$2") |
| 55 | + shift 2 |
| 56 | + ;; |
| 57 | + -h|--help) |
| 58 | + usage |
| 59 | + exit 0 |
| 60 | + ;; |
| 61 | + *) |
| 62 | + fail "unknown option: $1" |
| 63 | + ;; |
| 64 | + esac |
| 65 | +done |
| 66 | + |
| 67 | +if $prune_simulators && (( ${#keep_simulator_names[@]} == 0 )); then |
| 68 | + fail "--prune-simulators requires at least one --keep-simulator" |
| 69 | +fi |
| 70 | + |
| 71 | +if $apply_changes \ |
| 72 | + && ! $clean_derived_data \ |
| 73 | + && ! $delete_unavailable \ |
| 74 | + && ! $prune_simulators; then |
| 75 | + fail "--apply requires at least one cleanup category" |
| 76 | +fi |
| 77 | + |
| 78 | +current_user="$(/usr/bin/id -un)" |
| 79 | +home_record="$(/usr/bin/dscl . -read "/Users/$current_user" NFSHomeDirectory 2>/dev/null)" \ |
| 80 | + || fail "could not resolve the current user's home directory" |
| 81 | +user_home="${home_record#* }" |
| 82 | + |
| 83 | +derived_data_root="$user_home/Library/Developer/Xcode/DerivedData" |
| 84 | +device_support_root="$user_home/Library/Developer/Xcode/iOS DeviceSupport" |
| 85 | +simulator_root="$user_home/Library/Developer/CoreSimulator" |
| 86 | + |
| 87 | +[[ "$user_home" == /Users/* && -d "$user_home" ]] \ |
| 88 | + || fail "resolved an unsafe home directory" |
| 89 | +[[ "$derived_data_root" == "$user_home/Library/Developer/Xcode/DerivedData" ]] \ |
| 90 | + || fail "resolved an unsafe DerivedData path" |
| 91 | +[[ "$simulator_root" == "$user_home/Library/Developer/CoreSimulator" ]] \ |
| 92 | + || fail "resolved an unsafe simulator path" |
| 93 | + |
| 94 | +size_or_missing() { |
| 95 | + local label="$1" |
| 96 | + local target="$2" |
| 97 | + if [[ -e "$target" ]]; then |
| 98 | + print -- "$label: $(/usr/bin/du -sh "$target" 2>/dev/null | /usr/bin/awk '{print $1}')" |
| 99 | + else |
| 100 | + print -- "$label: missing" |
| 101 | + fi |
| 102 | +} |
| 103 | + |
| 104 | +print "Mode: $($apply_changes && print apply || print preview)" |
| 105 | +size_or_missing "DerivedData" "$derived_data_root" |
| 106 | +size_or_missing "iOS DeviceSupport" "$device_support_root" |
| 107 | +size_or_missing "CoreSimulator" "$simulator_root" |
| 108 | +print "Disk: $(/bin/df -h / | /usr/bin/tail -n 1 | /usr/bin/awk '{print $4 " available"}')" |
| 109 | +print |
| 110 | +/usr/bin/xcrun simctl list devices |
| 111 | + |
| 112 | +temporary_root="" |
| 113 | +cleanup_temporary_root() { |
| 114 | + if [[ -n "$temporary_root" \ |
| 115 | + && -d "$temporary_root" \ |
| 116 | + && "${temporary_root:t}" == xcode-hygiene.* ]]; then |
| 117 | + /usr/bin/find "$temporary_root" -depth -delete |
| 118 | + fi |
| 119 | +} |
| 120 | +trap cleanup_temporary_root EXIT |
| 121 | + |
| 122 | +typeset -a prune_candidates=() |
| 123 | +if $prune_simulators; then |
| 124 | + temporary_root="$(/usr/bin/mktemp -d "${TMPDIR:-/tmp}/xcode-hygiene.XXXXXX")" |
| 125 | + simulator_json="$temporary_root/simulators.json" |
| 126 | + candidate_file="$temporary_root/candidates.tsv" |
| 127 | + /usr/bin/xcrun simctl list devices available --json > "$simulator_json" |
| 128 | + |
| 129 | + /usr/bin/python3 - "$simulator_json" "$candidate_file" "${keep_simulator_names[@]}" <<'PY' |
| 130 | +import json |
| 131 | +import sys |
| 132 | +
|
| 133 | +source, destination, *keep_names = sys.argv[1:] |
| 134 | +keep = set(keep_names) |
| 135 | +found = {name: False for name in keep} |
| 136 | +rows = [] |
| 137 | +
|
| 138 | +with open(source, encoding="utf-8") as handle: |
| 139 | + devices_by_runtime = json.load(handle)["devices"] |
| 140 | +
|
| 141 | +for runtime, devices in devices_by_runtime.items(): |
| 142 | + for device in devices: |
| 143 | + name = device["name"] |
| 144 | + if name in keep: |
| 145 | + found[name] = True |
| 146 | + else: |
| 147 | + rows.append((device["udid"], name, runtime)) |
| 148 | +
|
| 149 | +missing = sorted(name for name, present in found.items() if not present) |
| 150 | +if missing: |
| 151 | + print("missing keep-name(s): " + ", ".join(missing), file=sys.stderr) |
| 152 | + sys.exit(2) |
| 153 | +
|
| 154 | +with open(destination, "w", encoding="utf-8") as handle: |
| 155 | + for row in rows: |
| 156 | + handle.write("\t".join(row) + "\n") |
| 157 | +PY |
| 158 | + |
| 159 | + while IFS=$'\t' read -r simulator_id simulator_name simulator_runtime; do |
| 160 | + [[ -n "$simulator_id" ]] || continue |
| 161 | + prune_candidates+=("$simulator_id") |
| 162 | + print -- "Prune candidate: $simulator_name [$simulator_id] ($simulator_runtime)" |
| 163 | + done < "$candidate_file" |
| 164 | +fi |
| 165 | + |
| 166 | +if ! $apply_changes; then |
| 167 | + print |
| 168 | + print "Preview complete. Re-run with --apply to perform the selected actions." |
| 169 | + exit 0 |
| 170 | +fi |
| 171 | + |
| 172 | +if /usr/bin/pgrep -x xcodebuild >/dev/null 2>&1 \ |
| 173 | + || /usr/bin/pgrep -f 'xctest|UITests-Runner|XCTRunner' >/dev/null 2>&1; then |
| 174 | + fail "an Xcode build or test runner is active" |
| 175 | +fi |
| 176 | + |
| 177 | +if $prune_simulators || $delete_unavailable; then |
| 178 | + /usr/bin/xcrun simctl shutdown all |
| 179 | +fi |
| 180 | + |
| 181 | +if $prune_simulators; then |
| 182 | + for simulator_id in "${prune_candidates[@]}"; do |
| 183 | + /usr/bin/xcrun simctl delete "$simulator_id" |
| 184 | + done |
| 185 | +fi |
| 186 | + |
| 187 | +if $delete_unavailable; then |
| 188 | + /usr/bin/xcrun simctl delete unavailable |
| 189 | +fi |
| 190 | + |
| 191 | +if $clean_derived_data && [[ -d "$derived_data_root" ]]; then |
| 192 | + /usr/bin/find "$derived_data_root" -mindepth 1 -depth -delete |
| 193 | +fi |
| 194 | + |
| 195 | +print |
| 196 | +print "Cleanup complete." |
| 197 | +size_or_missing "DerivedData" "$derived_data_root" |
| 198 | +size_or_missing "iOS DeviceSupport" "$device_support_root" |
| 199 | +size_or_missing "CoreSimulator" "$simulator_root" |
| 200 | +print "Disk: $(/bin/df -h / | /usr/bin/tail -n 1 | /usr/bin/awk '{print $4 " available"}')" |
0 commit comments