浏览代码

支持GIT源码编译处理。

jianfeng1.wang 2 年之前
父节点
当前提交
7e81836865
共有 1 个文件被更改,包括 180 次插入55 次删除
  1. 180 55
      APK自动编译/gradlew.sh

+ 180 - 55
APK自动编译/gradlew.sh

@@ -42,8 +42,12 @@ APK_DEBUG_DIR=''
 APK_RELEASE_DIR=''
 # APK的版本号
 APK_VER_CODE=''
-# 中间件源码路径;
+# 中间件服务APK<https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/TVMidware/debug/TVMidwareService>;
 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
@@ -57,6 +61,8 @@ 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
@@ -68,6 +74,71 @@ SwitchExternal()
     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(){
@@ -82,65 +153,51 @@ commitAPK(){
             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"
+
+        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}
-        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:"
+        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 "\tBefore apk copy,APK_DEBUG_DIR=${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 "\r\nsvn up结果="${svn_result}
+            echo -e "commitAPK:svn up结果=${svn_result}\n"
             if [[ $svn_result != *"Updated to revision"* ]] && [[ $svn_result != *"At revision"* ]]; then
-                echo -e "\r\n连接SVN服务器失败, 网络异常!\n"
+                echo -e "commitAPK:连接SVN服务器失败, 网络异常!\n"
                 SwitchIntranet
             fi
         else
-            mkdir -p ${APK_DEBUG_DIR}
-            cd ${APK_DEBUG_DIR}
+            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}
-        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:"
+        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 "\tWORK_HOME PWD:$(pwd)"
+    echo -e "WORK_HOME PWD:$(pwd)\n"
     
     python3 -c "import builder;builder.updateProjectRev('$APK_SRC_DIR','$svn_lastChangedRev','$svn_lastChangedDate')"
 }
 
-compileGeneralApp(){
+compileGeneralApp()
+{
     echo "enter compileGeneralApp"
     # 需要单独处理两APK:FastCastTV、FastScreenV3.0的版本号获取问题
     if [ x${APK_NAME} == "xFastCastTV" ];then
@@ -203,7 +260,9 @@ compileGeneralApp(){
         # 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}
+        if [ ! -d ${APK_DEBUG_DIR} ];then
+            mkdir ${APK_DEBUG_DIR}
+        fi
         commitAPK
     fi
 }
@@ -219,11 +278,11 @@ compileCustomApp(){
 # 返回:0表示不需要更新本地svn目录, 1表示需要更新;
 IsUpdateSVN()
 {
-    SwitchIntranet
     echo -e "\n\n==========执行函数:IsUpdate=========="
+    SwitchIntranet
     # 如果目录不存在,直接返回需要更新;
     if [ ! -d ${APK_SRC_DIR} ]; then
-        echo -e "\t目录[${APK_SRC_DIR}]不存在,需要更新\n"
+        echo -e "目录[${APK_SRC_DIR}]不存在,需要更新\n"
         return 1
     fi
 
@@ -236,33 +295,33 @@ IsUpdateSVN()
     # 查找服务器的地址;
     cd ${APK_SRC_DIR}
     APK_SRC_URL=$(svn info|grep "URL:" |grep -v "Relative URL:" 2>&1) 
-    echo -e "\t${APK_SRC_URL}"
+    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 "\t连接SVN服务器失败, 网络异常=${svn_server_lcr}!\n"
+        echo -e "连接SVN服务器失败, 网络异常=${svn_server_lcr}!\n"
         SwitchIntranet
-        exit
+        return 0
     fi
 
     svn_server_lcr=${svn_server_lcr#*Last Changed Rev: }
-    echo -e "\tServer SVN Last Changed Revision=${svn_server_lcr}"
+    echo -e "Server SVN Last Changed Revision=${svn_server_lcr}"
 
     # 比较版本号或日期;
     if [ ${svn_server_lcr} -gt ${svn_local_lcr} ];then
-        echo -e "\t服务器版本大于本地目录版本号,需要更新"
+        echo -e "服务器版本大于本地目录版本号,需要更新"
         return 1
     fi
 
     if [ x${REBUILD} == "x1" ]; then
-        echo -e "\t手动编译标记位"
+        echo -e "手动编译标记位"
         return 1
     fi
 
-    echo -e "\t本地版本与服务器版本相同 ,不需要更新\n"
+    echo -e "本地版本与服务器版本相同 ,不需要更新\n"
     return 0
 }
 
@@ -315,13 +374,13 @@ IsUpdateGIT()
     return 0
 }
 
-UpdateCode()
+UpdateSVNCode()
 {
     SwitchIntranet
     echo -e "\n\n==========执行函数:UpdateCode=========="
     # 本地APK SVN目录是否存在,存在则删除;
     if [ -d ${APK_SRC_DIR} ]; then
-        echo -e "\t目录不为空,删除未提交的文件;\n"
+        echo -e "目录不为空,删除未提交的文件;\n"
         #rm -rf ${APK_SRC_DIR} # 删除操作太危险;
         svn cleanup ${APK_SRC_DIR}   
         filelist=$(svn status ${APK_SRC_DIR} 2>&1)
@@ -330,27 +389,30 @@ UpdateCode()
         filearray=(${filelist//? /})
         for File in ${filearray[@]}
         do
-            echo -e "\t 即将删除File="$File
             # 文件是否存在;
             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 "\tAPK URL空\n"
+        echo -e "APK URL空\n"
         SwitchIntranet
         exit
     fi
 
     #echo -e "\t开始Check代码;\n"
     #svn co --force ${APK_SRC_URL} ${APK_SRC_DIR}
-    echo -e "\t开始更新代码\n"
+    echo -e "开始更新代码\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"
+    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
@@ -358,7 +420,44 @@ UpdateCode()
     cd ${APK_SRC_DIR}
     # 判断文件是否存在;
     if [ -f "update_modules.sh" ];then
-        echo -e "\n\tupdate_modules.sh 文件存在,更新复用库\n\n"
+        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
@@ -366,13 +465,24 @@ UpdateCode()
 
 compile()
 {
+    echo ${CURR_TIME}
     echo "shell_log===>Compile start---------"
     # 判断工作副本是否需要更新;
-    IsUpdateSVN
+    if [ x${REPO_TYPE} == 'xSVN' ];then
+        IsUpdateSVN
+    else
+        IsUpdateGIT
+    fi
+
     if [ $? == 1 ]
     then
-        echo "需要更新本地SVN"
-        UpdateCode
+        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'"))')
@@ -394,7 +504,7 @@ compile()
 main()
 {
     if [ $# -eq 1 ]; then
-        REBUILD=0
+        REBUILD=1
         APK_SRC_DIR=$1
     elif [ $# -eq 2 ]; then
         APK_SRC_DIR=$1
@@ -422,7 +532,7 @@ main()
         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}
@@ -439,7 +549,22 @@ main()
     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"
+    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"