-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_old_group.py
More file actions
270 lines (227 loc) · 8.32 KB
/
Copy pathremove_old_group.py
File metadata and controls
270 lines (227 loc) · 8.32 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
#!/usr/bin/env python3
"""
Remove old PBXGroup and related entries from Xcode project after migration.
Author: Deszip
Usage: python remove_old_group.py <json_path> <project_path>
"""
import json
import re
import sys
class PBXProjectCleaner:
"""Removes old group/file/build entries from project.pbxproj"""
def __init__(self, project_path):
self.project_path = project_path
try:
with open(project_path, 'r', encoding='utf-8') as f:
self.content = f.read()
except Exception as e:
print(f"Error reading project file: {e}")
sys.exit(1)
def remove_build_file(self, build_file_uuid):
"""Remove a PBXBuildFile entry line"""
pattern = rf'\t\t{build_file_uuid} /\*[^*]*\*/ = \{{isa = PBXBuildFile;[^}}]*\}};\n'
self.content, count = re.subn(pattern, '', self.content)
return count
def remove_build_phase_reference(self, build_file_uuid):
"""Remove build file UUID from build phase files arrays"""
pattern = rf'\t\t\t\t{build_file_uuid} /\*[^*]*\*/,\n'
self.content, count = re.subn(pattern, '', self.content)
return count
def remove_file_reference(self, file_ref_uuid):
"""Remove a PBXFileReference entry line"""
pattern = rf'\t\t{file_ref_uuid} /\*[^*]*\*/ = \{{[^}}]*\}};\n'
self.content, count = re.subn(pattern, '', self.content)
return count
def remove_group(self, group_uuid):
"""Remove a PBXGroup definition block (multi-line)"""
pattern = (
rf'\t\t{group_uuid} /\*[^*]*\*/ = \{{\n'
rf'.*?\n\t\t\}};\n'
)
self.content, count = re.subn(
pattern, '', self.content, flags=re.DOTALL
)
return count
def remove_child_reference(self, child_uuid):
"""Remove a UUID reference from any group's children array"""
pattern = rf'\t\t\t\t{child_uuid} /\*[^*]*\*/,\n'
self.content, count = re.subn(pattern, '', self.content)
return count
def remove_synced_root_group(self, group_uuid):
"""Remove a PBXFileSystemSynchronizedRootGroup
entry (single-line format)."""
pattern = (
rf'\t\t{group_uuid} /\*[^*]*\*/ = '
rf'\{{isa = PBXFileSystemSynchronized'
rf'RootGroup;.*?\}};\n'
)
self.content, count = re.subn(
pattern, '', self.content,
)
return count
def remove_synced_group_from_targets(
self, group_uuid,
):
"""Remove synced group UUID from all targets'
fileSystemSynchronizedGroups arrays."""
pattern = (
rf'\t\t\t\t{group_uuid}'
rf' /\*[^*]*\*/,\n'
)
self.content, count = re.subn(
pattern, '', self.content,
)
return count
def remove_exception_sets_for_synced_group(
self, synced_uuid,
):
"""Remove exception sets referenced by a synced
group. Must be called BEFORE removing the synced
group itself."""
# Find exception UUIDs from synced group entry
pattern = (
rf'{synced_uuid} /\*[^*]+\*/ = \{{.*?'
rf'exceptions = \((.*?)\)'
)
match = re.search(pattern, self.content)
if not match:
return 0
exc_uuids = re.findall(
r'([A-F0-9]{24})', match.group(1),
)
removed = 0
for exc_uuid in exc_uuids:
exc_pattern = (
rf'\t\t{exc_uuid} /\*.*?\*/ = \{{\n'
rf'.*?\n\t\t\}};\n'
)
self.content, count = re.subn(
exc_pattern, '', self.content,
flags=re.DOTALL,
)
removed += count
return removed
def write(self):
"""Write modified content back to file"""
try:
with open(self.project_path, 'w', encoding='utf-8') as f:
f.write(self.content)
except Exception as e:
print(f"Error writing project file: {e}")
sys.exit(1)
def remove_old_group(json_path, project_path):
"""Remove old PBXGroup entries from pbxproj"""
print(f"\n=== Removing Old Group from Project ===")
print(f"JSON: {json_path}")
print(f"Project: {project_path}")
print()
try:
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
except Exception as e:
print(f"Error reading JSON file: {e}")
return False
group_name = data['group_name']
group_uuid = data['group_uuid']
group_uuids = data.get('group_uuids', [group_uuid])
print(f"Group: {group_name}")
print(f"Group UUIDs to remove: {len(group_uuids)}")
print(f"Files to clean up: {len(data['files'])}")
print()
cleaner = PBXProjectCleaner(project_path)
# Step 1: Remove PBXBuildFile entries and build phase references
print("Removing PBXBuildFile entries...")
build_files_removed = 0
phase_refs_removed = 0
for file_data in data['files'].values():
for target_name, bf_uuid in file_data.get('build_files', {}).items():
build_files_removed += cleaner.remove_build_file(bf_uuid)
phase_refs_removed += cleaner.remove_build_phase_reference(
bf_uuid
)
print(f" Removed {build_files_removed} PBXBuildFile entries")
print(f" Removed {phase_refs_removed} build phase references")
# Step 2: Remove PBXFileReference entries
print("Removing PBXFileReference entries...")
file_refs_removed = 0
for file_data in data['files'].values():
file_refs_removed += cleaner.remove_file_reference(
file_data['file_ref_uuid']
)
print(f" Removed {file_refs_removed} PBXFileReference entries")
# Step 3: Remove top-level group child reference from parent
print("Removing child reference from parent group...")
cleaner.remove_child_reference(group_uuid)
# Step 4: Remove all PBXGroup entries (nested first, then top-level)
print("Removing PBXGroup entries...")
groups_removed = 0
# Process in reverse order so nested groups are removed first
for g_uuid in reversed(group_uuids):
groups_removed += cleaner.remove_group(g_uuid)
print(f" Removed {groups_removed} PBXGroup entries")
# Step 5: Remove nested synced folder entries
nested_synced = data.get('nested_synced_groups', [])
if nested_synced:
print(
"Removing nested synced folder entries..."
)
synced_removed = 0
target_refs_removed = 0
exc_removed = 0
for sg in nested_synced:
sg_uuid = sg['uuid']
sg_name = sg['name']
# Remove exception sets first
exc_removed += (
cleaner
.remove_exception_sets_for_synced_group(
sg_uuid,
)
)
# Remove from targets' synced groups arrays
target_refs_removed += (
cleaner
.remove_synced_group_from_targets(
sg_uuid,
)
)
# Remove child reference from parent group
cleaner.remove_child_reference(sg_uuid)
# Remove the synced root group entry
synced_removed += (
cleaner.remove_synced_root_group(
sg_uuid,
)
)
print(f" Removed synced group: {sg_name}")
print(
f" Total: {synced_removed} synced groups, "
f"{target_refs_removed} target refs, "
f"{exc_removed} exception sets"
)
# Write result
print()
print("Writing modified project file...")
cleaner.write()
print()
print(f"Old group '{group_name}' removed from project")
return True
def main():
if len(sys.argv) != 3:
print(
"Usage: python remove_old_group.py <json_path> <project_path>"
)
print()
print("Example:")
print(
" python remove_old_group.py \\\n"
" \"/tmp/xcode_target_memberships_Statistics.json\" \\\n"
" \"ReaddleDocs2.xcodeproj/project.pbxproj\""
)
sys.exit(1)
json_path = sys.argv[1]
project_path = sys.argv[2]
success = remove_old_group(json_path, project_path)
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()