最近学习了一下 Shell 脚本,对 Unix 很多命令更熟悉了,也学到了很多新命令。
简单使用 xcodebuild 来写了一个打包脚本,流程是打包、导出 ipa、上传到蒲公英 / Bugly 之类的测试网站,通知 Slack。
archive
打包和导出 ipa 都是使用这个命令,可以使用 man xcodebuild 来查看详细用法。
其中打包使用的是 archive 这个命令,一般 archive 完导出一个 .xcarchive 类型的文件,这需要我们指定路径。
你打包的项目是 project 还是 workspace 也要指定,使用 -project <projectName> 或者 -workspace <workspaceName>。
还有一个就是要指定 scheme,使用 -scheme <schemeName>。这个 scheme 有个地方要注意,如果 scheme name 有空格则要用转义,即 -scheme test\ dev。这里要小吐槽一下,不建议 scheme 取有空格的名字,可能会有各种转义问题。
所以打包命令就是这样,其他选项默认即可。
1
| xcodebuild archive -archivePath ~/Desktop/tmp.xcarchive -workspace test.xcworkspace -scheme test\ dev
|
export
打完包则要导出 ipa 文件,也是使用 xcodebuild,使用的命令是 -exportArchive。
要指定打包的路径,即刚才打包出来的 *.xcarchive 文件,使用 -archivePath ~/Desktop/tmp.xcarchive。
然后就是导出 ipa 文件的路径。
最后就是要指定一些选项。
<.plist path>1 2 3 4 5 6
| ``` 需要指定一个 plist 文件的路径。 例如使用 Xcode 导出的时候会让你选择以什么方法打包。
不指定的话一般默认 `development`,还有 `ad-hoc`、`app-store`、`package`、`enterprise`、`developer-id` 等选项。 还有 teamID 是要指定你的开发者账号的 team id。
|
xcodebuild -showBuildSettings -workspace test.xcworkspace -scheme testScheme |grep DEVELOPMENT_TEAM
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 使用这个命令可以查看到,也可以上开发者官网查看。 指定的 plist 文件就是这样:
```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> <plist version="1.0"> <dict> <key>teamID</key> <string>${teamID}</string> <key>method</key> <string>${method}</string> </dict> </plist>
|
上传
这里我上传的是蒲公英,使用 Bugly 也是差不多的。
使用的是 curl 命令,ipaPath 即指定你的 ipa 路径,然后添加你的 user key 和 api key 就行了。
1
| curl -F "file=@$ipaPath" -F "uKey=<your user key>" -F "_api_key=<your api key>" https://qiniu-storage.pgyer.com/apiv1/app/upload
|
通知 Slack
我使用的也是 curl 命令,最近经常用这个命令,感觉很好用。
1
| curl -X POST --data-urlencode 'payload={"channel": "@ascen", "username": "iOS", "text": "archive '${scheme}' <https://www.pgyer.com/****|你想写的链接文字>.", "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/T2A8E9XSP/B50F24ZRS/**你的url**
|
所以最后 Shell script 是这样的:
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
| #!/bin/sh #!/bin/bash
currentPath=$(pwd)
# scheme="testScheme" teamID="your team id" method="development" # project path cd ~/Desktop/your project path
#
xcodebuild clean
xcws=$(echo *.xcworkspace) xcodebuild archive -archivePath ~/Desktop/tmp.xcarchive -workspace $xcws -scheme $scheme
exportOptions="<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> <plist version="1.0"> <dict> <key>teamID</key> <string>${teamID}</string> <key>method</key> <string>${method}</string> </dict> </plist>"
echo $exportOptions > ~/Desktop/exportOptions.plist
xcodebuild -exportArchive -archivePath ~/Desktop/tmp.xcarchive -exportPath ~/Desktop/ -exportOptionsPlist ~/Desktop/exportOptions.plist
# delete tmp file rm -rf ~/Desktop/tmp.xcarchive rm ~/Desktop/exportOptions.plist
ipaPath="/Users/Ascen/Desktop/${scheme}.ipa" echo $ipaPath
curl -F "file=@$ipaPath" -F "uKey=<your user key>" -F "_api_key=<your api key>" https://qiniu-storage.pgyer.com/apiv1/app/upload
curl -X POST --data-urlencode 'payload={"channel": "@ascen", "username": "iOS", "text": "archive '${scheme}' <https://www.pgyer.com/shundaochuxing|乘客端>.", "icon_emoji": ":ghost:"}' https://your slack url
rm $ipaPath
cd $currentPath
|