Home > Net >  Share same pods across all sub-framework projects in a workspace
Share same pods across all sub-framework projects in a workspace

Time:12-05

I want to share pods across all the projects included in a workspace. This includes also any other projects that are going to be added in future as well. Here is roughly how my current project folder structure looks like:

 -- MyApp
|
 -- MyApp.xcworkspace
|
 -- Group 1
|    |
|     -- Project1
|    |    |
|    |     -- Target1.xcodeproj
|    |     -- (source code)
|    |    |
|    |     -- Target1Tests
|    |     -- (source code)
|    |
|     -- Project2
|    |    |
|    |     -- Target2.xcodeproj
|    |     -- (source code)
|    |    |
|    |     -- Target2Tests
|    |     -- (source code)
|
 -- Group 2
|    |
|     -- Project3
|    |    |
|    |     -- Target3.xcodeproj
|    |     -- (source code)
|    |    |
|    |     -- Target3Tests
|    |     -- (source code)
|    |
|     -- Project4
|    |    |
|    |     -- Target4.xcodeproj
|    |     -- (source code)
|    |    |
|    |     -- Target4Tests
|    |     -- (source code)

I have explored using abstract target but it still requires explicitly specifying all targets and in my workspace I have many projects. Here is what I have been able to come up with, with my limited knowledge in ruby:

use_frameworks!
def shared_pods
  pod 'Pod1'
  pod 'Pod2'
end

Dir["**/*.xcodeproj"].select { |project_path| !project_path.to_s.start_with?('Pods') }.each do |project_path|
  project_target = File.basename(project_path, ".xcodeproj")
  target project_target do
    workspace 'MyApp'
    project project_path
    shared_pods
  end

  target "#{project_target}Tests" do
    inherit! :search_paths
  end
end

but running pod install I am getting this error:

[!] Could not automatically select an Xcode project. Specify one in your Podfile like so:

    project 'path/to/Project.xcodeproj'

Is there any way to achieve what I am looking for?

CodePudding user response:

After taking some inspiration from this answer that @KirilS linked, I came up with this modified pod file that works:

use_frameworks!
def shared_pods
  pod 'Pod1'
  pod 'Pod2'
end

workspace 'MyApp'
abstract_target 'MyAppDependency' do
  shared_pods
  Dir["**/*.xcodeproj"].select { |project_path| !project_path.to_s.start_with?('Pods') }.each do |project_path|
    proj = Xcodeproj::Project.open project_path
    proj.targets.each do |t|
      target t.name do
        project project_path
      end
    end
  end
end

Alternate approach with better syntax:

use_frameworks!
def shared_pods
  pod 'Pod1'
  pod 'Pod2'
end

my_ws = 'MyApp'

workspace my_ws
abstract_target 'MyAppDependency' do
  shared_pods
  Xcodeproj::Workspace.new_from_xcworkspace("#{my_ws}.xcworkspace").file_references
   .select { |file| /^((?!Pods).)*\.xcodeproj/.match file.path }
   .map { |file| Xcodeproj::Project.open file.path }.each do |proj|
    proj.targets.each do |t|
      target t.name do
        project proj.path
      end
    end
  end
end
  • Related