Created
December 28, 2025 10:10
-
-
Save baba-s/2fdfe2ec707634dace49fae50d6e936a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import argparse | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import shutil | |
| def read_package_name(pkg_dir: Path): | |
| pj = pkg_dir / "package.json" | |
| if not pj.is_file(): | |
| return None | |
| try: | |
| with pj.open(encoding="utf-8") as f: | |
| data = json.load(f) | |
| return data.get("name") | |
| except Exception as e: | |
| print(f"[WARN] Failed to read {pj}: {e}") | |
| return None | |
| def main(): | |
| ap = argparse.ArgumentParser(description="Rename embedded package folders to match package.json name.") | |
| ap.add_argument("--project", required=True, help="Path to Unity project root (the folder containing Packages/)") | |
| ap.add_argument("--apply", action="store_true", help="Actually rename. If omitted, dry-run.") | |
| ap.add_argument("--backup-log", default="rename-embedded-packages.log", help="Path to write a mapping log") | |
| args = ap.parse_args() | |
| project = Path(args.project).resolve() | |
| packages = project / "Packages" | |
| if not packages.is_dir(): | |
| print(f"[ERR] Not found: {packages}") | |
| sys.exit(1) | |
| mappings = [] | |
| for child in sorted(packages.iterdir()): | |
| if not child.is_dir(): | |
| continue | |
| name = read_package_name(child) | |
| if not name: | |
| continue # not a package | |
| # すでに一致していればスキップ | |
| if child.name == name: | |
| continue | |
| target = child.parent / name | |
| # 同じ実体(ケース違いのみ)の可能性に配慮 | |
| same_ignoring_case = child.name.lower() == name.lower() | |
| collision = target.exists() and target.resolve() != child.resolve() | |
| if collision: | |
| print(f"[ERR] Skip: target already exists: {target}") | |
| continue | |
| print(f"[{('APPLY' if args.apply else 'DRY')}] {child.name} -> {name}") | |
| mappings.append((str(child), str(target))) | |
| if args.apply: | |
| # ケース違いのみで Mac の大小無視 FS だと直接 rename が失敗するので一時名を挟む | |
| if same_ignoring_case: | |
| temp = child.parent / (name + "__tmp__" ) | |
| child.rename(temp) | |
| temp.rename(target) | |
| else: | |
| child.rename(target) | |
| if mappings: | |
| with open(args.backup_log, "w", encoding="utf-8") as f: | |
| for a,b in mappings: | |
| f.write(f"{a} -> {b}\n") | |
| print(f"[INFO] Mapping log written: {args.backup_log}") | |
| if args.apply: | |
| print("[INFO] Done. Re-open Unity (or trigger reimport).") | |
| else: | |
| print("[INFO] Dry-run finished. Re-run with --apply to rename.") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment