123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- #!/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_DIR_TVMidware="/home/data/ApkAutoCompile/cacheAPK/TVMidware"
- # 日志文件;
- 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 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
- }
- 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)
- if [[ $svn_result != *"At revision"* ]] || [[ $svn_result != *"is already under version control"* ]]; then
- echo -e "\tsvn up:连接SVN服务器失败, 网络异常!\n"
- fi
- cp ${APK_BUILD_DIR} ${APK_DIR_TVMidware}
- echo "APK_DIR_TVMidware PWD:$(pwd)"
- svn add .
- svn add *
- svn_result=$(svn commit -m "upgrade ${APK_NAME}.${APK_VER_CODE}. ${svn_lastChangedRev}" 2>&1)
- if [[ $svn_result != *"At revision"* ]] || [[ $svn_result != *"is already under version control"* ]]; then
- echo -e "\t连接SVN服务器失败, 网络异常!\n"
- fi
- echo "After apk copy,P_APK_DIR:"
- ls -l ${APK_DIR_TVMidware}
- else
- # copy apk
- if [ -d ${APK_DEBUG_DIR} ]; then
- cd ${APK_DEBUG_DIR}
- echo -e "\tBefore apk copy,APK_DEBUG_DIR=${APK_DEBUG_DIR}"
- ls -l ${APK_DEBUG_DIR}
- rm *
- svn_result=$(svn up 2>&1)
- echo -e "\r\nsvn up结果="${svn_result}
- if [[ $svn_result != *"Updated to revision"* ]] && [[ $svn_result != *"At revision"* ]]; then
- echo -e "\r\n连接SVN服务器失败, 网络异常!\n"
- SwitchIntranet
- fi
- else
- mkdir -p ${APK_DEBUG_DIR}
- cd ${APK_DEBUG_DIR}
- fi
-
- 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}
- echo -e "\tAPK_DEBUG_DIR PWD:$(pwd)"
- svn add .
- svn add *
- svn_result=$(svn commit -m "upgrade ${APK_NAME}.${APK_VER_CODE}. ${svn_lastChangedRev}" 2>&1)
- echo -e "\r\nsvn commit结果="${svn_result}
- if [[ $svn_result != *"Committed revision"* ]]; then
- echo -e "\t连接SVN服务器失败, 网络异常!\n"
- SwitchIntranet
- exit
- fi
- echo -e "\tAfter apk copy,APK_DEBUG_DIR:"
- ls -l ${APK_DEBUG_DIR}
- fi
- #ensure under shell folder
- cd ${WORK_HOME}/shell
- echo -e "\tWORK_HOME PWD:$(pwd)"
-
- 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
- # 判断文件是否存在;
- 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}
- mkdir ${APK_DEBUG_DIR}
- 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()
- {
- 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
- }
- 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
- }
- UpdateCode()
- {
- SwitchIntranet
- echo -e "\n\n==========执行函数:UpdateCode=========="
- # 本地APK SVN目录是否存在,存在则删除;
- if [ -d ${APK_SRC_DIR} ]; then
- echo -e "\t目录不为空,删除未提交的文件;\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
- echo -e "\t 即将删除File="$File
- # 文件是否存在;
- if [ -f ${File} ];then
- rm -f ${File}
- fi
- done
- fi
- if [ x"${APK_SRC_URL}" = x ]; then
- echo -e "\tAPK URL空\n"
- SwitchIntranet
- exit
- fi
- #echo -e "\t开始Check代码;\n"
- #svn co --force ${APK_SRC_URL} ${APK_SRC_DIR}
- echo -e "\t开始更新代码\n"
- svn_result=$(svn up ${APK_SRC_DIR} 2>&1)
- echo -e "\r\nsvn up结果="${svn_result}
- if [[ $svn_result != *"At revision"* ]]; then
- echo -e "\t连接SVN服务器失败, 网络异常!\n"
- SwitchIntranet
- exit
- fi
- cd ${APK_SRC_DIR}
- # 判断文件是否存在;
- if [ -f "update_modules.sh" ];then
- echo -e "\n\tupdate_modules.sh 文件存在,更新复用库\n\n"
- chmod 777 update_modules.sh
- ./update_modules.sh
- fi
- }
- compile()
- {
- echo "shell_log===>Compile start---------"
- # 判断工作副本是否需要更新;
- IsUpdateSVN
- if [ $? == 1 ]
- then
- echo "需要更新本地SVN"
- UpdateCode
- 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=0
- 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
- # 检出中间件源码;
- 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文件;
- LOG_FILE="/home/data/ApkAutoCompile/shell/log/gradlew-${APK_NAME}.log"
- compile 2>&1 | tee ${LOG_FILE}
- echo -e "########################################################编译结束########################################################\n"
- }
- main $@
|