Search code examples
iosswiftcocoapodsdependency-managementswift-package-manager

Cocoapods, add dependencies to all targets in a generic way


App has over 20 targets.

Every time I add a target, I need to update my podfile:

Podfile

target 'SomeNewTarget' do
    runAllPods // Block that contains all "pod ...."
end

My question:

Is there a generic way of adding dependencies to all targets? I would basically like to add same dependencies to all targets except test target.

Full podifle:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

inhibit_all_warnings!

def runAllPods
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for MyApp

  pod 'Swinject', '~> 2.1.0'
  pod 'SwinjectAutoregistration', '2.1.0'
  pod 'IHKeyboardAvoiding', '4.0'
  pod 'SnapKit', '3.2.0'
  pod 'Material', '~> 2.10.2'
  pod 'RxSwift', '~> 3.0'
  pod 'RxCocoa', '~> 3.0'
  pod 'PureLayout'
  pod 'Alamofire', '~> 4.5'

....More dependencies

end

  post_install do |installer|
    installer.pods_project.targets.each do |target|
      if target.name == 'HydraAsync' || target.name == 'DKImagePickerController' || target.name == 'WhatsNew' || target.name == 'Closures' 
        target.build_configurations.each do |config|
          config.build_settings['SWIFT_VERSION'] = '4.0'
        end
      end
    end
  end



target 'Target1' do
  runAllPods
end

target 'Target2' do
  runAllPods
end

target 'Target3' do
  runAllPods
end

target 'Target4' do
  runAllPods
end
...
target 'Target 20' do
  runAllPods
end

... More targets


Solution

  • Doing something like this will probably work.

    def common_pods
        pod 'Pod1'
        pod 'Pod2'
        pod 'Pod3'
        …
    end
    
    project = Xcodeproj::Project.open "../Project.xcodeproj"
    project.targets.each do |t|
        target t.name do
            common_pods
        end
    end
    

    For more details on this approach, check out this thread on the Cocoapod's repo:

    https://github.com/CocoaPods/CocoaPods/issues/5898