最近公司搞马甲包,iOS想上架,代码肯定不能一样啊,否则不是妥妥等被拒嘛,搞了套批量修改代码的脚本,改代码+改UI,至今四个包了,还没有收到苹果大大的拒绝邮件,在就是那些混淆工具真是太贵了!!!不过也支持人家的知识产权,但是
开源万岁!!!
所以我把脚本共享给大家,希望共同学习,共同进步!
node.js版
const fs = require('fs');
const path = require('path')
//需要修改的类名前缀或关键字 替换处!
let oneruits = ['HQ', 'OneShot', 'Transaction']
// 新的类名前缀或关键字 一一对应 替换处!
let tworuits = ['TP', 'FirstShot', 'Jiaoyi']
// 项目路径
let project_path = '/Users/mxs/Desktop/***/代码文件夹'
let pbxpro_path = '/Users/mxs/Desktop/***/****.xcodeproj/project.pbxproj'
let suf_set = ['.h', '.m', '.xib', '.storyboard', '.mm']
var needModifyDic = {}
for (let index = 0; index < oneruits.length; index++) {
let pre_str = oneruits[index]
let pre_to_str = tworuits[index]
function getDirTree(inputPath, callback) {
let files = fs.readdirSync(inputPath)
for (file of files) {
let filePath = path.join(inputPath+ '/' + file);
let fileState = fs.statSync(filePath);
if (fileState.isDirectory()) { // 如果是目录 递归
getDirTree(filePath)
} else {
var file_name = path.parse(file).name;
//获取文件后缀
var ext_name = path.extname(file);
if (file_name.indexOf(pre_str) != -1 && suf_set.indexOf(ext_name) != -1) {
let newName = file_name;
newName = newName.replace(pre_str, pre_to_str)
needModifyDic[file_name] = newName
newName = newName + ext_name;
let fileNewPath = path.join(inputPath+ '/' + newName);
fs.renameSync(filePath,fileNewPath);
}
}
}
callback && callback.call();
}
getDirTree(project_path, function () {
console.log(needModifyDic);
getChaneDirTree(project_path,function () {
for (const key in needModifyDic) {
let dataStr = fs.readFileSync(pbxpro_path,'utf8');
if (dataStr.indexOf(key) != -1) {
dataStr = dataStr.replace(new RegExp(key,"gm"), needModifyDic[key]);
fs.writeFileSync(pbxpro_path,dataStr);
}
}
})
})
function getChaneDirTree(inputPath, callback) {
let files = fs.readdirSync(inputPath)
for (file of files) {
let filePath = path.join(inputPath+ '/' + file);
let fileState = fs.statSync(filePath);
if (fileState.isDirectory()) { // 如果是目录 递归
getChaneDirTree(filePath)
} else {
//获取文件后缀
var ext_name = path.extname(file);
if (suf_set.indexOf(ext_name) != -1) {
let dataStr = fs.readFileSync(filePath,'utf8');
for (const key in needModifyDic) {
if (dataStr.indexOf(key) != -1) {
dataStr = dataStr.replace(new RegExp(key,"gm"), needModifyDic[key]);
fs.writeFileSync(filePath,dataStr);
}
}
}
}
}
callback && callback.call();
}
}
python版本
# -- coding: UTF-8 --
import os
# 需要修改的类名前缀或关键字 替换处!
oneruits = ['MS', 'OneShot', 'Transaction']
# 新的类名前缀或关键字 一一对应 替换处!
tworuits = ['TP', 'FirstShot', 'Jiaoyi']
# 项目路径
project_path = '/Users/mxs/Desktop/***/代码文件夹'
pbxpro_path = '/Users/mxs/Desktop/***/****.xcodeproj/project.pbxproj'
suf_set = ('.h', '.m', '.xib', '.storyboard', '.mm')
for index in range(len(oneruits)):
pre_str = oneruits[index]
pre_to_str = tworuits[index]
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)
needModifyDic = {}
for (root, dirs, files) in os.walk(project_path):
for file_name in files:
print(type(file_name))
if pre_str in file_name 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):
print('-----fileName-------' + file_name)
with open(os.path.join(root, file_name), 'r+') as f:
print('========fileName========' + file_name)
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()