Xcode 新建文件都会给文件添加一段注释:
//
// AppDelegate.m
// DDNote
//
// Created by ajeyone on 2019/1/25.
// Copyright © 2019年 ajeyone. All rights reserved.
//
其实这段注释并没有什么卵用,都是冗余信息,历史记录都在 git commits 中呢。这个注释的模板是可以改的,但项目初期也没管这个,也不需要有特殊的注释,就保持原样吧。
但是,有的文件名与注释中的文件名不一致!这就有点不能忍了,强迫症犯了。一个一个改,烦死,好在哥会 python,写个小脚本都给改了吧,以后想起来执行以下就可以了。
非常简单,只有 30 行
import os,sys,glob
def rewrite_file(file_path):
base_name = os.path.basename(file_path)
lines = []
with open(file_path, 'r') as fd:
lines = fd.readlines()
# 👇👇👇 替换第二行
lines[1] = '// ' + base_name + '\n'
with open(file_path, 'w') as fd:
fd.writelines(lines)
def has_filename_comment(file_path):
base_name = os.path.basename(file_path)
with open(file_path, 'r') as fd:
line = fd.readline()
line = fd.readline()
# 👇👇👇 判断第二行有没有文件名
if line.startswith('//') and base_name in line:
return True
return False
def correct_files_without_filename_comment(search_pattern):
files = glob.glob(search_pattern, recursive=True)
for file_path in files:
if not has_filename_comment(file_path):
rewrite_file(file_path)
print('the file has been corrected:', file_path)
correct_files_without_filename_comment('**/*.h')
correct_files_without_filename_comment('**/*.m')
correct_files_without_filename_comment('**/*.mm')
这里假设所有文件都是Xcode自动生成的,没有自定义过注释的格式。直接判断每个文件的第二行有没有正确的文件名就行了。
把这个文件放在代码目录下执行就行了,注意别放到 Pod 目录的上级目录。
题图:Pixabay License
网友评论