[Mac]shell文件中读取plist文件并实现shell中的数值计算

2018年01月14日 iOS 6条评论 阅读8828次

[Mac]shell文件中读取plist文件并实现shell中的数值计算

有篇文章在说明Mac下iOS工程自动打包的shell文件中有用到动态写iOS工程下的plist文件。

iOS工程下的Info.plist文件中有个CFBundleVersion的键,该键对应的值一般认为是内部维护的版本号,这个内部是相对于CFBundleShortVersionString的外部给用户看的版本号而说的。

关于CFBundleVersion应该如何设置,方案有很多,有些项目使用VCS(版本控制系统)中本次提交的版本号,有些项目使用日期,有些从1开始每次累计。

个人习惯,一般都是用从1开始累加的方式给CFBundleVersion设置值。鉴于此场景,shell中就必须要有读取上次plist中CFBundleVersion值并加1后跟新plist文件。

三步走:1、读取上次的CFBundleVersion;2、CFBundleVersion加1;3、给CFBundleVersion写入新加1后的值

1、读取上次的CFBundleVersion

plist操作还是用PlistBuddy, 位置在/usr/libexec/PlistBuddy。
读属性值是用Print命令,单纯读出来在终端打印的话很简单,

/usr/libexec/PlistBuddy -c "Print CFBundleVersion" JILENIAO/Infp.plist


但要赋值给变量,就要稍微加工一下了。

BUILD_CODE=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" JILENIAO/Infp.plist)


这样再echo $BUILD_CODE 就能输出plist文件中的内部版本号了。

2、CFBundleVersion加1
Mac下shell中的计算和其他语言稍有不同。加减乘除的话写+-*/是会被当做为字符串的,想让shell知道你是个表达式计算的话,需要expr处理。

BUILD_CODE=`expr $BUILD_CODE + 1`

注意:expr 中运算符前后必须要有一个空格,我最初忽略掉空格, 结果是死活不出来想要的加法结果。

3、给CFBundleVersion写入新加1后的值

这个就简单了,一个PlistBuddy的Set命令搞定

/usr/libexec/PlistBuddy -c "Set CFBundleVersion $BUILD_CODE" JILENIAO/Infp.plist


分享本次更新后的自动编译打包ipa的shell文件。



#!/bin/bash
#Author Jileniao.Net
SCHEMENAME='JILENIAO'
DATE=`date +%Y%m%d_%H%M`
SOURCEPATH=$( cd "$( dirname $0 )" && pwd)
IPAPATH=$SOURCEPATH
DISNAME=$SCHEMENAME"_"$DATE
ARCHNAME=$DISNAME.xcarchive
IPANAME=$DISNAME
ExportOptions='ExportOptions.plist'
INFOPLIST=$SCHEMENAME/Info.plist
BUNDLE_ID='net.jileniao.iosblog'
DISPLAY_NAME='极乐鸟'
VERSION_NAME='1.0.5'
#BUILD_CODE='153'
PROVISION_PROFILE='jileniao-dis'
# 测试版APP
if [ -n "$1" ]; then
  # extra parameter 1
fi
rm -rf ./build
rm main.jsbundle
rm main.jsbundle.meta
rm -rf ./Build
rm -rf ./ModuleCache
rm -rf ./Logs
xcodebuild clean
CURR=`pwd`
cd .. && react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios/
cd $CURR
# 先读取之前的BUILD_CODE,加1得到新的BUILD_CODE
BUILD_CODE=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $INFOPLIST)
BUILD_CODE=`expr $BUILD_CODE + 1`
# 设置编译参数
/usr/libexec/PlistBuddy -c "Set CFBundleDisplayName $DISPLAY_NAME" $INFOPLIST
/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString $VERSION_NAME" $INFOPLIST
/usr/libexec/PlistBuddy -c "Set CFBundleVersion $BUILD_CODE" $INFOPLIST
/usr/libexec/PlistBuddy -c "Set CFBundleIdentifier $BUNDLE_ID" $INFOPLIST
/usr/libexec/PlistBuddy -c "Delete provisioningProfiles" $ExportOptions
/usr/libexec/PlistBuddy -c "Add provisioningProfiles dict" $ExportOptions
/usr/libexec/PlistBuddy -c "Add provisioningProfiles:$BUNDLE_ID string $PROVISION_PROFILE" $ExportOptions
# 构建
xcodebuild archive \
 -project JILENIAO.xcodeproj \
 -scheme $SCHEMENAME \
 -configuration Release \
 -archivePath $ARCHNAME \
 clean \
 build \
 -derivedDataPath ./
if [ -e $ARCHNAME ]; then
  echo "xcodebuild archive Successful"
else
  echo "xcodebuild archive Failed"
  exit 1
fi
# 导出ipa
xcodebuild -exportArchive \
 -archivePath $ARCHNAME \
 -exportPath $IPANAME \
 -exportOptionsPlist $ExportOptions
if [ -e $IPANAME ]; then
  echo "Export ipa Successful"
  open $IPAPATH
else
  echo "Export ipa Failed"
  exit 1
fi
#删除临时文件
rm -rf $ARCHNAME
rm -rf ./Build
rm -rf ./ModuleCache
rm -rf ./Logs



分享本文至:

WRITTEN BY

avatar
本文标签:shellmacPlistBuddy
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

6 条评论

  1. avatar giraprendi

    <a href="http://www.ibadoracao.com/hommes-nike-roshe-run-triangle-or-vert-trainersce">hommes nike roshe run triangle or vert</a> <a href="http://www.agilitytel.com/nike-free-3.0-m%C3%A4nner-blau-grau-gr%C3%BCn-schuhek">nike free 3.0 m盲nner blau grau gr眉n</a> <a href="http://www.tokhasan.com/herren-nike-air-huarache-nm-grau-rot-scarpeg">herren nike air huarache nm grau rot</a> <a href="http://www.larosepalmer.com/nike-tiempo-legend-5-negro-and-blanco-kurpesb">nike tiempo legend 5 negro and blanco</a> <a href="http://www.applepietv.com/nike-roshe-run-burgundy-and-blanco-zapatosk">nike roshe run burgundy and blanco</a> <a href="http://www.trianglescion.com/jordan-retro-4-rosa-lilla-runningc">jordan retro 4 rosa lilla</a>
    giraprendi http://www.giraprendi.com/

  2. avatar qiefun

    <a href="http://www.muebleactual.com/authentic-houston-colt-45s-jersey-nflh">authentic houston colt 45s jersey</a> <a href="http://www.xfullfilmizle.com/adidas-bbneo-skool-low-canvas-black-shoesh">adidas bbneo skool low canvas black</a> <a href="http://www.mycryptoverse.com/nike-air-pegasus-marine-bleu-chaussuresl">nike air pegasus marine bleu</a> <a href="http://www.blokistand.com/nike-air-max-90-jcrd-men-grey-sky-blue-white-shoesj">nike air max 90 jcrd men grey sky blue white</a> <a href="http://www.wadeesley.com/north-carolina-state-wolfpack-new-era-2017-ncaa-sport-knit-hatk">north carolina state wolfpack new era 2017 ncaa sport knit</a> <a href="http://www.dermonenc.com/kvinders-nike-air-max-tn-gr%C3%B8n-guld-skoj">kvinders nike air max tn gr酶n guld</a>
    qiefun http://www.qiefun.com/

  3. avatar khzrubber

    <a href="http://www.mumbaikichut.com/nike-air-force-1-blazer-azul-zapatosk">nike air force 1 blazer azul</a> <a href="http://www.ikmsdkravmaga.com/adidas-neo-suede-lyser%C3%B8d-gr%C3%A5-skoi">adidas neo suede lyser酶d gr氓</a> <a href="http://www.themusinglens.com/kvinners-air-yeezy-ii-2-sp-max-90-gr%C3%A5-r%C3%B8d-obuvl">kvinners air yeezy ii 2 sp max 90 gr氓 r酶d</a> <a href="http://www.startagencia.com/nike-kobe-8-yellow-sky-blue-shoesh">nike kobe 8 yellow sky blue</a> <a href="http://www.drjennieward.com/adidas-stan-smith-blu-nero-scarpei">adidas stan smith blu nero</a> <a href="http://www.genievres.com/herren-nike-air-max-1-rosa-grau-schuhej">herren nike air max 1 rosa grau</a>
    khzrubber http://www.khzrubber.com/

  4. avatar mustberavi

    <a href="http://www.taiwanmeinet.com/nike-air-max-flair-2017-women-purple-black-white-sporting-shoesk">nike air max flair 2017 women purple black white sporting</a> <a href="http://www.talkwithgirl.com/ray-ban-justin-sunglasses-for-sale-sunglassesh">ray ban justin sunglasses for sale</a> <a href="http://www.mamatale.com/adidas-15.1-fg-jaune-violet-orange-chaussuresl">adidas 15.1 fg jaune violet orange</a> <a href="http://www.strongtale.com/adidas-springblade-drive-2.0-blanc-bleu-chaussuresl">adidas springblade drive 2.0 blanc bleu</a> <a href="http://www.iranpayamco.com/schwarz-air-jordan-12-retro-schuhej">schwarz air jordan 12 retro</a> <a href="http://www.hapeid.com/asics-gel-lyte-3-gr%C3%BCn-lila-grau-schuhei">asics gel lyte 3 gr眉n lila grau</a>
    mustberavi http://www.mustberavi.com/

  5. avatar ayakkabiyeri

    <a href="http://www.investicrypto.com/adidas-originals-zx-750-svart-r%C3%B8d-obuvj">adidas originals zx 750 svart r酶d</a> <a href="http://www.nattaravnur.com/unsex-adidas-ultra-boost-in-esecuzione-scarpa-vino-rosso-scarpel">unsex adidas ultra boost in esecuzione scarpa vino rosso</a> <a href="http://www.uacheap.com/nike-air-max-90-hommes-noir-blanc-chaussuresh">nike air max 90 hommes noir blanc</a> <a href="http://www.sheeradiance.com/nike-cortez-argento-blu-scarpeh">nike cortez argento blu</a> <a href="http://www.solclimasur.com/kyrie-irving-home-jersey-nfli">kyrie irving home jersey</a> <a href="http://www.successlms.com/nike-mercurial-superfly-v-fg-azul-blanco-negro-zapatosh">nike mercurial superfly v fg azul blanco negro</a>
    [url=http://www.ayakkabiyeri.com/]ayakkabiyeri[/url]

  6. avatar mypotatocrop

    <a href="http://www.drcooper.net/adidas-zx-flux-og-svart-elemennts-schuhee">adidas zx flux og svart elemennts</a> <a href="http://www.drinkableair.net/nike-air-force-1-low-marr%C3%B3n-rojo-kurpesc">nike air force 1 low marr贸n rojo</a>
    mypotatocrop http://www.mypotatocrop.net/

欢迎留言




用户登录

sitemap