美文网首页iOS开发iOS老中医
iOS持续集成:命令行创建工程

iOS持续集成:命令行创建工程

作者: 树下老男孩 | 来源:发表于2015-05-19 21:43 被阅读2586次

    在开发iOS应用的时候,大部分都是直接采用Xcode进行开发,但有时候需要用命令行来创建工程,比如最近在做ci的持续集成,就只能通过命令行的方式,这时候就需要了解一下工程文件的构成。我们知道工程文件的相关信息保存在project.pbxproj,因此可以通过脚本创建出pbxproj文件,完成基础工程的创建。

    pbxproj

    下面介绍一下pbxproj文件,可以拖动.xcodeproj文件到文本编辑器,如sublime,查看pbxproj文件的组成方式,主要包括:

    • PBXBuildFile PBXFileReference
      这两个section保存中工程文件相关的信息:包含文件的类型,路径,名称等
    /* 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 */
    
    • PBXGroup
      这个section保存着工程文件的分组信息:分组的名称,以及该组内含有的文件,比如下面的例子,一个TestChat分组里面还有一个Supporting Files子分组,同时该组包含AppDelegate的.h和.m两个文件,该分组对应的路径为TestChat:
    * Begin PBXGroup section */
            F62417EA14D52F3C003CE997 /* TestChat */ = {
                isa = PBXGroup;
                children = (
                    F62417EB14D52F3C003CE997 /* Supporting Files */,
                    F62417F314D52F3C003CE997 /* AppDelegate.h */,
                    F62417F414D52F3C003CE997 /* AppDelegate.m */,
                );
                path = TestChat;
                sourceTree = "<group>";
            };
    /* End PBXGroup section */
    
    • PBXNativeTarget
      该section保存工程创建的target信息:包含target的对应的配置信息、创建规则、依赖、名称和类型等信息
    /* 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 */
    
    
    • XCBuildConfiguration XCConfigurationList
      这两个section保存着工程相关的配置信息:下面对应的是debug模式下的配置信息,可以看到里面包含CODE_SIGN_IDENTITY,sdk,framework的搜索路径等信息。
    /* 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以及对应的配置信息就行了。

    命令行生成pbxproj

    上面大概了解了一个pbxproj文件的整体构造,要想生成一个这样的文件,可以自己按照对应的格式书写,或者借助一些脚本工具,这里推荐cocoapods的Xcodeproj,项目的地址:Xcodeproj ,该工具是用ruby语言实现的,可以用它来修改和创建pbxproj文件,下面是自己用ruby生成project文件的一部分示例代码:

      #创建 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不太熟悉,因此有错误的地方欢迎提出!

    相关文章

      网友评论

      • 踩坑小分队:怎么添加 .a 文件的引用?
      • ahyin007:如何添加静态库文件呢
      • LJ8116:有个地方是不是要改一下,给target添加代码文件是add_file_reference, 添加资源文件应该是add_resources
        文中这里:
        #target添加相关的文件引用,这样编译的时候才能引用到
        target.add_file_references([ref1,ref2,ref3])

        应该改一下
      • 1f36bf1fd7d4:非常感谢作者的文章,帮了很大忙,另外请教一下像是单元测试的target通常都要依赖于需测试的APP,怎么制定它们二者的关系呢?
      • 9211dd8025c6:能够通过修改这个文件达到批量新建target的目的吗???

      本文标题:iOS持续集成:命令行创建工程

      本文链接:https://www.haomeiwen.com/subject/onylqttx.html