Object-C替换类前缀

iOSClassChangePrefix

该脚本只适用于有统一类前缀且类前缀唯一的情况。如果当前没有统一的类前缀可参考类名加前缀先添加统一的类前缀。

使用步骤:

第一步:将iOSClassChangePrefix.rb拷贝到工程文件同目录下。

第二步:打开iOSClassChangePrefix.rb文件,修改@oldPrefix和@newPrefix的值。(@oldPrefix为类当前前缀,@newPrefix为类新的前缀)

第三步:终端cd到同目录下,运行 ruby iOSClassChangePrefix.rb YYModel (YYModel即为工程名)


@oldPrefix = "ABCDEFG"  #旧的类前缀
@newPrefix = "GFEDCBA"  #新的类前缀

@pwd = Dir.pwd #当前路径

#更改project文件
def changeProject(path)
    project = `ls #{path} | grep .xcodeproj`.chomp
    projectPath = path+"#{project}/project.pbxproj"

    content = File.read projectPath
    content = content.gsub(@oldPrefix, @newPrefix)
    File.write projectPath, content
end

#更改类名前缀和类名引用
def changeClassPrefix(path)
    Dir.foreach path do |entry|
        if whetherToSkip entry
            next
        end

        #文件的完整路径
        filePath = path+"/"+entry

        if File.directory? filePath
            changeClassPrefix filePath
        end

        #只修改.h .m .xib .storyboard文件
        if entry.end_with? ".h" or entry.end_with? ".m" or entry.end_with? ".xib" or entry.end_with? ".storyboard"
            content = File.read filePath
            content = content.gsub(@oldPrefix, @newPrefix)
            File.write filePath, content
        end

        if entry.include? @oldPrefix
            oldPath = filePath
            newPath = path+"/"+entry.gsub(@oldPrefix, @newPrefix)
            File::rename oldPath, newPath
        end
    end
end

def whetherToSkip(entry)
    if entry.start_with? "." or entry.end_with? "xcodeproj" or entry.start_with? "Pod" or entry.end_with? "framework" or entry.end_with? "xcworkspace" or entry.end_with? ".a" #以./~/Pod开头的文件或文件夹不处理. .. ..DS_Store
        return true #跳过
    end
    false
end

if ARGV.count == 1 and File.exist? @pwd+"/"+ARGV[0]
    path = @pwd+"/"+ARGV[0]+"/" #工程项目的路径

    changeProject path
    changeClassPrefix path
else
    puts "请输入正确的项目名"
end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容