-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_group.py
More file actions
804 lines (715 loc) · 21.1 KB
/
Copy pathmigrate_group.py
File metadata and controls
804 lines (715 loc) · 21.1 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#!/usr/bin/env python3
"""
Umbrella script for migrating an Xcode PBXGroup to a
PBXFileSystemSynchronizedRootGroup (folder reference).
Author: Deszip
Usage:
python migrate_group.py <group_path>
python migrate_group.py --verbose <group_path>
python migrate_group.py --dry-run <group_path>
Example:
python migrate_group.py "Classes/Intents"
python migrate_group.py --verbose "Classes/Intents"
Steps:
1. Extract target memberships from the old PBXGroup
2. Move files on disk to match group hierarchy (git mv)
3. Remove old PBXGroup entries from pbxproj
4. Add PBXFileSystemSynchronizedRootGroup + exception sets
"""
import json
import os
import subprocess
import sys
import time
from collections import defaultdict
PBXPROJ_PATH = "ReaddleDocs2.xcodeproj/project.pbxproj"
ALL_TARGETS = [
'Documents', 'DocumentsDemo',
'PDFExpert', 'PDFExpert Demo',
]
def find_suspicious_items(target_base, files_dict):
"""Scan target folder for files/dirs not in the group.
files_dict: the 'files' dict from JSON with keys being
expected relative paths and values having 'disk_path'.
"""
if not os.path.isdir(target_base):
return []
expected = set(files_dict.keys())
# Current disk paths relative to target_base
known_current = set()
abs_base = os.path.abspath(target_base)
for fdata in files_dict.values():
abs_disk = os.path.abspath(fdata['disk_path'])
if abs_disk.startswith(abs_base + '/'):
rel = os.path.relpath(abs_disk, abs_base)
known_current.add(rel)
known_files = expected | known_current
known_dirs = set()
for f in known_files:
parts = f.split('/')
for i in range(1, len(parts)):
known_dirs.add('/'.join(parts[:i]))
opaque_exts = {
'.xcassets', '.xcdatamodeld',
'.xcmappingmodel', '.bundle',
'.playground',
}
suspicious = []
for dirpath, dirnames, filenames in os.walk(target_base):
rel_dir = os.path.relpath(dirpath, target_base)
if rel_dir == '.':
rel_dir = ''
# Don't recurse into opaque bundles
bundles = [
d for d in dirnames
if os.path.splitext(d)[1] in opaque_exts
]
for b in bundles:
dirnames.remove(b)
rel_path = (
f"{rel_dir}/{b}" if rel_dir else b
)
if rel_path not in known_files:
suspicious.append(rel_path)
# Don't recurse into dirs that are known files
# (folder references like Lottie animation dirs)
folder_refs = [
d for d in dirnames
if (
f"{rel_dir}/{d}" if rel_dir else d
) in known_files
]
for fr in folder_refs:
dirnames.remove(fr)
for fname in filenames:
if fname == '.DS_Store':
continue
rel_path = (
f"{rel_dir}/{fname}" if rel_dir else fname
)
if rel_path not in known_files:
suspicious.append(rel_path)
for dname in dirnames:
rel_path = (
f"{rel_dir}/{dname}" if rel_dir else dname
)
if rel_path not in known_dirs:
suspicious.append(f"{rel_path}/")
return sorted(suspicious)
def report_suspicious(target_base, suspicious):
"""Print a warning about untracked items in the folder."""
if not suspicious:
return
print()
print("!" * 60)
print(" SUSPICIOUS: Items on disk not in Xcode group")
print("!" * 60)
print()
print(
f" The following items exist in '{target_base}' "
f"but are not part"
)
print(
" of the migrated group. They were left untouched."
)
print()
for item in suspicious:
tag = "dir " if item.endswith('/') else "file"
print(f" [{tag}] {item}")
print()
def resolve_folder_name(parent_path, group_name):
"""Check if folder already exists on disk and pick a
non-conflicting name if needed."""
target = os.path.join(parent_path, group_name)
if not os.path.exists(target):
return group_name
for i in range(2, 100):
candidate = f"{group_name}{i}"
if not os.path.exists(
os.path.join(parent_path, candidate)
):
print(
f" WARNING: Folder '{target}' already "
f"exists on disk."
)
print(
f" Will use '{candidate}' as folder name "
f"instead."
)
return candidate
print(
f"Error: Could not find unused folder name "
f"for {target}"
)
sys.exit(1)
def parse_group_path(group_path):
"""Parse 'Classes/Intents' into parent_path and
group_name."""
parts = group_path.rstrip('/').split('/')
if len(parts) < 2:
print(
f"Error: group_path must be in parent/group "
f"format, e.g. 'Classes/Intents'. "
f"Got: '{group_path}'"
)
sys.exit(1)
parent_path = parts[0]
group_name = parts[-1]
return parent_path, group_name
def run_step(description, cmd, verbose=False):
"""Run a migration step as a subprocess.
Quiet mode: one compact line per step.
Verbose mode: full banner, command, and output.
"""
if verbose:
print()
print("=" * 60)
print(f" {description}")
print("=" * 60)
print(f" Command: {' '.join(cmd)}")
print()
result = subprocess.run(
cmd, capture_output=False,
)
if result.returncode != 0:
print()
print(f" FAILED: {description}")
print(f" Exit code: {result.returncode}")
return result.returncode == 0
# Quiet mode
sys.stdout.write(f" {description}...")
sys.stdout.flush()
start = time.time()
result = subprocess.run(
cmd, capture_output=True, text=True, timeout=300,
)
elapsed = time.time() - start
if result.returncode == 0:
print(f" OK ({elapsed:.1f}s)")
else:
print(f" FAILED ({elapsed:.1f}s)")
err = result.stderr or result.stdout or ''
for line in err.strip().split('\n')[:3]:
if line.strip():
print(f" {line.strip()}")
return result.returncode == 0
def dry_run(
group_path, parent_path, group_name, json_path,
script_dir, group_id=None, new_folder=False,
):
"""Run extract only and print a report."""
print()
print("=" * 60)
print(" DRY RUN - no files or project will be modified")
print("=" * 60)
print()
extract_cmd = [
sys.executable,
os.path.join(
script_dir, "extract_target_memberships.py",
),
group_name,
PBXPROJ_PATH,
"--parent-path", parent_path,
]
if group_id:
extract_cmd += ["--group-id", group_id]
if not run_step(
"Extract target memberships (read-only)",
extract_cmd, verbose=True,
):
sys.exit(1)
if not os.path.exists(json_path):
print(f"Error: Expected JSON not found: {json_path}")
sys.exit(1)
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
files = data['files']
if new_folder:
folder_name = resolve_folder_name(
parent_path, group_name,
)
else:
folder_name = group_name
target_base = os.path.join(parent_path, folder_name)
print()
print("=" * 60)
print(" DRY RUN REPORT")
print("=" * 60)
print()
print(f" Group: {group_name}")
if folder_name != group_name:
print(f" Folder: {folder_name} (new folder)")
print(f" Group UUID: {data['group_uuid']}")
print(
f" Subgroups: {len(data['group_uuids']) - 1}"
)
print(f" Total files: {len(files)}")
print()
# File moves
print("-" * 60)
print(" FILE MOVES (git mv)")
print("-" * 60)
print()
moves = 0
skips = 0
missing = 0
for relative_path, fdata in files.items():
src = fdata['disk_path']
dst = os.path.join(target_base, relative_path)
if os.path.abspath(src) == os.path.abspath(dst):
skips += 1
continue
exists = os.path.exists(src)
if not exists:
tag = " [MISSING]"
missing += 1
else:
tag = ""
moves += 1
print(f" {src}{tag}")
print(f" -> {dst}")
print()
print(f" Will move: {moves} files")
if skips:
print(f" Already there: {skips} files")
if missing:
print(f" MISSING: {missing} files")
print()
# Target membership
print("-" * 60)
print(" TARGET MEMBERSHIP")
print("-" * 60)
print()
target_set_groups = defaultdict(list)
for relative_path, fdata in files.items():
key = (
tuple(sorted(fdata['targets']))
if fdata['targets'] else ()
)
target_set_groups[key].append(relative_path)
for target_set, fpaths in sorted(
target_set_groups.items(),
key=lambda x: (-len(x[1]), x[0]),
):
if target_set:
label = ", ".join(target_set)
else:
label = "(no targets - header only)"
print(f" [{label}]")
for fp in sorted(fpaths):
print(f" {fp}")
print()
# Exceptions
print("-" * 60)
print(" EXCEPTIONS (files excluded per target)")
print("-" * 60)
print()
for target_name in ALL_TARGETS:
excluded = []
for relative_path, fdata in files.items():
if target_name not in fdata['targets']:
excluded.append(relative_path)
if excluded:
print(
f" {target_name}: "
f"{len(excluded)} exclusions"
)
for fp in sorted(excluded):
print(f" - {fp}")
print()
else:
print(
f" {target_name}: no exclusions "
f"(all files included)"
)
print()
# pbxproj changes
print("-" * 60)
print(" PBXPROJ CHANGES")
print("-" * 60)
print()
bf_count = sum(
len(fdata.get('build_files', {}))
for fdata in files.values()
)
print(f" Will remove:")
print(f" {bf_count} PBXBuildFile entries")
print(f" {bf_count} build phase references")
print(f" {len(files)} PBXFileReference entries")
print(
f" {len(data['group_uuids'])} PBXGroup entries"
)
print()
print(f" Will add:")
exc_count = sum(
1 for t in ALL_TARGETS
if any(
t not in fdata['targets']
for fdata in files.values()
)
)
print(f" 1 PBXFileSystemSynchronizedRootGroup")
print(
f" {exc_count} "
f"PBXFileSystemSynchronizedBuildFileExceptionSet"
)
print()
nested_synced = data.get('nested_synced_groups', [])
if nested_synced:
print("-" * 60)
print(
" NESTED SYNCED FOLDERS "
"(will be absorbed)"
)
print("-" * 60)
print()
for sg in nested_synced:
targets_str = ', '.join(sg['targets'])
print(
f" {sg['name']} ({sg['uuid']})"
)
print(f" Targets: {targets_str}")
print()
if os.path.isdir(target_base):
suspicious = find_suspicious_items(
target_base, files,
)
report_suspicious(target_base, suspicious)
def execute(
group_path, parent_path, group_name, json_path,
script_dir, group_id=None, new_folder=False,
verbose=False,
):
"""Run the full migration."""
# Step 1: Extract target memberships
extract_cmd = [
sys.executable,
os.path.join(
script_dir, "extract_target_memberships.py",
),
group_name,
PBXPROJ_PATH,
"--parent-path", parent_path,
]
if group_id:
extract_cmd += ["--group-id", group_id]
if not run_step(
"Extract memberships", extract_cmd, verbose,
):
sys.exit(1)
if not os.path.exists(json_path):
print(f"Error: Expected JSON not found: {json_path}")
sys.exit(1)
if new_folder:
folder_name = resolve_folder_name(
parent_path, group_name,
)
else:
folder_name = group_name
# Step 2: Move files on disk
move_cmd = [
sys.executable,
os.path.join(
script_dir, "move_files_to_group_structure.py",
),
json_path,
]
if folder_name != group_name:
move_cmd += ["--folder-name", folder_name]
if not run_step("Move files", move_cmd, verbose):
sys.exit(1)
# Step 3: Remove old group from pbxproj
remove_cmd = [
sys.executable,
os.path.join(script_dir, "remove_old_group.py"),
json_path,
PBXPROJ_PATH,
]
if not run_step(
"Remove group from project", remove_cmd, verbose,
):
sys.exit(1)
# Step 4: Add filesystem synchronized group
restore_cmd = [
sys.executable,
os.path.join(
script_dir, "restore_target_memberships.py",
),
json_path,
PBXPROJ_PATH,
group_name,
]
if folder_name != group_name:
restore_cmd += ["--folder-path", folder_name]
if not run_step(
"Add folder to project", restore_cmd, verbose,
):
sys.exit(1)
# Step 5: Validate and fix target memberships
validate_cmd = [
sys.executable,
os.path.join(
script_dir, "validate_memberships.py",
),
"--fix", json_path,
PBXPROJ_PATH,
]
if not run_step(
"Validate memberships", validate_cmd, verbose,
):
sys.exit(1)
if verbose:
print()
print("=" * 60)
print(" MIGRATION COMPLETE")
print("=" * 60)
if folder_name != group_name:
print(
f" Group '{group_name}' migrated to "
f"folder '{folder_name}'."
)
else:
print(
f" Group '{group_name}' migrated to "
f"folder reference."
)
print()
print(" Next steps:")
print(
" 1. Open Xcode and verify the folder "
"appears correctly"
)
print(
" 2. Build all targets to verify "
"no files are missing"
)
print(" 3. Commit with: git add -A && git commit")
print()
else:
print(f" Migration complete: {group_name}")
def restore_membership(
folder_name, membership_path, verbose=False,
):
"""Restore target memberships for an existing synced
folder using a membership file."""
from restore_target_memberships import (
restore_membership_for_folder,
)
if verbose:
print()
print("=" * 60)
print(" Restore Target Memberships")
print("=" * 60)
print(f" Folder: {folder_name}")
print(f" Membership: {membership_path}")
print(f" Project: {PBXPROJ_PATH}")
print()
if not os.path.exists(membership_path):
print(f"Error: File not found: {membership_path}")
sys.exit(1)
if not verbose:
sys.stdout.write(" Restore memberships...")
sys.stdout.flush()
start = time.time()
success = restore_membership_for_folder(
membership_path, PBXPROJ_PATH, folder_name,
)
elapsed = time.time() - start
if success:
if verbose:
print("=" * 60)
print(" MEMBERSHIP RESTORE COMPLETE")
print("=" * 60)
print()
print(" Next steps:")
print(
" 1. Open Xcode and verify "
"target memberships"
)
print(
" 2. Build all targets to verify "
"no files are missing"
)
print()
else:
print(f" OK ({elapsed:.1f}s)")
else:
if verbose:
print("Error: Failed to restore memberships")
else:
print(f" FAILED ({elapsed:.1f}s)")
sys.exit(1)
def main():
args = sys.argv[1:]
is_dry_run = False
verbose = False
group_id = None
new_folder = False
membership_file = None
if '--dry-run' in args:
is_dry_run = True
args.remove('--dry-run')
if '--verbose' in args:
verbose = True
args.remove('--verbose')
if '--new-folder' in args:
new_folder = True
args.remove('--new-folder')
if '--group-id' in args:
idx = args.index('--group-id')
if idx + 1 >= len(args):
print("Error: --group-id requires a value")
sys.exit(1)
group_id = args[idx + 1]
args = args[:idx] + args[idx + 2:]
if '--restore-membership' in args:
idx = args.index('--restore-membership')
if idx + 1 >= len(args):
print(
"Error: --restore-membership "
"requires a file path"
)
sys.exit(1)
membership_file = args[idx + 1]
args = args[:idx] + args[idx + 2:]
if membership_file:
if len(args) != 1:
print(
"Usage: python migrate_group.py "
"--restore-membership "
"<membership.json> <folder_name>"
)
print()
print(
" folder_name: Name of the synced "
"folder in Xcode project"
)
print(
" membership: JSON file mapping "
"file -> [targets]"
)
print()
print("Example:")
print(
" python migrate_group.py "
"--restore-membership \\\n"
" /tmp/xcode_membership_Statistics"
".json Statistics"
)
sys.exit(1)
restore_membership(
args[0], membership_file, verbose,
)
return
if len(args) != 1:
print(
"Usage: python migrate_group.py [options] "
"<group_path>"
)
print()
print("Options:")
print(
" --verbose Show full output "
"from each step"
)
print(
" --dry-run Extract and print report "
"only,"
)
print(
" do not modify files or "
"project"
)
print(
" --group-id ID Group UUID (to disambiguate "
"groups with same name)"
)
print(
" --new-folder Create a new folder if one "
"with the same name"
)
print(
" already exists (appends a "
"number suffix)"
)
print(
" --restore-membership <file>"
)
print(
" Restore target memberships "
"for an existing"
)
print(
" synced folder using a "
"membership file"
)
print()
print("Examples:")
print(
" python migrate_group.py "
"\"Classes/Intents\""
)
print(
" python migrate_group.py --verbose "
"\"Classes/Intents\""
)
print(
" python migrate_group.py --dry-run "
"\"Classes/Intents\""
)
print(
" python migrate_group.py "
"--group-id ABC123DEF456... "
"\"Classes/iCloud\""
)
print(
" python migrate_group.py "
"--restore-membership \\\n"
" /tmp/xcode_membership_Stats.json "
"Statistics"
)
sys.exit(1)
group_path = args[0]
parent_path, group_name = parse_group_path(group_path)
safe_name = (
group_name.replace('/', '_').replace(' ', '_')
)
json_path = (
f"/tmp/xcode_target_memberships_{safe_name}.json"
)
script_dir = os.path.dirname(os.path.abspath(__file__))
if verbose or is_dry_run:
print()
print("=" * 60)
mode = (
"DRY RUN" if is_dry_run
else "Xcode Group Migration"
)
print(f" {mode}")
print("=" * 60)
print(f" Group path: {group_path}")
print(f" Parent: {parent_path}")
print(f" Group name: {group_name}")
if group_id:
print(f" Group ID: {group_id}")
if new_folder:
print(f" New folder: yes")
print(f" Project: {PBXPROJ_PATH}")
print(f" JSON output: {json_path}")
print()
else:
print(f"Migrating: {group_name}")
if is_dry_run:
dry_run(
group_path, parent_path, group_name,
json_path, script_dir, group_id, new_folder,
)
else:
execute(
group_path, parent_path, group_name,
json_path, script_dir, group_id, new_folder,
verbose,
)
if __name__ == '__main__':
main()