学习Xcodeproj可以从最简单的项目开始着手。
- 新建一个SingleViewController Project,使用Cocoapod/Xcodeproj打开,查看相关的数据
- 仅仅删除掉 .xcodeproj
- 使用Cocoapod/Xcodeproj创建一个.xcodeproj,最终运行效果和刚新建一个SingleViewController Project一样
查看xcodeproj
require 'xcodeproj'
project = Xcodeproj::Project.open('/Users/wangxuefeng/WorkSpace/SingleVCApplication/SingleVCApplication.xcodeproj')
target = project.targets.first
target.build_configuration_list.build_configurations.each { |item| puts item.build_settings }
{
"INFOPLIST_FILE"=>"SingleVCApplication/Info.plist",
"PRODUCT_BUNDLE_IDENTIFIER"=>"com.feng.SingleVCApplication",
"ASSETCATALOG_COMPILER_APPICON_NAME"=>"AppIcon",
"LD_RUNPATH_SEARCH_PATHS"=>"$(inherited) @executable_path/Frameworks",
"PRODUCT_NAME"=>"$(TARGET_NAME)"
}
创建新的xcodeproj
require 'xcodeproj'
def removeBuildPhaseFilesRecursively(aTarget, aGroup)
aGroup.files.each do |file|
if file.real_path.to_s.end_with?(".m", ".mm", ".cpp") then
aTarget.source_build_phase.remove_file_reference(file)
elsif file.real_path.to_s.end_with?(".plist") then
aTarget.resources_build_phase.remove_file_reference(file)
end
end
aGroup.groups.each do |group|
removeBuildPhaseFilesRecursively(aTarget, group)
end
end
def addFilesToGroup(project,target,group)
Dir.foreach(group.real_path) do |entry|
filePath = File.join(group.real_path, entry)
puts filePath
# 过滤目录和.DS_Store
if !File.directory?(filePath) && entry != ".DS_Store" then
# 向group中增加文件引用
fileReference = group.new_reference(filePath)
# 如果不是头文件则继续增加到Build Phase中,PB文件需要加编译标志
if filePath.to_s.end_with?("pbobjec.m", "pbobjc.mm") then
target.add_file_references([fileReference],'-fno-objc-arc')
elsif filePath.to_s.end_with?(".m",".mm",".cpp") then
target.source_build_phase.add_file_reference(fileReference,true)
elsif filePath.to_s.end_with?(".plist",".storyboard",".xib") then
target.resources_build_phase.add_file_reference(fileReference,true)
end
elsif File.directory?(filePath) && entry != '.' && entry != '..' then
puts filePath
# 目录递归添加
hierarchy_path = group.hierarchy_path[1,group.hierarchy_path.length]
subGroup = project.main_group.find_subpath(hierarchy_path + '/' + entry,true)
subGroup.set_source_tree(group.source_tree)
subGroup.set_path(group.real_path + entry)
addFilesToGroup(project,target,subGroup)
end
end
end
proj = Xcodeproj::Project.new("./Test/Test.xcodeproj")
app_target = proj.new_target(:application, 'Test', :ios, '7.0')
puts proj.main_group.pretty_print
puts proj.main_group.real_path
test_group = proj.main_group.new_group("Test","Test","<group>")
addFilesToGroup(proj,app_target,test_group)
puts app_target.build_configuration_list
#创建一个PBXVariantGroup xib/storyboard
#test_group.new_variant_group("Asserts.xcassets","Asserts.xcassets");
app_target.build_configuration_list.set_setting('INFOPLIST_FILE','Test/Info.plist')
app_target.build_configuration_list.set_setting('PRODUCT_BUNDLE_IDENTIFIER','com.fungo.test')
app_target.build_configuration_list.build_configurations.each { |item| puts item.build_settings }
puts app_target.resolved_build_setting('INFOPLIST_FILE')
proj.save()