if [ "`pwd`" != "/" ]; then cd .. Root_path=$(pwd) Project_path="${Root_path}/${Project_dir}" else echo -e "\033[31m脚本路径错误\033[0m" exit 1; fi
if [ ! -d "${Project_path}" ];then echo -e "\033[31m首参数必须是有效的文件夹名称!!\033[0m" exit 2 fi
#Xcode project 文件路径 cd${Project_path} Valid_dic=false for i in `ls`; do #获取文件后缀名 extension=${i##*.} if [[ ${extension} == "xcodeproj" ]]; then Valid_dic=true elif [[ ${extension} == "xcworkspace" ]]; then Valid_dic=true fi done
if [[ ${Valid_dic} == false ]]; then echo -e "\033[31m请输入包含xcodeproj或xcworkspace的文件夹名称!!\033[0m" exit 2 fi
# 将数字123转换为1.2.3的格式输出 Num_str="" caculate(){ Parameter_num=$1; if [[ ${Parameter_num} < 10 ]]; then if [[ ${Num_str} == "" ]]; then Num_str="${Parameter_num}" else Num_str="${Parameter_num}.${Num_str}" fi else #商 quotient="$[${Parameter_num}/10]" #余数 remainder="$[${Parameter_num}%10]" if [[ ${Num_str} == "" ]]; then Num_str="${remainder}" else Num_str="${remainder}.${Num_str}" fi if [[ ${quotient} > 0 ]]; then caculate ${quotient} fi fi }
#-------------------------------------------- # 处理Version版本号 #-------------------------------------------- # Version版本号自增 if [[ ${Auto_increment_version_num} == "YES" ]]; then if [[ ${Version_str} != "custom" ]]; then echo -e "\033[31m选项-a 与 -v 不能同时存在\033[0m" exit 2 else #取Version版本号 Version_str=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString"${App_infoplist_path}) #把Version_str字符串根据.分割为数组 Array=($(echo$Version_str | tr '.'' ' | tr -s ' ')) #数组长度 Array_length=$(echo${#Array[@]}) # 获取版本号数字 Version_num=0 for (( i = 0; i < ${Array_length}; i++ )); do index_num=${Array[$i]} sum_num="$[10**(${Array_length}-${i}-1)]" index_num="$[${index_num}*${sum_num}]" Version_num="$[${Version_num}+${index_num}]" done
Version_num="$[$Version_num+1]" Num_str="" caculate ${Version_num} Version_str=${Num_str} fi fi
#-------------------------------------------- # 修改Version版本号以及Build号 #-------------------------------------------- if [[ ${Version_str} != "custom" ]]; then echo"初始Version = ${Version_initial}" /usr/libexec/Plistbuddy -c "Set CFBundleShortVersionString $Version_str""${App_infoplist_path}" Version_result=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString"${App_infoplist_path}) echo"Version改变后 = ${Version_result}" fi if [[ ${Build_str} != "custom" ]]; then echo"初始Build = ${Build_initial}" /usr/libexec/Plistbuddy -c "Set CFBundleVersion $Build_str""${App_infoplist_path}" Build_result=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion"${App_infoplist_path}) echo"Build改变后 = ${Build_result}" fi
if [ $? != 0 ]; then /usr/libexec/Plistbuddy -c "Set CFBundleShortVersionString $Version_initial""${App_infoplist_path}" /usr/libexec/Plistbuddy -c "Set CFBundleVersion $Build_initial""${App_infoplist_path}" echo -e "\033[31m************* 修改版本号出错 **************\033[0m" exit 2 fi
# 清除项目 # xcodebuild clean -configuration ${Configuration} &>/dev/null xcodebuild clean -configuration ${Configuration} >> $Log_path # if [[ ${Edit} == "YES" ]]; then # cd "~/Library/Developer/Xcode" # user=$USER # Xcode_path="/Users/${user}/Library/Developer/Xcode" # cd ${Xcode_path} # for i in `ls`; # do # if [[ ${i} == "DerivedData" ]]; then # if [ -d "${i}" ];then # # 删除~/Library/Developer/Xcode/DerivedData文件夹 # rm -rf ${i} # fi # fi # done # fi
revertVersionNum(){ /usr/libexec/Plistbuddy -c "Set CFBundleShortVersionString $Version_initial""${App_infoplist_path}" /usr/libexec/Plistbuddy -c "Set CFBundleVersion $Build_initial""${App_infoplist_path}" rm -rf "${IPA_path}" files=`ls ${Version_output_path}` if [ -z "$files" ]; then # 该版本号下没打过包,把该版本号文件夹删除 rm -rf "${Version_output_path}" fi }
if [ $? = 0 ]; then echo -e "\033[32m************* xcodebuild clean 完成 **************\033[0m" echo'' cd${Project_path} else revertVersionNum; echo -e "\033[31m************* xcodebuild clean 失败 **************\033[0m" echo'' exit 1; fi
Workspace_name="${Project_path}/${target_name}.xcworkspace" Project_name="${Project_path}/${target_name}.xcodeproj" if [[ ${Edit} == "YES" ]]; then if [[ ${Archive_type} == "workspace" ]];then #编译workspace xcodebuild -workspace "${Workspace_name}" -scheme "${Scheme_name}" -configuration "${Configuration}" >> $Log_path else #编译project xcodebuild -configuration "${Configuration}" >> $Log_path fi
if [ $? = 0 ]; then echo -e "\033[32m************* xcodebuild 编译 完成 **************\033[0m" echo'' else revertVersionNum; echo -e "\033[31m************* xcodebuild 编译 失败 **************\033[0m" echo'' exit 1; fi fi