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