-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.py
74 lines (62 loc) · 2.76 KB
/
rename.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 重命名文件
import os
# 需要修改的类名前缀
pre_str = "HYJ"
# 新的类名前缀 (需替换)
pre_to_str = "RGC"
# 搜寻以下文件类型 (根据自己需求替换)
suf_set = (".h",".m",".xib",".storyboard",".mm")
# 项目路径 (找到自己的项目路径)
project_path = "/Users/kenchoi/Documents/test/HYJRefactoring/HYJRefactoring"
# 项目project.pbxproj文件路径 需要更新配置文件中的类名 (找到自己的项目project.pbxproj路径)
pbxpro_path = "/Users/kenchoi/Documents/test/HYJRefactoring/HYJRefactoring.xcodeproj/project.pbxproj"
# 文件重命名函数,返回新的文件名
def file_rename(file_path):
root_path = os.path.split(file_path)[0] # 文件目录
root_name = os.path.split(file_path)[1] # 文件名包含扩展名
filename = os.path.splitext(root_name)[0] # 文件名
filetype = os.path.splitext(root_name)[1] # 文件扩展名
new_path = os.path.join(root_path, filename.replace(
pre_str, pre_to_str) + filetype) # 拼接新路径
os.renames(file_path, new_path) # 文件重命名
return filename.replace(pre_str, pre_to_str)
# 定义一个字典 key=旧类名 value=新类名
needModifyDic = {}
# 遍历文件,符合规则的进行重命名
for (root, dirs, files) in os.walk(project_path):
for file_name in files:
if file_name.startswith((pre_str,)) and file_name.endswith(suf_set):
old_name = os.path.splitext(file_name)[0]
new_name = file_rename(os.path.join(root, file_name))
needModifyDic[old_name] = new_name
# 遍历文件,在文件中更换新类名的引用
print(needModifyDic)
for (root, dirs, files) in os.walk(project_path):
for file_name in files:
if file_name.endswith(suf_set):
with open(os.path.join(root, file_name), "r +") as f:
s0 = f.read()
f.close()
for key in needModifyDic:
if key in s0:
with open(os.path.join(root, file_name), "r +") as f4:
s1 = f4.read().replace(key, needModifyDic[key])
print(key + " ——> " + needModifyDic[key])
f4.seek(0)
f4.write(s1)
f4.truncate()
f4.close()
# 替换配置文件中的类名
for key in needModifyDic:
with open(pbxpro_path, "r +") as f:
s0 = f.read()
f.close()
if key in s0:
with open(pbxpro_path, "r +") as f2:
s = f2.read().replace(key, needModifyDic[key])
f2.seek(0)
f2.write(s)
f2.truncate()
f2.close()