美文网首页
Xcodeproj:ruby脚本修改Xcode项目引用

Xcodeproj:ruby脚本修改Xcode项目引用

作者: iVikings | 来源:发表于2020-09-10 10:16 被阅读0次

    Xcodeproj:https://www.rubydoc.info/github/CocoaPods/Xcodeproj/index

    #!/usr/bin/env ruby
    require 'xcodeproj'
    
    #打开项目工程
    project_path = File.join(File.dirname(__FILE__), "../../WCUIKit/WCUIKit.xcodeproj")
    project = Xcodeproj::Project.open(project_path)
    #target = project.targets.first
    
    #遍历target,找到需要操作的target
    targetIndex = 0
    
    project.targets.each_with_index do |target, index|
      if target.name  == "WCUIKit"
        targetIndex = index
      end
    end
    
    target = project.targets[targetIndex]
    
    puts "#{target}, #{project}"
    
    def removeBuildPhaseFilesRecursively(aTarget, aGroup)
      aGroup.files.each do |file|
          if file.real_path.to_s.end_with?(".m", ".mm", ".cpp") then
              aTarget.source_build_phase.remove_file_reference(file)
          elsif file.real_path.to_s.end_with?(".a") then
              aTarget.frameworks_build_phases.remove_file_reference(file)
          # elsif file.real_path.to_s.end_with?(".plist") then
          #     aTarget.resources_build_phase.remove_file_reference(file)
          end
      end
      
      aGroup.groups.each do |group|
          removeBuildPhaseFilesRecursively(aTarget, group)
      end
    end
    
    
    def addFilesToGroup(project, aTarget, aGroup)
      Dir.foreach(aGroup.real_path) do |entry|
          filePath = File.join(aGroup.real_path, entry)
          # 过滤目录和.DS_Store文件
          if !File.directory?(filePath) && entry != ".DS_Store" then
            # 向group中增加文件引用
            file_ref = aGroup.new_reference(filePath)
            if filePath.to_s.end_with?(".m", ".mm", ".cpp") then
                aTarget.source_build_phase.add_file_reference(file_ref, true)
            elsif filePath.to_s.end_with?(".a") then
                aTarget.frameworks_build_phases.add_file_reference(file_ref)
            # 如果不是头文件则继续增加到Build Phase中,PB文件需要加编译标志
            # elsif filePath.to_s.end_with?("pbobjc.m", "pbobjc.mm") then
            #     aTarget.add_file_references([file_ref], '-fno-objc-arc')
            # elsif filePath.to_s.end_with?(".plist") then
            #     aTarget.resources_build_phase.add_file_reference(file_ref, true)    
            end
          # 目录情况下,递归添加
          elsif File.directory?(filePath) && entry != '.' && entry != '..' then
            hierarchy_path = aGroup.hierarchy_path[1, aGroup.hierarchy_path.length]
            subGroup = project.main_group.find_subpath(hierarchy_path + '/' + entry, true)
            subGroup.set_source_tree(aGroup.source_tree)
            subGroup.set_path(aGroup.real_path + entry)
            addFilesToGroup(project, aTarget, subGroup)
          end
      end
    end
    
    # framework_group = project.main_group.find_subpath('framework', true)
    # framework_group.set_source_tree('<group>')
    # framework_group.set_path('framework')
    
    im_lib_group = project.main_group.find_subpath('framework/im_lib', true)
    im_lib_group.set_source_tree('<group>')
    im_lib_group.set_path('im_lib')
    
    im_logic_group = project.main_group.find_subpath('framework/logic_lib', true)
    im_logic_group.set_source_tree('<group>')
    im_logic_group.set_path('logic_lib')
    
    if !im_lib_group.empty? then
        removeBuildPhaseFilesRecursively(target, im_lib_group)
        im_lib_group.clear()
    end
    
    if !im_logic_group.empty? then
        removeBuildPhaseFilesRecursively(target, im_logic_group)
        im_logic_group.clear()
    end
    
    addFilesToGroup(project, target, im_lib_group)
    addFilesToGroup(project, target, im_logic_group)
    
    
    # file_ref = project.frameworks_group.new_file('framework/im_lib/libWChatLib.a')
    # target.frameworks_build_phases.add_file_reference(file_ref)
    
    # file_ref = project.frameworks_group.new_file('framework/logic_lib/libWChatLogic.a')
    # target.frameworks_build_phases.add_file_reference(file_ref)
    
    #设置索引 FRAMEWORK_SEARCH_PATHS
    target.build_configurations.each do |config|
        config.build_settings["LIBRARY_SEARCH_PATHS"] = [
          "$(inherited)",
          "$(PROJECT_DIR)/framework/**",
        ]
    end
    
    target.build_configurations.each do |config|
        config.build_settings["HEADER_SEARCH_PATHS"] = [
          "$(inherited)",
          "$(PROJECT_DIR)/framework/**",
        ]
    end
    
    project.save
    

    参考:

    相关文章

      网友评论

          本文标题:Xcodeproj:ruby脚本修改Xcode项目引用

          本文链接:https://www.haomeiwen.com/subject/juwuektx.html