IOS编译打包提交自动化
该操作是指:首先对Xcode的Project或Workspace进行编译,编译通过的应用会生成app文件,为了提供给测试使用,我们需要将.app包转为.ipa包
实现方法有两种,AppleScript和Shell脚本
Shell脚本下一个值得参考的样例是webfrogs/xcode_shell
基础知识
工具:
- xcodebuild:主要用来编译、打包成Archive和导出ipa包
- xcrun:用于打包
- altool:ApplicationLoader,提交应用
- fir-cli:上传到fir
- PlistBuddy:读写plist文件
xcodebuild
和xcrun
都来自Command Line Tools
,需要在Xcode或终端中安装。
Shell实现
- ipa-build:编译xcode工程并生成ipa文件
- ipa-publish:生成符合itms—services协议的文件,并发不到服务器
- sendEmail:stmp发布email的脚本。(可以改成Applescipt来实现)
- sftpDownloadFile:用过sftp协议下载文件
- sftpUploadFile:通过sftp协议上传文件
- updateLocalIndexHtml:对索引文件进行处理(二进制文件,非shell脚本)
- uploadItemsServicesFiles:将itms-services协议文件上传到服务器
ipa-build
使用方法:
ipa-build 脚本绝对路径 参数1 参数2
参数1为IOS工程的跟路径;参数2可选,为编译是工程configuration的类型:Debug、AdHoc、Release、Distribution,默认为Release。
ipa-build脚本运行后,会在IOS工程根路径下生成名为“build”的文件夹,在这个文件夹中又有一个名为“ipa-build”的文件夹,打包所生成的最新ipa包就在其中。
源码分析
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#--------------------------------------------
# 功能:编译xcode项目并打ipa包
# 使用说明:
# 编译project
# ipa-build <project directory> [-c <project configuration>] [-o <ipa output directory>] [-t <target name>] [-n] [-p <platform identifier>]
# 编译workspace
# ipa-build <workspace directory> -w -s <schemeName> [-c <project configuration>] [-n]
#
# 参数说明:-c NAME 工程的configuration,默认为Release。
# -o PATH 生成的ipa文件输出的文件夹(必须为已存在的文件路径)默认为工程根路径下的”build/ipa-build“文件夹中
# -t NAME 需要编译的target的名称
# -w 编译workspace
# -s NAME 对应workspace下需要编译的scheme
# -n 编译前是否先clean工程
# -p 平台标识符
# 作者:ccf
# E-mail:ccf.developer@gmail.com
# 创建日期:2012/09/24
# 参数个数不能小于1
if [ $# -lt 1 ];then
echo "Error! Should enter the root directory of xcode project after the ipa-build command."
exit 2
fi
# 判断第一个参数提供的目录是否存在,其他命令有-f,-s,-e
if [ ! -d $1 ];then
echo "Error! The first param must be a directory."
exit 2
fi
# 工程绝对路径
cd $1
# 当前目录的全路径名称
project_path=$(pwd)
#编译的configuration,默认为Release
build_config=Release
# 设置参数名
param_pattern=":p:nc:o:t:ws:"
OPTIND=2
# 遍历参数
while getopts $param_pattern optname
do
case "$optname" in
"n")
# 是否清理工程
should_clean=y
;;
"p")
tmp_optind=$OPTIND
tmp_optname=$optname
tmp_optarg=$OPTARG
OPTIND=$OPTIND-1
if getopts $param_pattern optname ;then
echo "Error argument value for option $tmp_optname"
exit 2
fi
OPTIND=$tmp_optind
# 平台标志符
platform_id=$tmp_optarg
;;
"c")
tmp_optind=$OPTIND
tmp_optname=$optname
tmp_optarg=$OPTARG
OPTIND=$OPTIND-1
if getopts $param_pattern optname ;then
echo "Error argument value for option $tmp_optname"
exit 2
fi
OPTIND=$tmp_optind
# 设定工程的configuration
build_config=$tmp_optarg
;;
"o")
tmp_optind=$OPTIND
tmp_optname=$optname
tmp_optarg=$OPTARG
OPTIND=$OPTIND-1
if getopts $param_pattern optname ;then
echo "Error argument value for option $tmp_optname"
exit 2
fi
OPTIND=$tmp_optind
cd $tmp_optarg
# 输出文件夹
output_path=$(pwd)
cd ${project_path}
if [ ! -d $output_path ];then
echo "Error!The value of option o must be an exist directory."
exit 2
fi
;;
"w")
workspace_name='*.xcworkspace'
# 查看该路径下是否有xcworkspace文件
ls $project_path/$workspace_name &>/dev/null
rtnValue=$?
if [ $rtnValue = 0 ];then
build_workspace=$(echo $(basename $project_path/$workspace_name))
else
echo "Error!Current path is not a xcode workspace.Please check, or do not use -w option."
exit 2
fi
;;
"s")
tmp_optind=$OPTIND
tmp_optname=$optname
tmp_optarg=$OPTARG
OPTIND=$OPTIND-1
if getopts $param_pattern optname ;then
echo "Error argument value for option $tmp_optname"
exit 2
fi
OPTIND=$tmp_optind
# workspace下需要编译的scheme
build_scheme=$tmp_optarg
;;
"t")
tmp_optind=$OPTIND
tmp_optname=$optname
tmp_optarg=$OPTARG
OPTIND=$OPTIND-1
if getopts $param_pattern optname ;then
echo "Error argument value for option $tmp_optname"
exit 2
fi
OPTIND=$tmp_optind
# 编译目标的名称
build_target=$tmp_optarg
;;
"?")
echo "Error! Unknown option $OPTARG"
exit 2
;;
":")
echo "Error! No argument value for option $OPTARG"
exit 2
;;
*)
# Should not occur
echo "Error! Unknown error while processing options"
exit 2
;;
esac
done
#build文件夹路径
build_path=${project_path}/build
#生成的app文件目录
appdirname=Release-iphoneos
if [ $build_config = Debug ];then
appdirname=Debug-iphoneos
fi
if [ $build_config = Distribute ];then
appdirname=Distribute-iphoneos
fi
#编译后文件路径(仅当编译workspace时才会用到)
compiled_path=${build_path}/${appdirname}
#是否clean
if [ "$should_clean" = "y" ];then
xcodebuild clean -configuration ${build_config}
fi
#组合编译命令
build_cmd='xcodebuild'
if [ "$build_workspace" != "" ];then
#编译workspace
if [ "$build_scheme" = "" ];then
echo "Error! Must provide a scheme by -s option together when using -w option to compile a workspace."
exit 2
fi
build_cmd=${build_cmd}' -workspace '${build_workspace}' -scheme '${build_scheme}' -configuration '${build_config}' CONFIGURATION_BUILD_DIR='${compiled_path}' ONLY_ACTIVE_ARCH=NO'
else
#编译project
build_cmd=${build_cmd}' -configuration '${build_config}
if [ "$build_target" != "" ];then
build_cmd=${build_cmd}' -target '${build_target}
fi
fi
#编译工程
cd $project_path
$build_cmd || exit
#############################################################
#进入build路径
cd $build_path
#创建ipa-build文件夹
if [ -d ./ipa-build ];then
rm -rf ipa-build
fi
mkdir ipa-build
#app文件名称
appname=$(basename ./${appdirname}/*.app)
#通过app文件名获得工程target名字
target_name=$(echo $appname | awk -F. '{print $1}')
#app文件中Info.plist文件路径
app_infoplist_path=${build_path}/${appdirname}/${appname}/Info.plist
#取版本号
bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" ${app_infoplist_path})
#取build值
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" ${app_infoplist_path})
#取displayName
displayName=$(/usr/libexec/PlistBuddy -c "print CFBundleDisplayName" ${app_infoplist_path})
#IPA名称
ipa_name="${displayName}_${platform_id}_${bundleShortVersion}_${build_config}_${bundleVersion}_$(date +"%Y%m%d")"
echo $ipa_name
#xcrun打包
xcrun -sdk iphoneos PackageApplication -v ./${appdirname}/*.app -o ${build_path}/ipa-build/${ipa_name}.ipa || exit
if [ "$output_path" != "" ];then
cp ${build_path}/ipa-build/${ipa_name}.ipa $output_path/${ipa_name}.ipa
echo "Copy ipa file successfully to the path $output_path/${ipa_name}.ipa"
fi
AppleScript实现
相比shell,appleScript的简易性以及对mac OS系统的接口支持使其能够更好的在mac下工作,虽然该语言已经渐渐被大家遗忘,但并不表示它不work,所以我打算使用applescript对上述流程进行复现,并非简单的抄袭,而是根据自身需要以及applescript的特性进行改进,部分实现如下:
applescript的存在形式有三种,一种是文本文件,与shell类似,在首行定义#!/usr/bin/osascript
便可以在终端下运行;另一种是应用程序,通过双击来运行;还有一种是名为Droplet的应用程序,其特点是可以获取所有拖动到该应用图标上的文件的信息(支持多文件),于是我选择用第三种方式来传递工程路径,其他配置则设为默认值,实现一次拖动,完成自动编译和打包的过程。
拖动操作的Handle
文件拖动到应用图标上方是通过Handle监听实现的:
1 | on open these_items |
每个this_item为alias类型,代表其中一个被拖进图标的文件夹。
待续…