【问题标题】:'[CP] Embed Pods Frameworks' build script ordering'[CP] Embed Pods Frameworks' 构建脚本排序
【发布时间】:2021-02-10 12:12:13
【问题描述】:

我有一个使用 Cocoapods 的项目。作为 cocoa pods 设置的一部分,Xcode“构建阶段”获得了两个 Cocoapods 脚本:

  • [CP] Embed Pods Frameworks
  • [CP] Copy Pods Resources

在同一个项目中,我使用了在[CP] Embed Pods Frameworks 之后运行的脚本。我可以手动设置它,它的工作原理是

  • [CP] Embed Pods Frameworks
  • My Script
  • [CP] Copy Pods Resources

问题是在运行 pod deintegrate 重新安装 pod 后,脚本的顺序被重置为:

  • My Script
  • [CP] Embed Pods Frameworks
  • [CP] Copy Pods Resources

由于这个项目的团队工作量很大。错误地重置脚本顺序可能会导致 CI​​ 中断。

Podfile 中有没有办法控制脚本的顺序?

【问题讨论】:

    标签: ios xcode cocoapods


    【解决方案1】:

    CocoaPods 使用了一个 ruby​​ gem:https://github.com/CocoaPods/Xcodeproj 来处理 Xcode 项目文件。因此,在它的帮助下,可以操纵项目中构建阶段的顺序。我进行了快速测试,以下脚本可以满足您的要求:

    #fix_build_phases.rb
    require 'xcodeproj'
    
    def main
      begin
        proj = Xcodeproj::Project.open("YourProject.xcodeproj")
        target = proj.native_targets.select { |target| target.name == "YourTarget" }.first
        phase = target.build_phases.select { |phase| phase.display_name == "MyScript" }.first
        
        idx = -1
        target.build_phases.each_with_index do |phase, index| 
            if phase.display_name == "[CP] Embed Pods Frameworks"
                idx = index
            end
        end
        if idx > 0
            target.build_phases.move(phase, idx)
        end
        proj.save
      end
    end
    
    main
    

    如果您想确保安装 pod 后脚本始终位于正确的位置,您需要将其添加到您的 podfile:

    post_install do |installer|
        system("(sleep 1 && ruby fix_build_phases.rb)&")
    end
    

    有一些延迟,因为它没有立即在 post_install 上运行,可能是因为 Cocoapods 在那一刻自行更新了项目。

    【讨论】:

    • 我将post_install 更改为post_integrate 并且能够摆脱睡眠:post_integrate do |installer| system("ruby fix_build_phases.rb") end
    【解决方案2】:

    我还没有找到决定具体订单的方法。但是可以使用 Cocoapods 功能,该功能允许在 Podfile 中指定用户脚本。

    所以如果你在你的Podfile 中使用script_phase 喜欢

    target 'App' do
        all_your_pods
    
        script_phase :name => 'My Script', :script => 'echo "script"'
    end
    

    您可以将My Script 插入到所有其他[CP] 脚本之后:

    • [CP] Embed Pods Frameworks
    • [CP] Copy Pods Resources
    • [CP-User] My Script

    即使不允许您声明特定的顺序,这也解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2018-04-23
      • 1970-01-01
      • 2017-03-29
      • 2017-08-11
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      相关资源
      最近更新 更多