遇到的困难:做很多小项目,想想很多基本东西都是基本一致的,但是又不能封装,需要修改一些配置,例如工程Info.plist、基类,重新建工程,重复工作,不能忍, 百度过工程改名,过程对于我来说太复杂了... 例如这位哥们的修改方法链接
然后今天新建私有库的时候突然发现,CocoaPods的Pod Lib Create
的原理就是下载模板然后替换内容
emmm...
想法来了直接新建一个基本配置都差不多的工程,然后替换工程名...
人生苦短,用刚学两天的python开始干活
import os
def deal_file_content(file_path, old_name, new_name):# 处理替换的文件
try:
if file_path.endswith('.DS_Store'):
return
f =open(file_path, 'r+', )
content = f.readlines()
f.seek(0, 0)
for linein content:
line_new = line.replace(old_name, new_name)
f.write(line_new)
f.close()
except Exception as err:
print(file_path)
print(err)
# 遍历指定目录,并且修改
def each_file(dir_path, old_name, new_name):
path_dir = os.listdir(dir_path)
for allDirin path_dir:
# 自己选择忽略几个文件夹 处理的不好 我自己够用了
if allDir =='Assets.xcassets' or allDir =='base_project' or allDir =='Base.lproj':
continue
child = os.path.join('%s/%s' % (dir_path, allDir))
if allDir.__contains__(old_name):
if os.path.isdir(child):# 是文件夹
new_path = os.path.join('%s/%s' % (dir_path, allDir.replace(old_name, new_name)))
os.rename(child, new_path)
child = new_path
if os.path.isdir(child):
each_file(child, old_name, new_name)
else:
deal_file_content(child, old_name, new_name)
# 调用执行
each_file('/Users/jihaifeng/Desktop/TestFrame/BaseOCWorkspace/Farming', old_name="Farming",
new_name="SearchEverything")
喜欢这样的简单粗暴!!!
网友评论