-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_xcode_final.py
More file actions
86 lines (67 loc) · 3.14 KB
/
fix_xcode_final.py
File metadata and controls
86 lines (67 loc) · 3.14 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
#!/usr/bin/env python3
import re
import shutil
from collections import defaultdict
def remove_all_duplicates():
project_path = "/Users/个人_local/macos-cat-pet/Sources/swift/Pets Therapy.xcodeproj/project.pbxproj"
# Backup
backup_path = project_path + ".backup_final"
shutil.copy2(project_path, backup_path)
print(f"Backed up to: {backup_path}\n")
with open(project_path, 'r') as f:
content = f.read()
# Find all PBXSourcesBuildPhase sections
pattern = r'(/\* Begin PBXSourcesBuildPhase section \*/.*?/\* End PBXSourcesBuildPhase section \*/)'
def process_build_phases(match):
section = match.group(1)
modified = False
# Find each build phase within the section
phase_pattern = r'([A-F0-9]{24}\s*/\*\s*Sources\s*\*/\s*=\s*\{[^}]*?files\s*=\s*\()(.*?)(\);\s*[^}]*?\};)'
def process_phase(phase_match):
nonlocal modified
prefix = phase_match.group(1)
files_section = phase_match.group(2)
suffix = phase_match.group(3)
# Extract all file entries
file_entries = []
entry_pattern = r'([A-F0-9]{24}\s*/\*[^*]+\*/,)'
for entry in re.finditer(entry_pattern, files_section):
file_entries.append(entry.group(1))
# Track seen files by their identifier
seen_ids = set()
unique_entries = []
removed = 0
for entry in file_entries:
# Extract the ID
id_match = re.match(r'([A-F0-9]{24})', entry)
if id_match:
entry_id = id_match.group(1)
if entry_id not in seen_ids:
seen_ids.add(entry_id)
unique_entries.append(entry)
else:
removed += 1
# Extract file name for logging
name_match = re.search(r'/\*\s*([^*]+)\s*\*/', entry)
if name_match:
print(f" Removing duplicate: {name_match.group(1).strip()}")
modified = True
if removed > 0:
print(f" Removed {removed} duplicate(s) from this phase")
# Rebuild the files section
new_files = '\n'.join([f'\t\t\t\t{entry}' for entry in unique_entries])
if new_files:
new_files = '\n' + new_files + '\n\t\t\t'
return prefix + new_files + suffix
return phase_match.group(0)
new_section = re.sub(phase_pattern, process_phase, section, flags=re.DOTALL)
return new_section
print("Processing build phases...")
new_content = re.sub(pattern, process_build_phases, content, flags=re.DOTALL)
# Write back
with open(project_path, 'w') as f:
f.write(new_content)
print("\nProject file updated successfully!")
print("Run 'xcodebuild clean' or clean in Xcode (Cmd+Shift+K)")
if __name__ == "__main__":
remove_all_duplicates()