如果您经常使用开发 pod,那么 cocoapods 创建的默认项目结构有很多问题:
-
如果您的source_files / resources 等的根目录中没有至少一个文件,则根目录在项目中没有对应的组。
-
组的位置 指向您的 pod 目录的根目录(不一定是您的源文件所在的位置)。这意味着您不能只右键单击组和“添加文件”,因为它会将文件放在错误的位置。
-
组和文件的顺序在模块之间有所不同,因为事物是按名称排序的,并且您的 pod 显然具有不同的名称,这些名称可能出现在 Frameworks / Pods / Dependencies / Support Files 之前。
我一直在寻找解决上述所有问题的解决方案,并最终提出以下解决方案:
先决条件
我使用如下目录结构设置我的 pod(示例显示名为 SomeLib 的 pod)
Example/
SomeLib-Example/...
SomeLib-Example.xcodeproject
fix_project_structure (this is an empty text file)
SomeLib.podspec
Sources/
FolderA/
ClassA.swift
FolderB/
ClassA.swift
Tests/
fix_project_structure (another empty text file)
FolderA/
ClassATests.swift
FolderB/
ClassBTests.swift
第 1 步 - 让组镜像目录层次结构
在您的podspec 文件中,我们利用位于根目录中的空fix_project_structure 来修复组结构...
s.source_files = 'Sources/**/*.{swift}', 'fix_project_structure'
s.resource_bundles = {
s.name => ['Sources/**/*.{xcassets,json,storyboard,xib,xcdatamodeld}', 'fix_project_structure']
}
s.test_spec 'Tests' do |test_spec|
test_spec.source_files = 'Tests/**/*', 'Tests/fix_project_structure'
end
第 2 步 - 重新排序和修复组位置
这实际上是一个可选步骤(见评论)
将以下post_integrate 步骤添加到您的Podfile...
post_integrate do |installer|
# Fix up the Development Pods so they are easier to work with.
# The following changes are made to any pod that contains a `fix_project_structure` file
# - Removes the (now redundant) `fix_project_structure` file
# - Moves the root {project_name} group to the top
# - Moves the {project_name}/Sources group to the top
# - Moves the {project_name}/Tests group below {project_name}/Sources
# - Repoints the {project_name}/Tests group so it reflects its location on disc
# Note this script makes no functional changes to the pod so you should be able to comment out
# all of this script and have things continue to work just fine
installer.generated_projects.each do |project|
fix_files = project.files.select { |file| File.basename(file.path) == "fix_project_structure" }
project_name = "#{File.basename(project.path, '.xcodeproj')}"
next if fix_files.nil? || fix_files.empty? || project_name.nil?
puts "#{project_name} -> #{project.path}"
puts " `fix_project_structure` file(s) FOUND"
puts " Removing `fix_project_structure` file(s) from project entirely"
fix_files.each { |fix_file| fix_file.remove_from_project }
project_group = project.groups.find.find { |group| group.to_s == project_name }
next if project_group.nil?
puts " `#{project_name}` group FOUND"
puts " Moving `#{project_name}` group to top"
project.main_group.children.delete(project_group)
project.main_group.children.insert(0, project_group)
sources_group = project_group.groups.find.find { |subgroup| subgroup.to_s == 'Sources' }
if sources_group.nil? != true
puts " `#{project_name}/Sources` group FOUND"
puts " Moving `#{project_name}/Sources` group to top"
project_group.children.delete(sources_group)
project_group.children.insert(0, sources_group)
tests_group = project_group.groups.find.find { |subgroup| subgroup.to_s == 'Tests' }
if tests_group.nil? != true
puts " `#{project_name}/Test` group FOUND"
puts " Moving `#{project_name}/Test` group below `#{project_name}/Sources`"
project_group.children.delete(tests_group)
project_group.children.insert(1, tests_group)
puts " Adjusting `#{project_name}/Test` group and immediate children location"
tests_group.set_path("#{tests_group.real_path}/Tests")
tests_group.children.each do |tests_child|
tests_child_path_before = tests_child.path
tests_child.set_path("#{tests_child.real_path.to_s.gsub('/Tests/Tests/', '/Tests/')}")
#puts " '#{tests_child.path.to_s}' (previously '#{tests_child_path_before}')"
end
end
end
project.save
end
end