-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathxcode_manager.py
More file actions
333 lines (273 loc) Β· 11.3 KB
/
xcode_manager.py
File metadata and controls
333 lines (273 loc) Β· 11.3 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
# /// script
# dependencies = [
# "typer",
# "packaging",
# ]
# ///
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
from typing import NamedTuple, Optional, List
import plistlib
import typer
from packaging import version
class XcodeVersion(NamedTuple):
version: str
build: str
path: Path
is_beta: bool
def find_installed_xcode_apps() -> List[XcodeVersion]:
"""Find all Xcode installations in /Applications."""
apps_dir = Path("/Applications")
if not apps_dir.exists():
return []
xcode_apps = []
seen_targets = set() # Track resolved paths to avoid duplicates
xcode_pattern = re.compile(r"^Xcode.*\.app$", re.IGNORECASE)
for app_path in apps_dir.iterdir():
if not app_path.is_dir() or not xcode_pattern.match(app_path.name):
continue
# Resolve symlinks to get the actual target
resolved_path = app_path.resolve()
# Skip if we've already seen this target
if resolved_path in seen_targets:
continue
seen_targets.add(resolved_path)
info_plist = resolved_path / "Contents" / "Info.plist"
if not info_plist.exists():
continue
try:
with open(info_plist, "rb") as f:
plist_data = plistlib.load(f)
bundle_version = plist_data.get("CFBundleShortVersionString", "")
build_version = plist_data.get("DTXcodeBuild", "")
if not bundle_version:
continue
# Check if it's a beta version (check both original and resolved paths)
is_beta = (
"beta" in bundle_version.lower()
or "beta" in app_path.name.lower()
or "beta" in resolved_path.name.lower()
)
# Use the resolved path as the canonical path
xcode_apps.append(
XcodeVersion(
version=bundle_version,
build=build_version,
path=resolved_path,
is_beta=is_beta,
)
)
except Exception as e:
typer.echo(f"Warning: Could not parse {app_path}: {e}", err=True)
continue
# Sort by version (newest first)
try:
xcode_apps.sort(key=lambda x: version.parse(x.version.split()[0]), reverse=True)
except Exception:
# Fallback to string sorting if version parsing fails
xcode_apps.sort(key=lambda x: x.version, reverse=True)
return xcode_apps
def normalize_version(ver: str) -> str:
"""Normalize version string for comparison (e.g., '16.4' -> '16.4.0')."""
# Split on space to handle "16.4 Beta" format
version_part = ver.split()[0]
# Split version into parts
parts = version_part.split('.')
# Ensure we have at least major.minor.patch
while len(parts) < 3:
parts.append('0')
return '.'.join(parts[:3]) # Take only major.minor.patch
def versions_match(target: str, candidate: str) -> bool:
"""Check if two versions match, handling different formats."""
try:
target_norm = normalize_version(target)
candidate_norm = normalize_version(candidate)
return version.parse(target_norm) == version.parse(candidate_norm)
except Exception:
# Fallback to string comparison
return target.lower() == candidate.lower()
def find_xcode_version(
target_version: str, xcode_apps: List[XcodeVersion]
) -> Optional[XcodeVersion]:
"""Find a specific Xcode version."""
if target_version == "latest":
return xcode_apps[0] if xcode_apps else None
if target_version == "latest-stable":
for app in xcode_apps:
if not app.is_beta:
return app
return None
# Try exact match first
for app in xcode_apps:
if app.version == target_version:
return app
# Try normalized version matching (handles 16.4 vs 16.4.0)
for app in xcode_apps:
if versions_match(target_version, app.version):
return app
# Try version prefix matching
for app in xcode_apps:
if app.version.startswith(target_version):
return app
return None
def is_github_actions() -> bool:
"""Check if running in GitHub Actions."""
return os.getenv("GITHUB_ACTIONS") == "true"
app = typer.Typer()
@app.command()
def list_versions():
"""List all installed Xcode versions."""
xcode_apps = find_installed_xcode_apps()
if not xcode_apps:
typer.echo("No Xcode installations found.")
return
typer.echo("Found Xcode installations:")
for xcode_app in xcode_apps:
beta_marker = " (Beta)" if xcode_app.is_beta else ""
typer.echo(
f" {xcode_app.version} ({xcode_app.build}){beta_marker} - {xcode_app.path}"
)
@app.command()
def select(
version_spec: str = typer.Argument(
..., help="Version to select (e.g., '15.0', 'latest', 'latest-stable')"
),
cleanup: bool = typer.Option(
False,
"--cleanup",
help="π¨ DANGER: Remove other Xcode versions and move selected to /Applications/Xcode.app",
),
force_cleanup: bool = typer.Option(
False,
"--force-cleanup",
help="π¨π¨ SUPER DANGER: Force cleanup even outside GitHub Actions",
),
dry_run: bool = typer.Option(
False, "--dry-run", help="Show what would be done without making any changes"
),
):
"""Select an Xcode version and optionally clean up others."""
# Safety check for cleanup
if cleanup and not is_github_actions() and not force_cleanup and not dry_run:
typer.echo(
"π¨ SAFETY CHECK: --cleanup is only allowed in GitHub Actions!", err=True
)
typer.echo(
"If you really want to do this locally, use --force-cleanup", err=True
)
typer.echo(
"Or use --dry-run to see what would happen without making changes", err=True
)
typer.echo("This will DELETE other Xcode installations!", err=True)
raise typer.Exit(1)
xcode_apps = find_installed_xcode_apps()
if not xcode_apps:
typer.echo("No Xcode installations found.", err=True)
raise typer.Exit(1)
selected = find_xcode_version(version_spec, xcode_apps)
if not selected:
typer.echo(f"Xcode version '{version_spec}' not found.", err=True)
typer.echo("Available versions:")
for app in xcode_apps:
beta_marker = " (Beta)" if app.is_beta else ""
typer.echo(f" {app.version}{beta_marker}")
raise typer.Exit(1)
typer.echo(
f"Selected Xcode {selected.version} ({selected.build}) at {selected.path}"
)
# Determine final path after cleanup (if any)
final_path = selected.path
if cleanup:
canonical_path = Path("/Applications/Xcode.app")
if selected.path != canonical_path:
final_path = canonical_path
# Set as active Xcode (skip if cleanup will handle it)
if not cleanup:
developer_dir = final_path / "Contents" / "Developer"
if dry_run:
typer.echo(f"[DRY RUN] Would run: sudo xcode-select --switch {developer_dir}")
else:
try:
cmd = ["sudo", "xcode-select", "--switch", str(developer_dir)]
typer.echo(f"Running: {' '.join(cmd)}")
subprocess.run(cmd, check=True)
typer.echo(f"Set active Xcode developer directory to {developer_dir}")
except subprocess.CalledProcessError as e:
typer.echo(f"Failed to set Xcode developer directory: {e}", err=True)
raise typer.Exit(1)
if cleanup:
typer.echo("π§ Ensuring canonical path points to selected Xcode...")
canonical_path = Path("/Applications/Xcode.app")
# Check if canonical path already points to the selected Xcode
if canonical_path.exists():
if canonical_path.is_symlink():
current_target = canonical_path.resolve()
if current_target == selected.path:
if dry_run:
typer.echo("[DRY RUN] Canonical path already points to selected Xcode")
else:
typer.echo("β
Canonical path already points to selected Xcode")
else:
# Remove existing symlink and create new one
if dry_run:
typer.echo(f"[DRY RUN] Would remove existing symlink {canonical_path} -> {current_target}")
typer.echo(f"[DRY RUN] Would create symlink {canonical_path} -> {selected.path}")
else:
typer.echo(f"Removing existing symlink {canonical_path} -> {current_target}")
canonical_path.unlink()
typer.echo(f"Creating symlink {canonical_path} -> {selected.path}")
canonical_path.symlink_to(selected.path)
typer.echo("β
Canonical path updated!")
else:
# Canonical path exists but is not a symlink
if canonical_path.resolve() == selected.path:
if dry_run:
typer.echo("[DRY RUN] Selected Xcode is already at canonical location")
else:
typer.echo("β
Selected Xcode is already at canonical location")
else:
typer.echo(f"ERROR: {canonical_path} exists but is not a symlink and not the selected Xcode!", err=True)
typer.echo("Cannot safely replace it. Please remove it manually first.", err=True)
raise typer.Exit(1)
else:
# Create new symlink
if dry_run:
typer.echo(f"[DRY RUN] Would create symlink {canonical_path} -> {selected.path}")
else:
typer.echo(f"Creating symlink {canonical_path} -> {selected.path}")
canonical_path.symlink_to(selected.path)
typer.echo("β
Canonical path created!")
# Set as active Xcode after cleanup
developer_dir = final_path / "Contents" / "Developer"
if dry_run:
typer.echo(f"[DRY RUN] Would run: sudo xcode-select --switch {developer_dir}")
else:
try:
cmd = ["sudo", "xcode-select", "--switch", str(developer_dir)]
typer.echo(f"Running: {' '.join(cmd)}")
subprocess.run(cmd, check=True)
typer.echo(f"Set active Xcode developer directory to {developer_dir}")
except subprocess.CalledProcessError as e:
typer.echo(f"Failed to set Xcode developer directory: {e}", err=True)
raise typer.Exit(1)
# Verify selection
if dry_run:
typer.echo("[DRY RUN] Would verify selection with: xcode-select --print-path")
else:
try:
result = subprocess.run(
["xcode-select", "--print-path"],
capture_output=True,
text=True,
check=True,
)
typer.echo(f"Active Xcode: {result.stdout.strip()}")
except subprocess.CalledProcessError:
typer.echo("Warning: Could not verify Xcode selection", err=True)
if __name__ == "__main__":
app()