在开发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