经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
2020年iOS自动打包脚本
来源:cnblogs  作者:fengsh_h  时间:2020/12/14 17:18:45  对本文有异议

2020年iOS自动打包脚本

1.准备工作

安装fastlane
至于fastlane是做什么的就不多说了,自行百度;

  1. sudo gem install fastlane --verbose

安装成功后没有修改gem source 的修改一下,我用的是https://gems.ruby-china.com/
终端打开项目目录执行fastlane init, fastlane init执行过程中需要选择一些东西,如What would you like to use fastlane for? 并把Gemfile中的source修改成你的gem source的源,

  1. wk@wakedatadeMacBook-Pro uniapp-yuexiuhui-iOS % fastlane init
  2. [?] ??
  3. [?] Looking for iOS and Android projects in current directory...
  4. [15:40:54]: Created new folder './fastlane'.
  5. [15:40:54]: Detected an iOS/macOS project in the current directory: 'uniapp-yuexiuhui-iOS.xcworkspace'
  6. [15:40:55]: -----------------------------
  7. [15:40:55]: --- Welcome to fastlane ?? ---
  8. [15:40:55]: -----------------------------
  9. [15:40:55]: fastlane can help you with all kinds of automation for your mobile app
  10. [15:40:55]: We recommend automating one task first, and then gradually automating more over time
  11. [15:40:55]: What would you like to use fastlane for?
  12. /usr/local/Cellar/fastlane/2.168.0/libexec/gems/highline-1.7.10/lib/highline.rb:624: warning: Using the last argument as keyword parameters is deprecated
  13. 1. ?? Automate screenshots
  14. 2. ????? Automate beta distribution to TestFlight
  15. 3. ?? Automate App Store distribution
  16. 4. ?? Manual setup - manually setup your project to automate your tasks
  17. ? 这个地方输入你要上传到那个平台,一般选234

下一步需要提醒你输入的是你的iOS开发者账号

  1. [15:44:09]: To use App Store Connect and Apple Developer Portal features as part of fastlane,
  2. [15:44:09]: we will ask you for your Apple ID username and password
  3. [15:44:09]: This is necessary for certain fastlane features, for example:
  4. [15:44:09]:
  5. [15:44:09]: - Create and manage your provisioning profiles on the Developer Portal
  6. [15:44:09]: - Upload and manage TestFlight and App Store builds on App Store Connect
  7. [15:44:09]: - Manage your App Store Connect app metadata and screenshots
  8. [15:44:09]:
  9. [15:44:09]: Your Apple ID credentials will only be stored in your Keychain, on your local machine
  10. [15:44:09]: For more information, check out
  11. [15:44:09]: https://github.com/fastlane/fastlane/tree/master/credentials_manager
  12. [15:44:09]:
  13. [15:44:09]: Please enter your Apple ID developer credentials
  14. /usr/local/Cellar/fastlane/2.168.0/libexec/gems/highline-1.7.10/lib/highline.rb:624: warning: Using the last argument as keyword parameters is deprecated
  15. [15:44:09]: Apple ID Username:

然后是iOS开发者账号的密码,如果你的账号下面有多个企业或组织,然后是选择证书然后就配置完了。
然后在你的项目目录中会多一个fastlane文件夹,一个Gemfile,Gemfile中需要修改source就可以了。fastlane中有两个文件,一个Appfile一个Fastfile,Appfile保存的是iOS开发者账号的信息和证书信息,Fastfile中是自动打包的脚本,对应纯原生的代码这样基本就可以了;
而对于现在混口开发盛行的时代,我们项目基本都是uni-app写的所以才需要再写一些脚本,开发就是越来越懒,一步也不喜欢手动去做,那么脚本的用处就大了;

2.写脚本

  1. 打包到蒲公英的脚本
  1. #!/bin/bash
  2. env=$1
  3. buildUpdateDescription=$2
  4. ipa_name="项目名"
  5. api_key="蒲公英的api_key"
  6. if [ ! -n "$1" ] ;then
  7. env="build"
  8. fi
  9. # 向iOS的Info.plist里面写值
  10. # environment="production"
  11. # if [[ $env = "dev" ]]; then
  12. # environment="development"
  13. # fi
  14. # path=$(cd `dirname $0`; pwd)
  15. # target_plist="$path/uniapp-yuexiuhui-iOS/Info.plist"
  16. # echo $target_plist
  17. # for plist in "$target_plist"; do
  18. # if [[ -f "$plist" ]]; then
  19. # /usr/libexec/PlistBuddy -c "Set :Environment $environment" "$plist"
  20. # fi
  21. # done
  22. # 修改YXHCommon.m文件,这个的目的主要是切换生产环境与测试环境的,修改的值主要用于条件编译
  23. cd uniapp-yuexiuhui-iOS/utils
  24. if [[ "dev" = $env ]]; then
  25. sed -i '' 's/#if false/#if true/g' YXHCommon.m
  26. else
  27. sed -i '' 's/#if true/#if false/g' YXHCommon.m
  28. fi
  29. cd ../..
  30. cd ../uniapp-yuexiuhui-vue
  31. # 而下面的npm run xxx这个主要是HbuilderX 打包脚本
  32. if [ "dev" = $env ]
  33. then
  34. npm run dev:app-plus
  35. else
  36. npm run build:app-plus
  37. fi
  38. echo '------覆盖www------'
  39. cp -R dist/$env/app-plus/ ../$ipa_name/$ipa_name/Pandora/apps/__UNI__E59AA60/www
  40. cd ../uniapp-yuexiuhui-iOS
  41. fastlane pgy
  42. cd ./build
  43. export_ipa_path=$(cd `dirname $0`; pwd)
  44. #上传ipa到蒲公英 我自己加的
  45. if [ -f "${export_ipa_path}/${ipa_name}.ipa" ]
  46. then
  47. echo '开始上传ipa/apk到蒲公英'
  48. result=$(curl -F "file=@${export_ipa_path}/${ipa_name}.ipa" -F "_api_key=${api_key}" -F "buildUpdateDescription=${buildUpdateDescription}" 'http://www.pgyer.com/apiv2/app/upload')
  49. else
  50. echo "在目录:${export_ipa_path}/${ipa_name}.ipa 不存在"
  51. fi
  52. echo '上传到蒲公英成功'
  53. exit 0

2.上传到TestFlight的脚本

  1. #!/bin/bash
  2. env=$1
  3. if [ ! -n "$1" ] ;then
  4. env="build"
  5. fi
  6. cd uniapp-yuexiuhui-iOS/utils
  7. if [[ "dev" = $env ]]; then
  8. sed -i '' 's/#if false/#if true/g' YXHCommon.m
  9. else
  10. sed -i '' 's/#if true/#if false/g' YXHCommon.m
  11. fi
  12. cd ../..
  13. cd ../uniapp-yuexiuhui-vue
  14. if [ "dev" = $env ]
  15. then
  16. npm run dev:app-plus
  17. else
  18. npm run build:app-plus
  19. fi
  20. echo '------覆盖www------'
  21. cp -R dist/$env/app-plus/ ../uniapp-yuexiuhui-iOS/uniapp-yuexiuhui-iOS/Pandora/apps/__UNI__E59AA60/www
  22. cd ../uniapp-yuexiuhui-iOS
  23. fastlane beta
  24. exit 0

3.如果运行脚本报错请检查shell语法,当然你也可以用Python写
如有什么疑问可以联系我:
WeChat:fengsh_h
e-mail: fengsh_h@aliyun.com

原文链接:http://www.cnblogs.com/fshmjl/p/14119071.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号