# -*- coding: UTF-8 -*-
import os,sys
import random
import string
import re
#import md5
import time
import json
import shutil
import hashlib 
import time
import argparse

import sys 


script_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
add_prefix = ""
old_prefix = "ZZH_"
new_prefix = "BSHighCode"
ios_src_path = ""
project_file_path = ""

ignore_path_text = [".a", ".png", ".plist", ".storyboard", ".pch"]

#首字母大写
def isNeedIgnore(file_path):
    global ignore_path_text
    for ignore_text in ignore_path_text:
        if file_path.find(ignore_text) != -1:
            return True
    return False

def replaceStringInFile(full_path, old_text, new_text):
    with open(full_path, "r",errors="ignore") as fileObj:
        all_text = fileObj.read()
        fileObj.close()

    all_text = all_text.replace(old_text, new_text)
    with open(full_path, "w",errors="ignore") as fileObj:
        fileObj.write(all_text)
        fileObj.close()

def renameFileInXcodeProj(old_file_name, new_file_name):
    global project_file_path
    if os.path.exists(project_file_path):
        replaceStringInFile(project_file_path, old_file_name, new_file_name)
        
#替换文件中的文本内容:匹配到的文本被替换为指定的new_text
def renameInAllFile(old_text, new_text):
    global ios_src_path
    for parent, folders, files in os.walk(ios_src_path):
        for file in files:
            full_path = os.path.join(parent, file)
            replaceStringInFile(full_path, old_text, new_text)

#开始重命名类名
def dealWithIos(folder_path):
    print ("开始重命名类名")
    global old_prefix, new_prefix
    for parent, folders, files in os.walk(folder_path):
        #先处理每个文件的名字
        for file in files:
            print("\n the file:" + file + "\n")
            #得到该文件的路径
            old_full_path = os.path.join(parent, file)
            #如果该文件名称匹配为需要重命名的类名 名称,并且该文本不属于被忽略的文件,则需要执行重命名操作
            if file.startswith(old_prefix) and not isNeedIgnore(old_full_path):
                #将该文件名称中匹配上的文本部分替换为new_prefix,并得到新文件名 名称
                new_file_name = file.replace(old_prefix, new_prefix)
                print ("\t重命名文件: %s -> %s" %(file, new_file_name))
                #合成新文件名对应的文件路径
                new_full_path = os.path.join(parent, new_file_name)
                #将原来的文件改命为新的文件名称
                os.rename(old_full_path, new_full_path)
                #在项目工程中改名
                renameFileInXcodeProj(file, new_file_name)

                #在可能引用的地方替换,获取不带文件名后缀的部分
                old_file_base_name = os.path.splitext(file)[0]
                print("\n the old_file_base_name:" + old_file_base_name + "\n")
                new_file_base_name = old_file_base_name.replace(old_prefix, new_prefix)
                #将ios_src_path 所指定的目录下的所有old_file_base_name文本(主要是指的类名) 替换为新的new_file_base_name文本 (类名)
                renameInAllFile(old_file_base_name, new_file_base_name)

    for parent, folders, files in os.walk(folder_path):
        for folder in folders:
            old_full_path = os.path.join(parent, folder)
            #递归处理该目录下的内容
#            dealWithIos(old_full_path)
            if folder.startswith(old_prefix) and not isNeedIgnore(old_full_path):
                new_folder_name = folder.replace(old_prefix, new_prefix)
                print ("\t重命名文件夹: %s -> %s" %(folder, new_folder_name))
                new_full_path = os.path.join(parent, new_folder_name)
                os.rename(old_full_path, new_full_path)
                #在项目工程中改名
                renameFileInXcodeProj(folder, new_folder_name)
    print ("finish\n")

#开始添加前缀
def addPreFix():
    print ("开始添加前缀")
    global add_prefix, ios_src_path
    for parent, folders, files in os.walk(ios_src_path):
        for file in files:
            old_full_path = os.path.join(parent, file)
            if not isNeedIgnore(old_full_path):
                new_file_name = add_prefix + file
                print ("\t重命名文件: %s -> %s" %(file, new_file_name))

                new_full_path = os.path.join(parent, new_file_name)
                os.rename(old_full_path, new_full_path)
                #在项目工程中改名
                renameFileInXcodeProj(file, new_file_name)

                #在可能引用的地方替换
                old_file_base_name = os.path.splitext(file)[0]
                new_file_base_name = os.path.splitext(new_file_name)[0]
                renameInAllFile(old_file_base_name, new_file_base_name)
                renameInAllFile(add_prefix+add_prefix, add_prefix)

    for parent, folders, files in os.walk(ios_src_path):
        for folder in folders:
            old_full_path = os.path.join(parent, folder)
            if not isNeedIgnore(old_full_path):
                new_folder_name = add_prefix + folder
                print ("\t重命名文件夹: %s -> %s" %(folder, new_folder_name))
                new_full_path = os.path.join(parent, new_folder_name)
                os.rename(old_full_path, new_full_path)
                #在项目工程中改名
                renameFileInXcodeProj(folder, new_folder_name)
    print ("finish\n")

#给方法添加前缀
#.....


#修改指定前缀的方法名
#....

#给每个方法的起始行处 添加垃圾代码
#......

#----------------------------------------------------main------------------------------------------------
def parse_args():
    global script_path, proj_ios_path
    parser = argparse.ArgumentParser(description='修改类名前缀工具.\n')
    parser.add_argument('--add_prefix', dest='add_prefix', type=str, required=False, default="", help='添加类名前缀')
    parser.add_argument('--old_prefix', dest='old_prefix', type=str, required=False, help='原类名前缀')
    parser.add_argument('--new_prefix', dest='new_prefix', type=str, required=False, help='替换后类名前缀')
    parser.add_argument('--ios_path', dest='ios_path', type=str, required=True, help='OC文件目录')
    parser.add_argument('--proj_path', dest='proj_path', type=str, required=False, default="", help='xx.xcodeproj路径')
    args = parser.parse_args()
    return args

def main():
    global old_prefix, new_prefix, ios_src_path, project_file_path, add_prefix
    app_args = parse_args()

    add_prefix = app_args.add_prefix
    old_prefix = app_args.old_prefix
    new_prefix = app_args.new_prefix
    ios_src_path = app_args.ios_path
    project_file_path = os.path.join(app_args.proj_path, "project.pbxproj")
    if not os.path.exists(ios_src_path):
        print ("ios_path not exists: " +  ios_src_path)
        exit(0)
    if not os.path.exists(project_file_path):
        print ("proj_path not exists: " +  project_file_path)

    print ("请提前备份文件夹或确认在版本管理软件中")
    input("回车继续执行")
    #给类名添加前缀:实际作用是,当以前编写的项目工程中的类名(类名必需和文件名字一致,因为是通过文件名称去依次修改文件名、类名的)没有添加一致的前缀
    if add_prefix and add_prefix != "":
        addPreFix()
        #添加完毕前缀后,就退出程序
        print("\n添加完毕前缀后,就退出程序\n")
        exit(0)
    dealWithIos(ios_src_path)
 

if __name__ == "__main__":
    main()
