I have a project where I'm using CocoaPods. As part of the CocoaPods setup, the Xcode "Build Phase" gets two Cocoapods scripts:
[CP] Embed Pods Frameworks
[CP] Copy Pods Resources
In the same project I use a script that is required to run after [CP] Embed Pods Frameworks
. I can setup it up manually and it works having
[CP] Embed Pods Frameworks
My Script
[CP] Copy Pods Resources
Problem is that after running pod deintegrate
to reinstall the pods the order of script is reset to:
My Script
[CP] Embed Pods Frameworks
[CP] Copy Pods Resources
As the team work on this project is big. It can happen that the script order is reset by mistake causing the CI to break.
Is there a way in the Podfile to control the order of the scripts?
There's a ruby gem used by CocoaPods: https://github.com/CocoaPods/Xcodeproj to work with Xcode project file. So with its help it's possible to manipulate the order of build phases in your project. I did a quick testing and the following script shold do what you want:
#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
If you want to ensure the script is always at the right position after the pods are installed you will need to add it to your podfile:
post_install do |installer|
system("(sleep 1 && ruby fix_build_phases.rb)&")
end
There's some delay because it didn't work immediately on post_install, probably due to Cocoapods updating the project by itself at that moment.