Skip to content

Instantly share code, notes, and snippets.

@11010001101001
Last active December 26, 2023 04:35
Show Gist options
  • Save 11010001101001/0774e59f00cfce2df08c644e027a4469 to your computer and use it in GitHub Desktop.
Save 11010001101001/0774e59f00cfce2df08c644e027a4469 to your computer and use it in GitHub Desktop.
Renamer
import time
import os
"""
Rename classes, structs, enums, entities, etc. in whole project, quickly
"""
DIR = 'abs path to yout project'
def replace(old_name, new_name):
try:
print('Renaming... ⏳')
start = time.time()
for subdir, dirs, files in os.walk(DIR):
def update(file):
new_contents = None
path = f'{subdir}/{file}'
with open(f'{path}', 'r') as f:
new_contents = f.read().replace(old_name, new_name)
f.close()
if new_name in new_contents:
with open(f'{path}', 'w') as f2:
f2.write(new_contents)
f2.close()
for file in files:
name = os.path.splitext(file)
_format = name[1]
if _format == '.swift':
update(file)
finish = time.time()
spent = finish - start
print(f'✅ Success! Done for {round(spent, 3)} sec.')
except BaseException as e:
print(f'💥 Failure! Error: {e}')
def interact():
old_name = None
while not old_name:
old_name = input('Enter old entity name: \n')
new_name = old_name.replace('New', '')
correct = input(f'Is "{new_name}" correct? ("" / "n")\n')
match correct:
case '':
replace(old_name, new_name)
case 'n':
new_name = None
while not new_name:
new_name = input('Enter new entity name: \n')
replace(old_name, new_name)
case _:
interact()
if __name__ == '__main__':
interact()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment