美文网首页tool for work
多target配置描述文件自动修改匹配描述文件打包ipa

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

作者: 独孤流 | 来源:发表于2019-08-19 11:41 被阅读2次
    前言

    在上一篇讲述了多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"
    
    

    相关文章

      网友评论

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

        本文链接:https://www.haomeiwen.com/subject/ozkssctx.html