#!/usr/bin/env python3 import re import sys # List of tab files to modify (file path, component name) tabs = [ ('src/components/game/tabs/SpireTab.tsx', 'SpireTab'), ('src/components/game/tabs/AttunementsTab.tsx', 'AttunementsTab'), ('src/components/game/tabs/GolemancyTab.tsx', 'GolemancyTab'), ('src/components/game/tabs/SpellsTab.tsx', 'SpellsTab'), ('src/components/game/tabs/EquipmentTab.tsx', 'EquipmentTab'), ('src/components/game/tabs/CraftingTab.tsx', 'CraftingTab'), ('src/components/game/tabs/LootTab.tsx', 'LootTab'), ('src/components/game/tabs/AchievementsTab.tsx', 'AchievementsTab'), ('src/components/game/tabs/StatsTab.tsx', 'StatsTab'), ('src/components/game/tabs/DebugTab.tsx', 'DebugTab'), ('src/components/game/SkillsTab.tsx', 'SkillsTab'), ] for file_path, component_name in tabs: print(f"Processing {file_path}...") try: with open(file_path, 'r') as f: content = f.read() except FileNotFoundError: print(f" - ERROR: File not found: {file_path}") continue original_content = content # Check if DebugName is already imported if 'from \'@/lib/game/debug-context\'' in content or 'from "@/lib/game/debug-context"' in content: print(f" - DebugName already imported in {file_path}") else: # Find the last import line and add the DebugName import after it lines = content.split('\n') last_import_idx = -1 for i, line in enumerate(lines): if line.startswith('import ') or 'import {' in line: last_import_idx = i if last_import_idx >= 0: # Insert the import after the last import lines.insert(last_import_idx + 1, "import { DebugName } from '@/lib/game/debug-context';") content = '\n'.join(lines) print(f" - Added DebugName import to {file_path}") else: print(f" - WARNING: No import found in {file_path}") continue # Now find the main return statement and wrap its JSX with DebugName # Pattern: return ( ) where the jsx starts with < # We need to find: return ( followed by newline and then < # Find the pattern: return ( \n \n \n < pattern1 = r'(return\s*\(\s*\n)(\s*)(<)' def replace_start(m): indent = m.group(2) # The indentation before the opening tag return f'{m.group(1)}{indent}\n{indent}{m.group(3)}' modified = re.sub(pattern1, replace_start, content, count=1) if modified == content: print(f" - WARNING: Could not find return pattern in {file_path}") continue # Now find the closing of that return statement and add # The return ends with: ); followed by } (end of function) # We need to find the matching closing parenthesis and then add before it # Let's find the last or similar closing tag before the displayName line # Actually, a simpler approach: find the line with ");" that is followed by "}" # and then the displayName line # Let's find the displayName line and work backwards display_name_pattern = re.escape(component_name) + r'\.displayName\s*=\s*"[^"]+"' # Find where the function ends - look for the displayName or end of file lines = modified.split('\n') # Find the line with displayName display_name_line = -1 for i, line in enumerate(lines): if re.search(display_name_pattern, line): display_name_line = i break if display_name_line == -1: print(f" - WARNING: Could not find displayName for {component_name}") continue # Now find the closing of the return statement - look backwards from displayName # Find the line with " );" which closes the return close_paren_line = -1 for i in range(display_name_line - 1, -1, -1): if ');' in lines[i] and i > 0: close_paren_line = i break if close_paren_line == -1: print(f" - WARNING: Could not find closing ); for return in {file_path}") continue # Insert before the closing ); # We need to find the right place - it should be after the last or similar # Let's just insert it right before the ); # Find the indentation of the ); close_indent = '' for char in lines[close_paren_line]: if char == ' ': close_indent += ' ' else: break # Insert the closing tag before the ); line lines.insert(close_paren_line, f"{close_indent}") modified = '\n'.join(lines) # Write back with open(file_path, 'w') as f: f.write(modified) print(f" - Successfully wrapped {component_name} with DebugName") print("\nDone!")