Search code examples
rubycocoapods

Post-Install of Cocoapod get main project directly


In creating a CocoaPod Podspec for a vendored_framework I need to install a shell script to be run after all other build_phases in the main project. The only way I have found to do this is by groping the directory for the .xcodeproj file from the Podfile.

post_install do |installer|
  project_name = Dir.glob("*.xcodeproj").first
  project = Xcodeproj::Project.open(project_name)
  project.targets.each do |target|
    inject_shell_script_build_phase(target)
  end
  project.save
end

This seems ridiculous given that pod install or pod update is running and clearly has this information already. Is there a way to get the project reference directly without having to glob or hardcode the project filename?


Solution

  • https://guides.cocoapods.org/syntax/podfile.html

    There is installer.pods_project

    post_install do |installer|
      project = installer.pods_project
    
      project.targets.each do |target|
        inject_shell_script_build_phase(target)
      end
    
      project.save
    end
    

    https://www.rubydoc.info/gems/cocoapods/Pod/Installer#pods_project-instance_method https://www.rubydoc.info/gems/cocoapods/Pod/Project inherits from https://www.rubydoc.info/gems/xcodeproj/Xcodeproj/Project


    to iterate the *.xcodeproj you can do

    post_install do |installer|
      installer.aggregate_targets.each do |target|
        user_project = target.user_project
    
        puts "#{user_project.path}"
      end
    end
    

    https://www.rubydoc.info/gems/cocoapods/Pod%2FInstaller%3Aaggregate_targets https://www.rubydoc.info/gems/cocoapods/Pod/AggregateTarget