gradlew.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #!/bin/sh
  2. # 编码要求:
  3. # 所有全大写变量均为全局变量;
  4. # 函数内部变量请使用非全大写;
  5. # 主目录;
  6. WORK_HOME="/home/data/ApkAutoCompile"
  7. # 参数1:apk源码路径;
  8. APK_SRC_DIR=$1
  9. # apk名称,源于源码路径,需要对带 _General 的删除后缀。
  10. APK_NAME=''
  11. # debug apk的svn版本号;
  12. APK_DEBUG_SVN_REVISION=$3
  13. # Debug Apk 根地址;
  14. APK_DEBUG_ROOT_URL='https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/app/apk/01debug/general'
  15. # Release Apk 根地址;
  16. APK_RELEASE_ROOT_URL='https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/app/apk/02release/general'
  17. # Apk源码根地址;
  18. APK_SRC_ROOT_URL='https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/app/src'
  19. # APK 根路径;
  20. APK_ROOT_DIR="${WORK_HOME}/cacheAPK/general"
  21. # Debug Apk 根路径;
  22. APK_DEBUG_ROOT_DIR="${APK_ROOT_DIR}/Debug"
  23. # Release Apk 根路径;
  24. APK_RELEASE_ROOT_DIR="${APK_ROOT_DIR}/Release"
  25. # Debug Apk Log
  26. APK_DEBUG_LOG=$4
  27. # Gradle目录;
  28. GRADLE_HOME=/home/data/android/gradle/gradle-6.7.1
  29. # Android SDK 目录;
  30. ANDROID_SDK_HOME=/home/data/android/android_sdk/android-sdk-linux
  31. # 是否重新编译;
  32. REBUILD=$2
  33. # 当前要处理的APK的SVN路径;
  34. APK_SRC_URL=''
  35. # Build的apk路径;
  36. APK_BUILD_DIR=''
  37. # debug Apk 路径;
  38. APK_DEBUG_DIR=''
  39. # debug Apk 路径;
  40. APK_RELEASE_DIR=''
  41. # APK的版本号
  42. APK_VER_CODE=''
  43. # 中间件源码路径;
  44. APK_DIR_TVMidware="/home/data/ApkAutoCompile/cacheAPK/TVMidware"
  45. # 日志文件;
  46. if [ ! -d '/home/data/ApkAutoCompile/shell/log' ];then
  47. mkdir '/home/data/ApkAutoCompile/shell/log'
  48. fi
  49. LOG_FILE="/home/data/ApkAutoCompile/shell/log/gradlew-${APK_NAME}-$(date '+%d-%H%M%S').log"
  50. SwitchIntranet()
  51. {
  52. echo -e "\r\r---切换到内网---"
  53. route del default gw 10.201.40.1
  54. sleep 3s
  55. route add default gw 10.201.40.1
  56. sleep 3s
  57. route add default gw 10.201.40.1
  58. }
  59. SwitchExternal()
  60. {
  61. echo -e "\r\r---切换到外网---"
  62. route del default gw 10.201.40.1
  63. sleep 3s
  64. route del default gw 10.201.40.1
  65. }
  66. commitAPK(){
  67. echo "---------commitAPK------------"
  68. # 切回内网;
  69. SwitchIntranet
  70. sleep 10s
  71. echo "apk name = "${APK_NAME}
  72. if [ x${APK_NAME} == "xMiddleCommon" ];then
  73. if [ -d ${APK_DIR_TVMidware} ]; then
  74. cd ${APK_DIR_TVMidware}
  75. rm *
  76. fi
  77. svn_result=$(svn up)
  78. if [[ $svn_result != *"At revision"* ]] || [[ $svn_result != *"is already under version control"* ]]; then
  79. echo -e "\tsvn up:连接SVN服务器失败, 网络异常!\n"
  80. fi
  81. cp ${APK_BUILD_DIR} ${APK_DIR_TVMidware}
  82. echo "APK_DIR_TVMidware PWD:$(pwd)"
  83. svn add .
  84. svn add *
  85. svn_result=$(svn commit -m "upgrade ${APK_NAME}.${APK_VER_CODE}. ${svn_lastChangedRev}" 2>&1)
  86. if [[ $svn_result != *"At revision"* ]] || [[ $svn_result != *"is already under version control"* ]]; then
  87. echo -e "\t连接SVN服务器失败, 网络异常!\n"
  88. fi
  89. echo "After apk copy,P_APK_DIR:"
  90. ls -l ${APK_DIR_TVMidware}
  91. else
  92. # copy apk
  93. if [ -d ${APK_DEBUG_DIR} ]; then
  94. cd ${APK_DEBUG_DIR}
  95. echo -e "\tBefore apk copy,APK_DEBUG_DIR=${APK_DEBUG_DIR}"
  96. ls -l ${APK_DEBUG_DIR}
  97. rm *
  98. svn_result=$(svn up 2>&1)
  99. echo -e "\r\nsvn up结果="${svn_result}
  100. if [[ $svn_result != *"Updated to revision"* ]] && [[ $svn_result != *"At revision"* ]]; then
  101. echo -e "\r\n连接SVN服务器失败, 网络异常!\n"
  102. SwitchIntranet
  103. fi
  104. else
  105. mkdir -p ${APK_DEBUG_DIR}
  106. cd ${APK_DEBUG_DIR}
  107. fi
  108. svn_lastChangedDate=$(svn info ${APK_SRC_DIR}|grep Date 2>&1)
  109. svn_lastChangedRev=$(svn info ${APK_SRC_DIR}|grep "Last Changed Rev" 2>&1)
  110. cp ${APK_BUILD_DIR} ${APK_DEBUG_DIR}
  111. echo -e "\tAPK_DEBUG_DIR PWD:$(pwd)"
  112. svn add .
  113. svn add *
  114. svn_result=$(svn commit -m "upgrade ${APK_NAME}.${APK_VER_CODE}. ${svn_lastChangedRev}" 2>&1)
  115. echo -e "\r\nsvn commit结果="${svn_result}
  116. if [[ $svn_result != *"Committed revision"* ]]; then
  117. echo -e "\t连接SVN服务器失败, 网络异常!\n"
  118. SwitchIntranet
  119. exit
  120. fi
  121. echo -e "\tAfter apk copy,APK_DEBUG_DIR:"
  122. ls -l ${APK_DEBUG_DIR}
  123. fi
  124. #ensure under shell folder
  125. cd ${WORK_HOME}/shell
  126. echo -e "\tWORK_HOME PWD:$(pwd)"
  127. python3 -c "import builder;builder.updateProjectRev('$APK_SRC_DIR','$svn_lastChangedRev','$svn_lastChangedDate')"
  128. }
  129. compileGeneralApp(){
  130. echo "enter compileGeneralApp"
  131. # 需要单独处理两APK:FastCastTV、FastScreenV3.0的版本号获取问题
  132. if [ x${APK_NAME} == "xFastCastTV" ];then
  133. echo -e "\t单独处理此APK Local_FastScreenTV_CN"
  134. appVCodeT=$(cat "${APK_SRC_DIR}/Local_FastScreenTV/build.gradle"|grep versionCode |grep -v "getVersionNameByCode\|=\|flavorDimensions")
  135. elif [ x${APK_NAME} == "xFastScreenV3.0" ];then
  136. echo -e "\t单独处理此APK FastScreenV3.0"
  137. appVCodeT=$(cat "${APK_SRC_DIR}/AdvertisementActivity/build.gradle"|grep versionCode |grep -v "getVersionNameByCode\|=\|flavorDimensions")
  138. else
  139. appVCodeT=$(cat "${APK_SRC_DIR}/app/build.gradle"|grep versionCode |grep -v "getVersionNameByCode\|=\|flavorDimensions")
  140. fi
  141. # 查找apk版本号
  142. echo "appVCodeT=${appVCodeT}"
  143. if [ ${#appVCodeT} -eq 0 ];then
  144. echo -e "\tappVCode error"
  145. else
  146. vCode=$(echo $appVCodeT | tr -cd "[0-9]")
  147. echo -e "\tappVCode number:"$vCode
  148. mainVersionCode=$(expr $vCode / 100)
  149. childVersionCode=$(expr $vCode % 100)
  150. appDate=$(date +%Y%m%d)
  151. APK_VER_CODE="appVersionName:"$mainVersionCode"."$childVersionCode"."$appDate
  152. echo -e "\t$APK_VER_CODE"
  153. fi
  154. # route to google
  155. SwitchExternal
  156. sleep 10s
  157. #get release packet name
  158. releaseName=$(python3 -c 'import builder;print(builder.getReleaseName("'$APK_SRC_DIR'"))')
  159. echo -e "\treleaseName:"${releaseName}
  160. #compile app for release apk
  161. cp sabuild/local.properties $APK_SRC_DIR
  162. cd "$APK_SRC_DIR"
  163. echo -e "\tAPK_SRC_DIR,PWD:$(pwd)"
  164. chmod +x gradlew
  165. gradle --version
  166. # 判断文件是否存在;
  167. if [ -f "build.sh" ];then
  168. echo -e "\tbuild.sh"
  169. chmod +x build.sh
  170. ./build.sh
  171. else
  172. echo -e "\tNo build.sh"
  173. ./gradlew ${releaseName}
  174. fi
  175. #commit apk
  176. APK_BUILD_DIR=$(find ${APK_SRC_DIR} -name "*.apk")
  177. echo -e "\tAPK_BUILD_DIR:${APK_BUILD_DIR}"
  178. if [ ${#APK_BUILD_DIR} -eq 0 ];then
  179. echo -e "\tcompile apk fail"
  180. else
  181. tmp1=${APK_NAME%*_General}
  182. echo -e "\t==========APK_NAME=" ${APK_NAME}
  183. echo -e "\t==========tmp1=" ${tmp1}
  184. # P_APK_DIR="${APK_DIR_general}/${tmp1#*TIF_}"
  185. APK_DEBUG_DIR="${APK_DEBUG_ROOT_DIR}/${tmp1}"
  186. echo -e "\t==========APK_DEBUG_DIR=" ${APK_DEBUG_DIR}
  187. mkdir ${APK_DEBUG_DIR}
  188. commitAPK
  189. fi
  190. }
  191. compileCustomApp(){
  192. echo -e "\tenter compileCustomApp"
  193. apkSections=$(python3 -c 'import builder;print(builder.getAPKSection("'$APK_SRC_DIR'"))')
  194. echo -e "\tapkSections:"${apkSections}
  195. }
  196. # 函数:是否需要更新本地svn目录;
  197. # 描述:判断本地svn目录版本与服务器版本是否一致;
  198. # 返回:0表示不需要更新本地svn目录, 1表示需要更新;
  199. IsUpdateSVN()
  200. {
  201. SwitchIntranet
  202. echo -e "\n\n==========执行函数:IsUpdate=========="
  203. # 如果目录不存在,直接返回需要更新;
  204. if [ ! -d ${APK_SRC_DIR} ]; then
  205. echo -e "\t目录[${APK_SRC_DIR}]不存在,需要更新\n"
  206. return 1
  207. fi
  208. # 获取本地目录的最新提交版本;
  209. svn_local_lcr=$(svn info ${APK_SRC_DIR}|grep "Last Changed Rev" 2>&1)
  210. echo -e "\r\r获取本地目录的最新提交版本="${svn_local_lcr}
  211. svn_local_lcr=${svn_local_lcr#*Last Changed Rev: }
  212. echo -e "\r\nLocal SVN Last Changed Revision=${svn_local_lcr}"
  213. # 查找服务器的地址;
  214. cd ${APK_SRC_DIR}
  215. APK_SRC_URL=$(svn info|grep "URL:" |grep -v "Relative URL:" 2>&1)
  216. echo -e "\t${APK_SRC_URL}"
  217. APK_SRC_URL=${APK_SRC_URL#*URL:}
  218. cd -
  219. # 获取服务器的最新提交版本;
  220. svn_server_lcr=$(svn info ${APK_SRC_URL}|grep "Last Changed Rev" 2>&1)
  221. if [[ $svn_server_lcr == *"svn: E170013: Unable to connect to a repository at URL"* ]]; then
  222. echo -e "\t连接SVN服务器失败, 网络异常=${svn_server_lcr}!\n"
  223. SwitchIntranet
  224. exit
  225. fi
  226. svn_server_lcr=${svn_server_lcr#*Last Changed Rev: }
  227. echo -e "\tServer SVN Last Changed Revision=${svn_server_lcr}"
  228. # 比较版本号或日期;
  229. if [ ${svn_server_lcr} -gt ${svn_local_lcr} ];then
  230. echo -e "\t服务器版本大于本地目录版本号,需要更新"
  231. return 1
  232. fi
  233. if [ x${REBUILD} == "x1" ]; then
  234. echo -e "\t手动编译标记位"
  235. return 1
  236. fi
  237. echo -e "\t本地版本与服务器版本相同 ,不需要更新\n"
  238. return 0
  239. }
  240. IsUpdateGIT()
  241. {
  242. SwitchIntranet
  243. echo -e "\n\n==========执行函数:IsUpdate=========="
  244. # 如果目录不存在,直接返回需要更新;
  245. if [ ! -d ${APK_SRC_DIR} ]; then
  246. echo -e "\t目录[${APK_SRC_DIR}]不存在,需要更新\n"
  247. return 1
  248. fi
  249. # 获取本地目录的最新提交版本;
  250. svn_local_lcr=$(svn info ${APK_SRC_DIR}|grep "Last Changed Rev" 2>&1)
  251. echo -e "\r\r获取本地目录的最新提交版本="${svn_local_lcr}
  252. svn_local_lcr=${svn_local_lcr#*Last Changed Rev: }
  253. echo -e "\r\nLocal SVN Last Changed Revision=${svn_local_lcr}"
  254. # 查找服务器的地址;
  255. cd ${APK_SRC_DIR}
  256. APK_SRC_URL=$(svn info|grep "URL:" |grep -v "Relative URL:" 2>&1)
  257. echo -e "\t${APK_SRC_URL}"
  258. APK_SRC_URL=${APK_SRC_URL#*URL:}
  259. cd -
  260. # 获取服务器的最新提交版本;
  261. svn_server_lcr=$(svn info ${APK_SRC_URL}|grep "Last Changed Rev" 2>&1)
  262. if [[ $svn_server_lcr == *"svn: E170013: Unable to connect to a repository at URL"* ]]; then
  263. echo -e "\t连接SVN服务器失败, 网络异常=${svn_server_lcr}!\n"
  264. SwitchIntranet
  265. exit
  266. fi
  267. svn_server_lcr=${svn_server_lcr#*Last Changed Rev: }
  268. echo -e "\tServer SVN Last Changed Revision=${svn_server_lcr}"
  269. # 比较版本号或日期;
  270. if [ ${svn_server_lcr} -gt ${svn_local_lcr} ];then
  271. echo -e "\t服务器版本大于本地目录版本号,需要更新"
  272. return 1
  273. fi
  274. if [ x${REBUILD} == "x1" ]; then
  275. echo -e "\t手动编译标记位"
  276. return 1
  277. fi
  278. echo -e "\t本地版本与服务器版本相同 ,不需要更新\n"
  279. return 0
  280. }
  281. UpdateCode()
  282. {
  283. SwitchIntranet
  284. echo -e "\n\n==========执行函数:UpdateCode=========="
  285. # 本地APK SVN目录是否存在,存在则删除;
  286. if [ -d ${APK_SRC_DIR} ]; then
  287. echo -e "\t目录不为空,删除未提交的文件;\n"
  288. #rm -rf ${APK_SRC_DIR} # 删除操作太危险;
  289. svn cleanup ${APK_SRC_DIR}
  290. filelist=$(svn status ${APK_SRC_DIR} 2>&1)
  291. echo -e "未提交的文件="${filelist}
  292. # 以"A "分组字符串,并修改logo后缀;
  293. filearray=(${filelist//? /})
  294. for File in ${filearray[@]}
  295. do
  296. echo -e "\t 即将删除File="$File
  297. # 文件是否存在;
  298. if [ -f ${File} ];then
  299. rm -f ${File}
  300. fi
  301. done
  302. fi
  303. if [ x"${APK_SRC_URL}" = x ]; then
  304. echo -e "\tAPK URL空\n"
  305. SwitchIntranet
  306. exit
  307. fi
  308. #echo -e "\t开始Check代码;\n"
  309. #svn co --force ${APK_SRC_URL} ${APK_SRC_DIR}
  310. echo -e "\t开始更新代码\n"
  311. svn_result=$(svn up ${APK_SRC_DIR} 2>&1)
  312. echo -e "\r\nsvn up结果="${svn_result}
  313. if [[ $svn_result != *"At revision"* ]]; then
  314. echo -e "\t连接SVN服务器失败, 网络异常!\n"
  315. SwitchIntranet
  316. exit
  317. fi
  318. cd ${APK_SRC_DIR}
  319. # 判断文件是否存在;
  320. if [ -f "update_modules.sh" ];then
  321. echo -e "\n\tupdate_modules.sh 文件存在,更新复用库\n\n"
  322. chmod 777 update_modules.sh
  323. ./update_modules.sh
  324. fi
  325. }
  326. compile()
  327. {
  328. echo "shell_log===>Compile start---------"
  329. # 判断工作副本是否需要更新;
  330. IsUpdateSVN
  331. if [ $? == 1 ]
  332. then
  333. echo "需要更新本地SVN"
  334. UpdateCode
  335. cd ${WORK_HOME}/shell
  336. # 读取ini配置文件中的信息;
  337. releaseType=$(python3 -c 'import builder;print(builder.getReleaseName("'$APK_SRC_DIR'"))')
  338. if [ $releaseType == 'custom' ];then
  339. compileCustomApp
  340. else
  341. compileGeneralApp
  342. fi
  343. echo "shell_log===>Compile end------------"
  344. else
  345. echo "无需更新本地SVN"
  346. echo "shell_log===>No upgrade end------------"
  347. fi
  348. # 切换回内网;
  349. SwitchIntranet
  350. }
  351. main()
  352. {
  353. if [ $# -eq 1 ]; then
  354. REBUILD=0
  355. APK_SRC_DIR=$1
  356. elif [ $# -eq 2 ]; then
  357. APK_SRC_DIR=$1
  358. REBUILD=$2
  359. fi
  360. echo -e "########################################################开始编译########################################################\n"
  361. # 导出变量到子shell;
  362. export WORK_HOME #/home/data/ApkAutoCompile
  363. export GRADLE_HOME #/home/data/android/gradle/gradle-6.7.1
  364. export ANDROID_SDK_HOME #/home/data/android/android_sdk/android-sdk-linux
  365. # 创建 编译完成的Apk文件 根目录;
  366. if [ ! -d ${APK_ROOT_DIR} ];then
  367. mkdir -p ${APK_ROOT_DIR}
  368. fi
  369. # Apk Debug目录不用创建,但Release根目录需要;
  370. if [ ! -d ${APK_RELEASE_ROOT_DIR} ];then
  371. mkdir -p ${APK_RELEASE_ROOT_DIR}
  372. fi
  373. # 检出所有debug apk;
  374. if [ ! -d ${APK_DEBUG_ROOT_DIR} ];then
  375. svn co ${APK_DEBUG_ROOT_URL} ${APK_DEBUG_ROOT_DIR}
  376. fi
  377. # 检出中间件源码;
  378. if [ ! -d ${APK_DIR_TVMidware} ];then
  379. echo -e 'svn co TVMidware Apk'
  380. svn co https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/TVMidware/debug/TVMidwareService ${APK_DIR_TVMidware}
  381. fi
  382. # 取右边第一个字符,判断是否为斜杠;
  383. if [ "${APK_SRC_DIR:0-1:1}" = "/" ];then
  384. # 若为斜杠,%号截取,删除右边第一个'/'及右边字符,保存左边字符;
  385. echo "最后为斜杠,删除斜杠"
  386. APK_SRC_DIR=${APK_SRC_DIR%/*}
  387. fi
  388. # 获取APK名称;
  389. APK_NAME=${APK_SRC_DIR##*/}
  390. echo -e "APK_SRC_DIR=${APK_SRC_DIR}\r\nAPK_NAME=${APK_NAME}"
  391. # 重新定义log文件;
  392. LOG_FILE="/home/data/ApkAutoCompile/shell/log/gradlew-${APK_NAME}.log"
  393. compile 2>&1 | tee ${LOG_FILE}
  394. echo -e "########################################################编译结束########################################################\n"
  395. }
  396. main $@