多target配置描述文件自动修改匹配描述文件打包ipa

前言

在上一篇讲述了多target项目在Xcode里配置好各个target的描述文件及设置后自动打包,现在进一步对每个target指定mobileprovision文件自动修改内容后再自动打包

#!/bin/bash

#定义要查找的target
find_target="xxx"
#要解析的xcodeproj
src_root_path="/xxx/xxx"
#SRCROOT=$src_root_path
xcodeproj_path="$src_root_path/xxx.xcodeproj"
#项目配置文件本质上也是一个xml,所以可以用/usr/libexec/PlistBuddy进行读和写
pbxproj_path="$xcodeproj_path/project.pbxproj"
xcworkspace_path="$src_root_path/xxx.xcworkspace"
mobileprovision_path="/xxx/xxx/xxx.mobileprovision"


#将描述文件解析成plist文件
temp_profile_path="./temp_profile.plist"
security cms -D -i "$mobileprovision_path" > $temp_profile_path

#创建临时保存变量的文件
tempVarFile="./tempVarFile.txt"
if [ ! -f $tempVarFile ];then
touch $tempVarFile
else
  rm -f $tempVarFile
  touch $tempVarFile
fi

#证书持有者机构
config_TeamName=$(/usr/libexec/PlistBuddy -c "print TeamName" $temp_profile_path)
echo "config_TeamName='${config_TeamName}'" >> $tempVarFile
#证书持有者组Id
config_teamId=$(/usr/libexec/PlistBuddy -c "print :Entitlements:com.apple.developer.team-identifier" $temp_profile_path)
echo "config_teamId='${config_teamId}'" >> $tempVarFile
#bundleId
config_identifier=$(/usr/libexec/PlistBuddy -c "print :Entitlements:application-identifier" $temp_profile_path)
config_identifier_cut="${config_teamId}."
config_identifier=${config_identifier#*$config_identifier_cut}
echo "config_identifier='${config_identifier}'" >> $tempVarFile
#发布证书名称
config_profile_name=$(/usr/libexec/PlistBuddy -c "print Name" $temp_profile_path)
echo "config_profile_name='${config_profile_name}'" >> $tempVarFile
#描述文件UUID
config_profile_uuid=$(/usr/libexec/PlistBuddy -c "print UUID" $temp_profile_path)
echo "config_profile_uuid='${config_profile_uuid}'" >> $tempVarFile
#过期时间
config_expirationDate=$(/usr/libexec/PlistBuddy -c "print ExpirationDate" $temp_profile_path)
echo "config_expirationDate='${config_expirationDate}'" >> $tempVarFile
#证书环境dev-dis-adhoc
config_envirionment=$(/usr/libexec/PlistBuddy -c "print :Entitlements:aps-environment" $temp_profile_path)



#打印所有的target,纯粹为了打印使用
xcodebuild -project $xcodeproj_path -list -alltargets
#将pbxproj项目配置文件转换成jsno文件好人工阅读解析
#plutil -convert json -r -o "./test.json" "$pbxproj_path"
#获取根配置
pbx_rootObject=$(/usr/libexec/PlistBuddy -c "print :rootObject:" $pbxproj_path)
echo $pbx_rootObject
#获取根配置对应的所有targets列表
pbx_targets=$(/usr/libexec/PlistBuddy -c "print :objects:$pbx_rootObject:targets" $pbxproj_path)

#将查找到的所有targets的id转换成数组
array_targets=(${pbx_targets//,/ })
echo "total:${#array_targets[@]} realCount:$[${#array_targets[@]}-3] find:$find_target"
#查找次数
find_times=0
#遍历所有target对应的id
for pbx_target in ${array_targets[@]}
do
echo $pbx_target
real_pbx_target=""
#判断只有不是Array,{,}这几个的才是真正的target的id
  if  [ $pbx_target != "Array"  -a  $pbx_target != "{"   -a   $pbx_target !=  "}" ];  then
    find_times=$[$find_times+1]
#获取targetId对应的名字,并和查找的名字对比
    target_name=$(/usr/libexec/PlistBuddy -c "print :objects:$pbx_target:name" $pbxproj_path)
    if [ $target_name != $find_target ]
    then
      continue
    fi
    real_pbx_target=$pbx_target
#打包
    if [ $target_name == $find_target ]
    then
       break
    fi
   fi
done

 echo "find_times:$find_times"
echo "real_pbx_target:$real_pbx_target"
 target_buildConfigurationList=$(/usr/libexec/PlistBuddy -c "print :objects:$pbx_target:buildConfigurationList" $pbxproj_path)
 target_debug=$(/usr/libexec/PlistBuddy -c "print :objects:$target_buildConfigurationList:buildConfigurations:0" $pbxproj_path)
 target_release=$(/usr/libexec/PlistBuddy -c "print :objects:$target_buildConfigurationList:buildConfigurations:1" $pbxproj_path)

 target_debug_identifier=$(/usr/libexec/PlistBuddy -c "print :objects:$target_debug:buildSettings:PRODUCT_BUNDLE_IDENTIFIER" $pbxproj_path)
 target_debug_profile=$(/usr/libexec/PlistBuddy -c "print :objects:$target_debug:buildSettings:PROVISIONING_PROFILE_SPECIFIER" $pbxproj_path)
 target_debug_team=$(/usr/libexec/PlistBuddy -c "print :objects:$target_debug:buildSettings:DEVELOPMENT_TEAM" $pbxproj_path)
 #iPhone Developer, iPhone Distribute
 target_debug_code=$(/usr/libexec/PlistBuddy -c "print :objects:$target_debug:buildSettings:CODE_SIGN_IDENTITY" $pbxproj_path)
 #manual auto
 target_debug_style=$(/usr/libexec/PlistBuddy -c "print :objects:$target_debug:buildSettings:CODE_SIGN_STYLE" $pbxproj_path)

 target_release_identifier=$(/usr/libexec/PlistBuddy -c "print :objects:$target_release:buildSettings:PRODUCT_BUNDLE_IDENTIFIER" $pbxproj_path)
 target_release_profileName=$(/usr/libexec/PlistBuddy -c "print :objects:$target_release:buildSettings:PROVISIONING_PROFILE_SPECIFIER" $pbxproj_path)
 target_release_profileId=$(/usr/libexec/PlistBuddy -c "print :objects:$target_release:buildSettings:PROVISIONING_PROFILE" $pbxproj_path)
 target_release_team=$(/usr/libexec/PlistBuddy -c "print :objects:$target_release:buildSettings:DEVELOPMENT_TEAM" $pbxproj_path)
 #iPhone Developer, iPhone Distribute
 target_release_code=$(/usr/libexec/PlistBuddy -c "print :objects:$target_release:buildSettings:CODE_SIGN_IDENTITY" $pbxproj_path)
 #manual auto
 target_release_style=$(/usr/libexec/PlistBuddy -c "print :objects:$target_release:buildSettings:CODE_SIGN_STYLE" $pbxproj_path)
 target_release_infoPath=$(/usr/libexec/PlistBuddy -c "print :objects:$target_release:buildSettings:INFOPLIST_FILE" $pbxproj_path)
#获取Info.plist的位置
 real_target_release_infoPath=${target_release_infoPath/\$(SRCROOT)/$src_root_path}



 echo "\r\n"
 echo "targetId:$pbx_target"
 echo "target_name:$target_name"
 echo "target_configList:$target_buildConfigurationList"
 echo "target_release:$target_release"
 echo "target_debug:$target_debug"

 echo "target_release_identifier:$target_release_identifier"
 echo "target_release_profileName:$target_release_profileName"
 echo "target_release_profileId:$target_release_profileId"
 echo "target_release_team:$target_release_team"
 echo "target_release_code:$target_release_code"
 echo "target_release_style:$target_release_style"
 echo "target_release_infoPath:$target_release_infoPath"
 echo "real_target_release_infoPath:$real_target_release_infoPath"


 #修改配置
 #修改release配置
 #bundileId
 /usr/libexec/PlistBuddy -c "Set :objects:$target_release:buildSettings:PRODUCT_BUNDLE_IDENTIFIER $config_identifier" $pbxproj_path
 #profileName
 /usr/libexec/PlistBuddy -c "Set :objects:$target_release:buildSettings:PROVISIONING_PROFILE_SPECIFIER $config_profile_name" $pbxproj_path
 #profile udid
 /usr/libexec/PlistBuddy -c "Set :objects:$target_release:buildSettings:PROVISIONING_PROFILE $config_profile_uuid" $pbxproj_path
 #teamId
 /usr/libexec/PlistBuddy -c "Set :objects:$target_release:buildSettings:DEVELOPMENT_TEAM $config_teamId" $pbxproj_path
 #iPhone Developer, iPhone Distribute
 real_code_identity="iPhone Distribution"
 /usr/libexec/PlistBuddy -c "Set :objects:$target_release:buildSettings:CODE_SIGN_IDENTITY $real_code_identity" $pbxproj_path
 #manual auto
 real_sign_style="Manual"
 /usr/libexec/PlistBuddy -c "Set :objects:$target_release:buildSettings:CODE_SIGN_STYLE $real_sign_style" $pbxproj_path
 #修改APP名称
 /usr/libexec/PlistBuddy -c "Set :CFBundleName 你好" $real_target_release_infoPath
 #修改APP版本(Xcode中的Version)
 /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString 8.8.8" $real_target_release_infoPath
 #修改APP版本(Xcode中的Build)
 /usr/libexec/PlistBuddy -c "Set :CFBundleVersion 9.9.9" $real_target_release_infoPath



 #修改debug配置
 #bundileId
 /usr/libexec/PlistBuddy -c "Set :objects:$target_debug:buildSettings:PRODUCT_BUNDLE_IDENTIFIER $config_identifier" $pbxproj_path
 #profileName
 /usr/libexec/PlistBuddy -c "Set :objects:$target_debug:buildSettings:PROVISIONING_PROFILE_SPECIFIER $config_profile_name" $pbxproj_path
 #profile udid
 #/usr/libexec/PlistBuddy -c "Set :objects:$target_debug:buildSettings:PROVISIONING_PROFILE $config_profile_uuid" $pbxproj_path
 #teamId
 /usr/libexec/PlistBuddy -c "Set :objects:$target_debug:buildSettings:DEVELOPMENT_TEAM $config_teamId" $pbxproj_path
 #iPhone Developer, iPhone Distribute
 real_code_identity="iPhone Distribution"
 /usr/libexec/PlistBuddy -c "Set :objects:$target_debug:buildSettings:CODE_SIGN_IDENTITY $real_code_identity" $pbxproj_path
 #manual auto
 real_sign_style="Manual"
 /usr/libexec/PlistBuddy -c "Set :objects:$target_debug:buildSettings:CODE_SIGN_STYLE $real_sign_style" $pbxproj_path


#通过设置的mobileprovision配置文件信息生成导出ipa包的ExportOptions.plist描述文件
 cat > "./ExportOptions.plist" << END_TEXT
<?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>compileBitcode</key>
<true/>
<key>destination</key>
<string>export</string>
<key>method</key>
<string>enterprise</string>
<key>provisioningProfiles</key>
<dict>
 <key>${config_identifier}</key>
 <string>${config_profile_name}</string>
</dict>
<key>signingCertificate</key>
<string>iPhone Distribution</string>
<key>signingStyle</key>
<string>manual</string>
<key>stripSwiftSymbols</key>
<true/>
<key>teamID</key>
<string>${config_teamId}</string>
<key>thinning</key>
<string>&lt;none&gt;</string>
</dict>
</plist>
END_TEXT

 #生成archive
  xcodebuild archive \
 -workspace "$xcworkspace_path" \
 -scheme ${find_target} \
 -configuration Release \
 -archivePath "./test.xcarchive"

 #导出ipa
 xcodebuild -exportArchive \
 -archivePath "./test.xcarchive" \
 -exportPath "./test_ipa" \
 -exportOptionsPlist "./ExportOptions.plist"

 # 压缩dSYM文件
 zip -q -r "./test.app.dSYM.zip" "./test.xcarchive/dSYMs/${find_target}.app.dSYM"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,837评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,551评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,417评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,448评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,524评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,554评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,569评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,316评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,766评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,077评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,240评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,912评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,560评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,176评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,425评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,114评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,114评论 2 352

推荐阅读更多精彩内容