在工作区中的所有子框架项目中共享相同的 pods

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

我想在工作区中包含的所有项目中共享 pods。这也包括将来要添加的任何其他项目。我当前的项目文件夹结构大致如下:

+-- 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)

我已经探索过使用抽象目标,但它仍然需要明确指定所有目标,并且在我的工作区中我有很多项目。以下是我在 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

但是 运行 pod install 我收到这个错误:

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

    project 'path/to/Project.xcodeproj'

有什么方法可以实现我想要的吗?

从@KirilS 链接的 this 回答中获得一些灵感后,我想出了这个有效的修改后的 pod 文件:

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

具有更好语法的替代方法:

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