#! /bin/sh # 源码地址; ARG_CODE_URL='' # 源码存储方式:SVN、GIT ARG_CODE_REPOS='SVN' # 源码版本; ARG_CODE_REVISION='' # 源码本地路径; ARG_CODE_WORK_PATH='' # 日期; CURR_TIME=$(date +"%Y-%m-%d %T") #LOG_FILE=/home/data/ApkAutoCompile/shell/log/download_$(date '+%d-%H%M%S').log LOG_FILE=/home/data/ApkAutoCompile/shell/log/download.log StrIsSubstring() { local x=1 case "$2" in *$1*) x=0;; esac echo $x } SwitchIntranet() { echo -e "\r\r---------------切换到内网---------------" route del default gw 10.201.40.1 sleep 2s route del default gw 10.201.40.1 sleep 1s route add default gw 10.201.40.1 sleep 2s route add default gw 10.201.40.1 } svn_clean() { SVN_PATH=$1 if [ -d ${SVN_PATH} ];then cd ${SVN_PATH} # 清空; msg=$(svn cleanup .) # 查找未提交的文件; filelist=$(svn status 2>&1) echo -e "未提交的文件="${filelist} # 以"? "分组字符串; filearray=(${filelist//? /}) for File in ${filearray[@]} do # 文件是否存在; if [ -f ${File} ];then rm -f ${File} echo -e "已删除文件=${File}" elif [ -d ${File} ];then rm -rf ${File} echo -e "已删除路径=${File}" fi done else echo -e "svn clean: 路径不存在${SVN_PATH}\n" fi } git_clean() { echo "" } svn_download() { local Opt=$1 echo ${CURR_TIME} echo -e "shell_log===>Download start---------\n" echo "AndroidSvnPath=${ARG_CODE_URL}" echo "AndroidSvnRevision=${ARG_CODE_REVISION}" echo -e "AndroidCachePath=${ARG_CODE_WORK_PATH}\n" if [ $Opt -eq 1 ];then echo -e "删除重新下载Code" rm -rf ${ARG_CODE_WORK_PATH} fi # 如果路径名是个文件,删除; if [ -f ${ARG_CODE_WORK_PATH} ];then echo -e "删除已有路径[${ARG_CODE_WORK_PATH}]代码\n" rm -rf ${ARG_CODE_WORK_PATH} sleep 1s fi # 如果路径存在,只更新; if [ -d ${ARG_CODE_WORK_PATH} ];then # clean清空; svn_clean ${ARG_CODE_WORK_PATH} cd ${ARG_CODE_WORK_PATH} echo -e "svn cleanup msg="${msg}"\n" if [ ${ARG_CODE_REVISION} != "" -a ${ARG_CODE_REVISION} != "0" ];then echo -e "update the specified version!\n" msg=$(svn up -r ${ARG_CODE_REVISION} --force 2>&1) else echo -e "update the latest version!\n" msg=$(svn up --force 2>&1) fi else #### 如果路径不存在,checkout; if [ ${ARG_CODE_REVISION} != "" -a ${ARG_CODE_REVISION} != "0" ];then echo -e "download the specified version!\n" msg=$(svn co -r ${ARG_CODE_REVISION} --force ${ARG_CODE_URL} ${ARG_CODE_WORK_PATH} 2>&1) else echo -e "download the latest version!\n" msg=$(svn co --force ${ARG_CODE_URL} ${ARG_CODE_WORK_PATH} 2>&1) fi fi echo -e "svn msg=${msg}\n" # 修改update_modules.sh权限; if [ ! -d ${ARG_CODE_WORK_PATH} ];then echo -e "下载代码[${ARG_CODE_URL}]失败" else cd ${ARG_CODE_WORK_PATH} update_modules fi echo -e "\nshell_log===>Download end------------" } git_download() { local Opt=$1 echo ${CURR_TIME} echo "shell_log===>Download start---------" echo "AndroidSvnPath=${ARG_CODE_URL}" echo "AndroidSvnRevision=${ARG_CODE_REVISION}" echo "AndroidCachePath=${ARG_CODE_WORK_PATH}" if [ ${Opt} -eq 1 ]; then echo -e "删除重新下载Code" rm -rf ${ARG_CODE_WORK_PATH} # 重新下载代码; msg=$(git clone ${ARG_CODE_URL} ${ARG_CODE_WORK_PATH} 2>&1) echo "git clone msg: $msg\n" if [[ $msg != *"Total "* ]]; then echo -e "1.下载代码[${ARG_CODE_URL}]失败" exit fi else # 更新代码; if [ -d ${ARG_CODE_WORK_PATH} ];then cd ${ARG_CODE_WORK_PATH} msg=$(git pull origin master 2>&1) echo -e "git pull msg=${msg}\n" if [[ $msg != *"Already up to date."* ]]; then echo -e "1.更新代码[${ARG_CODE_URL}]失败" exit fi else # 重新下载代码; msg=$(git clone ${ARG_CODE_URL} ${ARG_CODE_WORK_PATH} 2>&1) echo "git clone msg: $msg\n" if [[ $msg != *"Total "* ]]; then echo -e "2.下载代码[${ARG_CODE_URL}]失败" exit fi fi fi if [ ${ARG_CODE_REVISION} != "" -a ${ARG_CODE_REVISION} != "0" ];then echo "download the specified version!" # 切换到指定commit id; msg=$(git checkout -b master ${ARG_CODE_REVISION} 2>&1) echo "git checkout msg: $msg\n" else echo "download the latest version!" fi if [ ! -d ${ARG_CODE_WORK_PATH} ];then echo -e "2.下载代码[${ARG_CODE_URL}]失败" else cd ${ARG_CODE_WORK_PATH} update_modules fi echo "shell_log===>Download end------------" } update_svn_modules() { local name=$1 # 模块名称; local url=$2 # 模块地址; local revision=$3 # 模块版本; echo -e "on func:update_svn_modules:${name},${url},${revision}\n" # 判断模块名文件夹是否存在; if [ -d ${name} ];then echo -e "${name}->存在\n" if [ ${revision} != "" -a ${revision} != "0" ];then echo -e "update the specified version!\n" msg=$(svn up -r ${revision} --force 2>&1) else echo -e "update the latest version!\n" msg=$(svn up --force 2>&1) fi echo -e "svn up msg=${msg}\n" else echo -e "${name}->不存在\n" if [ ${revision} != "" -a ${revision} != "0" ];then echo -e "download the specified version!\n" msg=$(svn co -r ${revision} --force ${url} ${name} 2>&1) else echo -e "download the latest version!\n" msg=$(svn co --force ${url} ${name} 2>&1) fi echo -e "svn up msg=${msg}\n" fi } update_git_modules() { local name=$1 # 模块名称; local url=$2 # 模块地址; local revision=$3 # 模块版本; local subdir=$4 # 仓库子路径; echo -e "on func:update_git_modules:${name},${url},${revision},${subdir}\n" if [ x${subdir} == x"" ];then echo -e "git clone all repo\n" # 判断模块名文件夹是否存在; if [ -d ${name} ];then echo -e "${name}->存在\n" # 先更新; msg=$(git pull origin master 2>&1) echo "git pull msg: $msg\n" if [ ${revision} != "" -a ${revision} != "0" ];then msg=$(git checkout -b master ${revision} 2>&1) echo "git checkout msg: $msg\n" else echo -e "update the latest version!\n" fi else echo -e "${name}->不存在\n" # 先clone; msg=$(git clone ${url} ${name} 2>&1) echo "git clone msg: $msg\n" if [ ${revision} != "" -a ${revision} != "0" ];then msg=$(git checkout -b master ${revision} 2>&1) echo "git checkout msg: $msg\n" else echo -e "download the latest version!\n" fi fi else echo -e "git clone sub dir\n" if [ ! -d ${name} ];then mkdir ${name} fi cd ${name} # 初始化空仓库; git init # 拉取远程仓库; git remote add -f origin ${url} # 开启sparse clone; git config core.sparsecheckout true # 设置要pull的目录; echo ${subdir} >> .git/info/sparse-checkout more .git/info/sparse-checkout # 更新; git pull origin master fi } update_modules() { echo -e "### on func:update_modules ###\n" # 进入源码路径; cd ${ARG_CODE_WORK_PATH} # 判断modules_svn.cfg是否存在; if [ -f "modules_svn.cfg" ];then local git_user=$(git config --global --list |grep user.name|cut -d'=' -f2) local git_email=$(git config --global --list |grep user.email|cut -d'=' -f2) echo "user.name=${git_user}, user.email=${git_email}" local module_name= module_url= module_ver= module_subdir= # 读取git config --global --list内容; 从中获取到用户名、邮箱; # git clone ssh://邮箱名@10.126.16.60:29418/TIF_ATSCInput.git ################################################################################################################### # SVN的cfg文件如下; # modules_svn.cfg: [MODULE],UDP,https://odm-design-center-hz.tclking.com/svn/scbc_apps/trunk/app/src/UDP/udp,6265 ################################################################################################################### # GIT的cfg文件如下:比SVN多出的一项是该仓库下的子目录; # modules_svn.cfg: [MODULE],UDP,ssh://apk@10.126.16.60:29418/UDP,6265,udp # modules_svn.cfg: [MODULE],firebaseanalytisc,ssh://apk@10.126.16.60:29418/firebaseanalytisc,6255,firebaseanalytisc # 按行读取; while read line do val=`echo $line|sed 's/[\r\n]*$//g'` echo "行内容:$val" if [ `StrIsSubstring "[MODULE]" $val` -ne 0 ]; then echo "Warning: No Module Format in - " + $val continue fi # 获取模块名称; val=${val#*,} module_name=${val%%,*} # 获取模块地址; val=${val#*,} module_url=${val%%,*} # 判断模块存储仓库类型; if [[ $module_url = *"https://odm-design-center-hz.tclking.com"* ]];then echo "源码为svn" # 获取版本; if [ `StrIsSubstring "," $val` -eq 0 ]; then module_ver=${val#*,} else module_ver="" fi echo -e "module_name=${module_name}\nmodule_url=${module_url}\nmodule_ver=${module_ver}" update_svn_modules ${module_name} ${module_url} ${module_ver} elif [[ $module_url = *"10.126.16.60"* ]];then echo "源码为git" # 获取版本; val=${val#*,} module_ver=${val%%,*} # 获取子路径; if [ `StrIsSubstring "," $val` -eq 0 ]; then module_subdir=${val#*,} else module_subdir="" fi echo -e "module_name=${module_name}\nmodule_url=${module_url}\nmodule_ver=${module_ver}\n${module_subdir}" update_git_modules ${module_name} ${module_url} ${module_ver} ${module_subdir} fi done < "modules_svn.cfg" else echo -e "modules_svn.cfg 模块文件不存在\n" fi } main() { ARG_CODE_URL=$1 ARG_CODE_REVISION=$2 ARG_CODE_WORK_PATH=$3 ############################## 获取APK名称 ############################################# APK_NAME=$ARG_CODE_URL # 取右边第一个字符,判断是否为斜杠; if [ "${APK_NAME:0-1:1}" = "/" ];then # 若为斜杠,%号截取,删除右边第一个'/'及右边字符,保存左边字符; echo "最后为斜杠,删除斜杠" APK_NAME=${APK_NAME%/*} fi # 获取APK名称; APK_NAME=${APK_NAME##*/} # 重新定义log文件; if [ ! -d /home/data/ApkAutoCompile/shell/log/download/ ];then msg=$(mkdir /home/data/ApkAutoCompile/shell/log/download/last) fi LOG_FILE="/home/data/ApkAutoCompile/shell/log/download/${APK_NAME}.log" ############################## 获取APK名称 ############################################# if [ ! -d /home/data/ApkAutoCompile/cacheAndroidSrc ];then # 递归创建目录; mkdir -p /home/data/ApkAutoCompile/cacheAndroidSrc fi SwitchIntranet if [[ $ARG_CODE_URL = *"https://odm-design-center-hz.tclking.com"* ]];then echo "源码为svn" ARG_CODE_REPOS='SVN' svn_download 1 2>&1 | tee ${LOG_FILE} # 导出svn提交账号; cd ${ARG_CODE_WORK_PATH} svn log --xml | grep "^' '{print $2}' | awk -F '' '{print $1}' > userinfo.txt elif [[ $ARG_CODE_URL = *"10.126.16.60"* ]];then echo "源码为git" ARG_CODE_REPOS='GIT' git_download 1 2>&1 | tee ${LOG_FILE} fi } main $@