#!/bin/sh # 编码要求: # 所有全大写变量均为全局变量; # 函数内部变量请使用非全大写; # 主目录; WORK_HOME="/home/data/ApkAutoCompile" # 参数1:apk源码路径; APK_SRC_DIR=$1 # apk名称,源于源码路径,需要对带 _General 的删除后缀。 APK_NAME='' # debug apk的svn版本号; APK_DEBUG_SVN_REVISION=$3 # Debug Apk 根地址; APK_DEBUG_ROOT_URL='https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/app/apk/01debug/general' # Release Apk 根地址; APK_RELEASE_ROOT_URL='https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/app/apk/02release/general' # Apk源码根地址; APK_SRC_ROOT_URL='https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/app/src' # APK 根路径; APK_ROOT_DIR="${WORK_HOME}/cacheAPK/general" # Debug Apk 根路径; APK_DEBUG_ROOT_DIR="${APK_ROOT_DIR}/Debug" # Release Apk 根路径; APK_RELEASE_ROOT_DIR="${APK_ROOT_DIR}/Release" # Debug Apk Log APK_DEBUG_LOG=$4 # Gradle目录; GRADLE_HOME=/home/data/android/gradle/gradle-6.7.1 # Android SDK 目录; ANDROID_SDK_HOME=/home/data/android/android_sdk/android-sdk-linux # 是否重新编译; REBUILD=$2 # 当前要处理的APK的SVN路径; APK_SRC_URL='' # Build的apk路径; APK_BUILD_DIR='' # debug Apk 路径; APK_DEBUG_DIR='' # debug Apk 路径; APK_RELEASE_DIR='' # APK的版本号 APK_VER_CODE='' # 中间件服务APK; APK_DIR_TVMidware="/home/data/ApkAutoCompile/cacheAPK/TVMidware" # 日期; CURR_TIME=$(date +"%Y-%m-%d %T") # 仓库类型:SVN或GIT REPO_TYPE='SVN' # 日志文件; if [ ! -d '/home/data/ApkAutoCompile/shell/log' ];then mkdir '/home/data/ApkAutoCompile/shell/log' fi LOG_FILE="/home/data/ApkAutoCompile/shell/log/gradlew-${APK_NAME}-$(date '+%d-%H%M%S').log" SwitchIntranet() { echo -e "\r\r---切换到内网---" route del default gw 10.201.40.1 sleep 3s route del default gw 10.201.40.1 sleep 3s route add default gw 10.201.40.1 sleep 3s route add default gw 10.201.40.1 } SwitchExternal() { echo -e "\r\r---切换到外网---" route del default gw 10.201.40.1 sleep 3s route del default gw 10.201.40.1 sleep 3s route del default gw 10.201.40.1 } debug_apk_svn_commit() { echo -e "func-> debug_apk_svn_commit\n" SVN_DEBUG_APK_PATH=$1 if [ -d ${SVN_DEBUG_APK_PATH} ];then local count=0 cd ${SVN_DEBUG_APK_PATH} # 查找未提交或变更的文件; filelist=$(svn status) # 以"? "分组字符串; filearray=(${filelist//? /}) for File in ${filearray[@]} do let count+=1 # 如果是文件; if [ -f ${File} ];then msg=$(svn add ${File} 2>&1) echo -e "svn add file = ${File}, result=${msg}\n" elif [ -d ${File} ];then msg=$(svn add ${File} 2>&1) echo -e "svn add dir = ${File}, result=${msg}\n" fi done # 提交; echo "变化文件数量 = ${count}" if [ $count -gt 0 ];then cd ${SVN_DEBUG_APK_PATH} # 判断源码路径类型; if [ x${REPO_TYPE} == x'GIT' ];then echo "源码路径为git" # 获取源码版本; cd ${APK_SRC_DIR} shortId=$(git rev-parse --short HEAD 2>&1) lastChangedRev="Last Changed Rev: ${shortId}" cd ${SVN_DEBUG_APK_PATH} else # 获取源码版本; echo "源码路径为svn" lastChangedRev=$(svn info ${APK_SRC_DIR}|grep "Last Changed Rev" 2>&1) fi msg=$(svn commit -m "upgrade ${APK_NAME}.${APK_VER_CODE}. ${lastChangedRev}" 2>&1) echo -e "svn commit result=${msg}\n" if [[ $msg != *"Committed revision"* ]]; then echo -e "svn_commit:连接SVN服务器失败, 网络异常!\n" SwitchIntranet return 0 else echo -e "提交svn成功\n" return 1 fi else echo -e "无文件变化,不需要提交svn\n" return 0 fi else echo -e "目录<${SVN_DEBUG_APK_PATH}>不存在,无法提交.\n" return 0 fi } commitAPK(){ echo "---------commitAPK------------" # 切回内网; SwitchIntranet sleep 10s echo "apk name = "${APK_NAME} if [ x${APK_NAME} == "xMiddleCommon" ];then if [ -d ${APK_DIR_TVMidware} ]; then cd ${APK_DIR_TVMidware} rm * fi svn_result=$(svn up 2>&1) if [[ $svn_result != *"Updated to revision"* ]] && [[ $svn_result != *"At revision"* ]]; then echo -e "svn up:连接SVN服务器失败, 网络异常!\n" fi cp ${APK_BUILD_DIR} ${APK_DIR_TVMidware} debug_apk_svn_commit ${APK_DIR_TVMidware} ls -l ${APK_DIR_TVMidware} else # copy apk if [ -d ${APK_DEBUG_DIR} ]; then cd ${APK_DEBUG_DIR} echo -e "APK_DEBUG_DIR=${APK_DEBUG_DIR}\n" ls -l ${APK_DEBUG_DIR} rm * svn_result=$(svn up 2>&1) echo -e "commitAPK:svn up结果=${svn_result}\n" if [[ $svn_result != *"Updated to revision"* ]] && [[ $svn_result != *"At revision"* ]]; then echo -e "commitAPK:连接SVN服务器失败, 网络异常!\n" SwitchIntranet fi else mkdir -p ${APK_DEBUG_DIR} fi cd ${APK_DEBUG_DIR} svn_lastChangedDate=$(svn info ${APK_SRC_DIR}|grep Date 2>&1) svn_lastChangedRev=$(svn info ${APK_SRC_DIR}|grep "Last Changed Rev" 2>&1) cp ${APK_BUILD_DIR} ${APK_DEBUG_DIR} debug_apk_svn_commit ${APK_DEBUG_DIR} echo -e "After apk copy, APK_DEBUG_DIR:" ls -l ${APK_DEBUG_DIR} fi #ensure under shell folder cd ${WORK_HOME}/shell echo -e "WORK_HOME PWD:$(pwd)\n" python3 -c "import builder;builder.updateProjectRev('$APK_SRC_DIR','$svn_lastChangedRev','$svn_lastChangedDate')" } compileGeneralApp() { echo "enter compileGeneralApp" # 需要单独处理两APK:FastCastTV、FastScreenV3.0的版本号获取问题 if [ x${APK_NAME} == "xFastCastTV" ];then echo -e "\t单独处理此APK Local_FastScreenTV_CN" appVCodeT=$(cat "${APK_SRC_DIR}/Local_FastScreenTV/build.gradle"|grep versionCode |grep -v "getVersionNameByCode\|=\|flavorDimensions") elif [ x${APK_NAME} == "xFastScreenV3.0" ];then echo -e "\t单独处理此APK FastScreenV3.0" appVCodeT=$(cat "${APK_SRC_DIR}/AdvertisementActivity/build.gradle"|grep versionCode |grep -v "getVersionNameByCode\|=\|flavorDimensions") else appVCodeT=$(cat "${APK_SRC_DIR}/app/build.gradle"|grep versionCode |grep -v "getVersionNameByCode\|=\|flavorDimensions") fi # 查找apk版本号 echo "appVCodeT=${appVCodeT}" if [ ${#appVCodeT} -eq 0 ];then echo -e "\tappVCode error" else vCode=$(echo $appVCodeT | tr -cd "[0-9]") echo -e "\tappVCode number:"$vCode mainVersionCode=$(expr $vCode / 100) childVersionCode=$(expr $vCode % 100) appDate=$(date +%Y%m%d) APK_VER_CODE="appVersionName:"$mainVersionCode"."$childVersionCode"."$appDate echo -e "\t$APK_VER_CODE" fi # route to google SwitchExternal sleep 10s #get release packet name releaseName=$(python3 -c 'import builder;print(builder.getReleaseName("'$APK_SRC_DIR'"))') echo -e "\treleaseName:"${releaseName} #compile app for release apk cp sabuild/local.properties $APK_SRC_DIR cd "$APK_SRC_DIR" echo -e "\tAPK_SRC_DIR,PWD:$(pwd)" chmod +x gradlew gradle --version # 修改 adle/wrapper/gradle-wrapper.properties distributionUrl 路径为本地; # home/data/ApkAutoCompile/cacheAndroidSrc/Factory_General/gradle/wrapper/gradle-wrapper.properties GradlePro=${APK_SRC_DIR}/gradle/wrapper/gradle-wrapper.properties sed -i "s/https\\\:\/\/services.gradle.org\/distributions/file:\/\/\/home\/local_gradle/" ${GradlePro} # 判断文件是否存在; if [ -f "build.sh" ];then echo -e "\tbuild.sh" chmod +x build.sh ./build.sh else echo -e "\tNo build.sh" ./gradlew ${releaseName} fi #commit apk APK_BUILD_DIR=$(find ${APK_SRC_DIR} -name "*.apk") echo -e "\tAPK_BUILD_DIR:${APK_BUILD_DIR}" if [ ${#APK_BUILD_DIR} -eq 0 ];then echo -e "\tcompile apk fail" else tmp1=${APK_NAME%*_General} echo -e "\t==========APK_NAME=" ${APK_NAME} echo -e "\t==========tmp1=" ${tmp1} # P_APK_DIR="${APK_DIR_general}/${tmp1#*TIF_}" APK_DEBUG_DIR="${APK_DEBUG_ROOT_DIR}/${tmp1}" echo -e "\t==========APK_DEBUG_DIR=" ${APK_DEBUG_DIR} if [ ! -d ${APK_DEBUG_DIR} ];then mkdir ${APK_DEBUG_DIR} fi commitAPK fi } compileCustomApp(){ echo -e "\tenter compileCustomApp" apkSections=$(python3 -c 'import builder;print(builder.getAPKSection("'$APK_SRC_DIR'"))') echo -e "\tapkSections:"${apkSections} } # 函数:是否需要更新本地svn目录; # 描述:判断本地svn目录版本与服务器版本是否一致; # 返回:0表示不需要更新本地svn目录, 1表示需要更新; IsUpdateSVN() { echo -e "\n\n==========执行函数:IsUpdate==========" SwitchIntranet # 如果目录不存在,直接返回需要更新; if [ ! -d ${APK_SRC_DIR} ]; then echo -e "目录[${APK_SRC_DIR}]不存在,需要更新\n" return 1 fi # 获取本地目录的最新提交版本; svn_local_lcr=$(svn info ${APK_SRC_DIR}|grep "Last Changed Rev" 2>&1) echo -e "\r\r获取本地目录的最新提交版本="${svn_local_lcr} svn_local_lcr=${svn_local_lcr#*Last Changed Rev: } echo -e "\r\nLocal SVN Last Changed Revision=${svn_local_lcr}" # 查找服务器的地址; cd ${APK_SRC_DIR} APK_SRC_URL=$(svn info|grep "URL:" |grep -v "Relative URL:" 2>&1) echo -e "${APK_SRC_URL}" APK_SRC_URL=${APK_SRC_URL#*URL:} cd - # 获取服务器的最新提交版本; svn_server_lcr=$(svn info ${APK_SRC_URL}|grep "Last Changed Rev" 2>&1) if [[ $svn_server_lcr == *"svn: E170013: Unable to connect to a repository at URL"* ]]; then echo -e "连接SVN服务器失败, 网络异常=${svn_server_lcr}!\n" SwitchIntranet return 0 fi svn_server_lcr=${svn_server_lcr#*Last Changed Rev: } echo -e "Server SVN Last Changed Revision=${svn_server_lcr}" # 比较版本号或日期; if [ ${svn_server_lcr} -gt ${svn_local_lcr} ];then echo -e "服务器版本大于本地目录版本号,需要更新" return 1 fi if [ x${REBUILD} == "x1" ]; then echo -e "手动编译标记位" return 1 fi echo -e "本地版本与服务器版本相同 ,不需要更新\n" return 0 } IsUpdateGIT() { SwitchIntranet echo -e "\n\n==========执行函数:IsUpdate==========" # 如果目录不存在,直接返回需要更新; if [ ! -d ${APK_SRC_DIR} ]; then echo -e "\t目录[${APK_SRC_DIR}]不存在,需要更新\n" return 1 fi # 获取本地目录的最新提交版本; svn_local_lcr=$(svn info ${APK_SRC_DIR}|grep "Last Changed Rev" 2>&1) echo -e "\r\r获取本地目录的最新提交版本="${svn_local_lcr} svn_local_lcr=${svn_local_lcr#*Last Changed Rev: } echo -e "\r\nLocal SVN Last Changed Revision=${svn_local_lcr}" # 查找服务器的地址; cd ${APK_SRC_DIR} APK_SRC_URL=$(svn info|grep "URL:" |grep -v "Relative URL:" 2>&1) echo -e "\t${APK_SRC_URL}" APK_SRC_URL=${APK_SRC_URL#*URL:} cd - # 获取服务器的最新提交版本; svn_server_lcr=$(svn info ${APK_SRC_URL}|grep "Last Changed Rev" 2>&1) if [[ $svn_server_lcr == *"svn: E170013: Unable to connect to a repository at URL"* ]]; then echo -e "\t连接SVN服务器失败, 网络异常=${svn_server_lcr}!\n" SwitchIntranet exit fi svn_server_lcr=${svn_server_lcr#*Last Changed Rev: } echo -e "\tServer SVN Last Changed Revision=${svn_server_lcr}" # 比较版本号或日期; if [ ${svn_server_lcr} -gt ${svn_local_lcr} ];then echo -e "\t服务器版本大于本地目录版本号,需要更新" return 1 fi if [ x${REBUILD} == "x1" ]; then echo -e "\t手动编译标记位" return 1 fi echo -e "\t本地版本与服务器版本相同 ,不需要更新\n" return 0 } UpdateSVNCode() { SwitchIntranet echo -e "\n\n==========执行函数:UpdateCode==========" # 本地APK SVN目录是否存在,存在则删除; if [ -d ${APK_SRC_DIR} ]; then echo -e "目录不为空,删除未提交的文件;\n" #rm -rf ${APK_SRC_DIR} # 删除操作太危险; svn cleanup ${APK_SRC_DIR} filelist=$(svn status ${APK_SRC_DIR} 2>&1) echo -e "未提交的文件="${filelist} # 以"A "分组字符串,并修改logo后缀; filearray=(${filelist//? /}) for File in ${filearray[@]} do # 文件是否存在; if [ -f ${File} ];then rm -f ${File} echo -e "已删除文件=${File}\n" elif [ -d ${File} ];then rm -rf ${File} echo -e "已删除路径=${File}\n" fi done fi if [ x"${APK_SRC_URL}" = x ]; then echo -e "APK URL空\n" SwitchIntranet exit fi #echo -e "\t开始Check代码;\n" #svn co --force ${APK_SRC_URL} ${APK_SRC_DIR} echo -e "开始更新代码\n" svn_result=$(svn up ${APK_SRC_DIR} 2>&1) echo -e "UpdateCode: svn up结果=${svn_result}\n" if [[ $svn_result != *"At revision"* ]] && [[ $svn_result != *"Updated to revision"* ]]; then echo -e "连接SVN服务器失败, 网络异常!\n" SwitchIntranet exit fi cd ${APK_SRC_DIR} # 判断文件是否存在; if [ -f "update_modules.sh" ];then echo -e "\nupdate_modules.sh 文件存在,更新复用库\n\n" chmod 777 update_modules.sh ./update_modules.sh fi } UpdateGITCode() { SwitchIntranet echo -e "\n\n==========执行函数:UpdateCode==========" # 本地APK SVN目录是否存在,存在则删除; if [ -d ${APK_SRC_DIR} ]; then echo -e "目录不为空,删除未提交的文件;\n" cd ${APK_SRC_DIR} git clean -xfd fi if [ x"${APK_SRC_URL}" = x ]; then echo -e "APK URL空\n" SwitchIntranet #exit fi #echo -e "\t开始Check代码;\n" #svn co --force ${APK_SRC_URL} ${APK_SRC_DIR} echo -e "开始更新代码\n" git_result=$(git pull 2>&1) echo -e "UpdateCode: git pull结果=${git_result}\n" if [[ $git_result != *"files changed,"* ]] && [[ $git_result != *"Already up to date."* ]]; then echo -e "连接GIT服务器失败, 网络异常!\n" SwitchIntranet #exit fi cd ${APK_SRC_DIR} # 判断文件是否存在; if [ -f "update_modules.sh" ];then echo -e "\nupdate_modules.sh 文件存在,更新复用库\n\n" chmod 777 update_modules.sh ./update_modules.sh fi } compile() { echo ${CURR_TIME} echo "shell_log===>Compile start---------" # 判断工作副本是否需要更新; if [ x${REPO_TYPE} == 'xSVN' ];then IsUpdateSVN else IsUpdateGIT fi if [ $? == 1 ] then echo "需要更新代码" if [ x${REPO_TYPE} == 'xSVN' ];then UpdateSVNCode else UpdateGITCode fi cd ${WORK_HOME}/shell # 读取ini配置文件中的信息; releaseType=$(python3 -c 'import builder;print(builder.getReleaseName("'$APK_SRC_DIR'"))') if [ $releaseType == 'custom' ];then compileCustomApp else compileGeneralApp fi echo "shell_log===>Compile end------------" else echo "无需更新本地SVN" echo "shell_log===>No upgrade end------------" fi # 切换回内网; SwitchIntranet } main() { if [ $# -eq 1 ]; then REBUILD=1 APK_SRC_DIR=$1 elif [ $# -eq 2 ]; then APK_SRC_DIR=$1 REBUILD=$2 fi echo -e "########################################################开始编译########################################################\n" # 导出变量到子shell; export WORK_HOME #/home/data/ApkAutoCompile export GRADLE_HOME #/home/data/android/gradle/gradle-6.7.1 export ANDROID_SDK_HOME #/home/data/android/android_sdk/android-sdk-linux # 创建 编译完成的Apk文件 根目录; if [ ! -d ${APK_ROOT_DIR} ];then mkdir -p ${APK_ROOT_DIR} fi # Apk Debug目录不用创建,但Release根目录需要; if [ ! -d ${APK_RELEASE_ROOT_DIR} ];then mkdir -p ${APK_RELEASE_ROOT_DIR} fi # 检出所有debug apk; if [ ! -d ${APK_DEBUG_ROOT_DIR} ];then svn co ${APK_DEBUG_ROOT_URL} ${APK_DEBUG_ROOT_DIR} fi # 检出中间件服务APK; if [ ! -d ${APK_DIR_TVMidware} ];then echo -e 'svn co TVMidware Apk' svn co https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/TVMidware/debug/TVMidwareService ${APK_DIR_TVMidware} fi # 取右边第一个字符,判断是否为斜杠; if [ "${APK_SRC_DIR:0-1:1}" = "/" ];then # 若为斜杠,%号截取,删除右边第一个'/'及右边字符,保存左边字符; echo "最后为斜杠,删除斜杠" APK_SRC_DIR=${APK_SRC_DIR%/*} fi # 获取APK名称; APK_NAME=${APK_SRC_DIR##*/} echo -e "APK_SRC_DIR=${APK_SRC_DIR}\r\nAPK_NAME=${APK_NAME}" # 重新定义log文件; if [ ! -d /home/data/ApkAutoCompile/shell/log/gradlew/ ];then msg=$(mkdir /home/data/ApkAutoCompile/shell/log/gradlew/last) fi LOG_FILE="/home/data/ApkAutoCompile/shell/log/gradlew/${APK_NAME}.log" if [ -f ${LOG_FILE} ];then mkdir -p /home/data/ApkAutoCompile/shell/log/gradlew/last mv ${LOG_FILE} /home/data/ApkAutoCompile/shell/log/gradlew/last/${APK_NAME}.log fi if [ -d ${APK_SRC_DIR}/.git ];then REPO_TYPE='GIT' echo "-------仓库类型为GIT-------" else REPO_TYPE='SVN' echo "-------仓库类型为SVN-------" fi compile 2>&1 | tee ${LOG_FILE} echo -e "########################################################编译结束########################################################\n" } main $@