Files
Mana-Loop/add_debugname.py
T
n8n-gitea 482320b519
Build and Publish Mana Loop Docker Image / build-and-publish (push) Failing after 1m12s
fix: apply DebugName wrappers to tab components (BUG 7 partial) and other updates
2026-05-07 13:32:04 +02:00

140 lines
5.3 KiB
Python

#!/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 ( <jsx...> ) where the jsx starts with <
# We need to find: return ( followed by newline and then <
# Find the pattern: return ( \n <tag
# Replace with: return ( \n <DebugName name="..."> \n <tag
# First, let's find where the main return starts
# Look for "return (" that is followed by a newline and then some whitespace and then <
# Simple approach: find the first occurrence of "return (" and then find the next "("
# Actually, let's find the pattern more carefully
# We'll use a regex to find: return ( \s* \n \s* <
# And replace with: return ( \n <DebugName name="..."> \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}<DebugName name="{component_name}">\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 </DebugName>
# The return ends with: ); followed by } (end of function)
# We need to find the matching closing parenthesis and then add </DebugName> before it
# Let's find the last </div> 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 </DebugName> before the closing );
# We need to find the right place - it should be after the last </div> 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}</DebugName>")
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!")