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

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

pbxproj

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

  • PBXBuildFile PBXFileReference
    这两个section保存中工程文件相关的信息:包含文件的类型,路径,名称等
1
2
3
4
5
6
7
/* 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:
1
2
3
4
5
6
7
8
9
10
11
12
* 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的对应的配置信息、创建规则、依赖、名称和类型等信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* 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的搜索路径等信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 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文件的一部分示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #创建 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不太熟悉,因此有错误的地方欢迎提出!