在開發iOS應用的時候,大部分都是直接採用Xcode進行開發,但有時候須要用命令行來建立工程,好比最近在作ci的持續集成,就只能經過命令行的方式,這時候就須要瞭解一下工程文件的構成。咱們知道工程文件的相關信息保存在project.pbxproj,所以能夠經過腳本建立出pbxproj文件,完成基礎工程的建立。ios
下面介紹一下pbxproj文件,能夠拖動.xcodeproj文件到文本編輯器,如sublime,查看pbxproj文件的組成方式,主要包括:git
/* Begin PBXBuildFile section */ F60CC2A114D4EA0500A005E4 /* SocketOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F60CC2A014D4EA0500A005E4 /* SocketOperation.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section * F60CC2A014D4EA0500A005E4 /* SocketOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SocketOperation.m; sourceTree = "<group>"; }; /* End PBXFileReference section */
* Begin PBXGroup section */ F62417EA14D52F3C003CE997 /* TestChat */ = { isa = PBXGroup; children = ( F62417EB14D52F3C003CE997 /* Supporting Files */, F62417F314D52F3C003CE997 /* AppDelegate.h */, F62417F414D52F3C003CE997 /* AppDelegate.m */, ); path = TestChat; sourceTree = "<group>"; }; /* End PBXGroup section */
/* Begin PBXNativeTarget section */ CAC8612E08B161103B6C9DC7 /* UIModuleExample */ = { isa = PBXNativeTarget; buildConfigurationList = 56006E5E8040DE2B3965BE91 /* Build configuration list for PBXNativeTarget "UIModuleExample" */; buildPhases = ( 58D3152C3900AA8B62A79D47 /* Sources */, A5BF724742232AA0E86F3339 /* Frameworks */, ); buildRules = ( ); dependencies = ( ); name = UIModuleExample; productName = UIModuleExample; productReference = 377070E96E22438316AB8879 /* UIModuleExample.app */; productType = "com.apple.product-type.application"; }; /* End PBXNativeTarget section */
/* Begin XCBuildConfiguration section */ F62417FD14D52F3C003CE997 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "\"$(DEVELOPER_FRAMEWORKS_DIR)\"", ); GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "TestChat/TestChat-Prefix.pch"; INFOPLIST_FILE = "TestChat/TestChat-Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 5.0; OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; TARGETED_DEVICE_FAMILY = "1,2"; WRAPPER_EXTENSION = app; }; name = Debug; }; /* End XCBuildConfiguration section */
經過上面分析一個pbxproj文件的過程能夠看出,要建立一個工程,首先須要添加相關的文件,而後設置須要生成的target以及對應的配置信息就好了。github
上面大概瞭解了一個pbxproj文件的總體構造,要想生成一個這樣的文件,能夠本身按照對應的格式書寫,或者藉助一些腳本工具,這裏推薦cocoapods的Xcodeproj,項目的地址:Xcodeproj ,該工具是用ruby語言實現的,能夠用它來修改和建立pbxproj文件,下面是本身用ruby生成project文件的一部分示例代碼:xcode
#建立 Example.xcodeproj工程文件,並保存 Xcodeproj::Project.new("./Example.xcodeproj").save #打開建立的Example.xcodeproj文件 proj=Xcodeproj::Project.open("./Example.xcodeproj") #建立一個分組,名稱爲Example,對應的路徑爲./Example exampleGroup=proj.main_group.new_group("Example","./Example") #給Example分組添加文件引用 exampleGroup.new_reference("AppDelegate.h") ref1=exampleGroup.new_reference("AppDelegate.m") ref2=exampleGroup.new_reference("Images.xcassets") exampleGroup.new_reference("Base.lproj/LaunchScreen.xib") #在Example分組下建立一個名字爲Supporting Files的子分組,並給該子分組添加main和info.plist文件引用 supportingGroup=exampleGroup.new_group("Supporting Files") ref3=supportingGroup.new_reference("main.m") supportingGroup.new_reference("Info.plist") #建立target,主要的參數 type: application :dynamic_library framework :static_library 意思你們都懂的 #name:target名稱 #platform:平臺 :ios或者:osx target = proj.new_target(:application,"Example",:ios) #添加target配置信息 target.build_configuration_list.set_setting('INFOPLIST_FILE', "$(SRCROOT)/Example/Info.plist") #target添加相關的文件引用,這樣編譯的時候才能引用到 target.add_file_references([ref1,ref2,ref3]) testGroup=proj.main_group.new_group("ExampleTests","./ExampleTests") ref4=testGroup.new_reference("ExampleTests.m") supportingGroup=testGroup.new_group("Supporting Files") supportingGroup.new_reference("Info.plist") #建立test target testTarget = proj.new_target(:unit_test_bundle,"ExampleTests",:ios,nil,proj.products_group) testRefrence = testTarget.product_reference testRefrence.set_explicit_file_type('wrapper.cfbundle') testRefrence.name = "ExampleTests.xctest" testTarget.add_file_references([ref4]) #保存 proj.save
經過上面的腳本生產的工程以下:
ruby
因爲對ruby不太屬性,所以有錯誤的地方歡迎提出!另附地址:IOS持續集成app