1 #!/bin/bash
2 ###############################################################
3 ### 通用代码
4
5 ## 显示错误
6 function error(){
7 local msg=$1
8 if [[ ${msg} != '' ]]; then
9 echo -e "[\033[31mERROR\033[0m] "$1
10 fi
11 }
12
13 ##显示调试信息
14 function debug(){
15 local msg=$1
16 if [[ ${msg} != '' ]]; then
17 echo -e "[\033[34mMESSAGE\033[0m] "$1
18 fi
19 }
20
21 ##显示成功的信息
22 function ok() {
23 local msg=$1
24 if [[ ${msg} != '' ]]; then
25 echo -e "[\033[32mOK\033[0m] "$1
26 fi
27 }
28
29 ## 检查上一条命令的执行结果
30 function checkResult(){
31 local ret=$1
32 local successMsg=$2
33 local errormsg=$3
34 if [[ ${ret} != 0 ]]; then
35 error ${errormsg}
36 return ${ret}
37 else
38 ok ${successMsg}
39 return 0
40 fi
41 }
42
43 ## 检查上一条命令的反选结果。如果检查到有失败的消息就停止执行后面的任务
44 function checkResultAndEchoFailMsgAndExit(){
45 local ret=$1
46 if [[ ${ret} != 0 ]]; then
47 echo -e "[\033[31mERROR\033[0m] "$2 #用通用函数会导致数据显示的数据不完整。我也不知道是为什么。
48 exit ${ret}
49 fi
50 }
51
52 ## 根据时间生成文件名
53 function getTimerFolderName(){
54 local ret=$(date "+%y-%m-%d[%H%M%S]")
55 echo ${ret}
56 }
57
58 ## 检查目录是否存在,不存在会创建 一个目录
59 function checkFolder(){
60 local dirName=$1
61 if [ -d "${dirName}" ]; then
62 return 0
63 else
64 debug "mkdir ${dirName}"
65 mkdir -p ${dirName}
66 if [ ! -d "${dirName}" ]; then
67 error "make dir error.dirname:${dirName}"
68 return 1
69 fi
70 fi
71 return 0
72 }
73
74 ## 备份目录
75 function backup() {
76 local src=$1
77 local dic=$2
78 cp -rf ${src} ${dic}
79 if [[ $? == 0 ]]; then
80 ok "backup [${src}] to [${dic}] success."
81 return 0
82 else
83 error "backup [${src}] to [${dic}] fail."
84 return 1
85 fi
86 }
87
88 ##检查cmd命令是否存在
89 function isExistCmd(){
90 local cmd=$1
91 which ${cmd}
92 return $?
93 }
94
95 ##检查命令是否存在,如果否存在停止执行后面的任务
96 function notHasCommandExit() {
97 local cmd=$1
98 isExistCmd ${cmd}
99 checkResultAndEchoFailMsgAndExit $? "not found command:"${cmd} " . linux: try to use yum install ${cmd}"
100 }
101
102 ##使用mvn编译java
103 function buildJavaMvn(){
104 local src=$1
105 local pomXml=$2
106 local jarName=$3
107 if [[ ${src} == '' ]]; then
108 error "used mvn to build,target java project src is null."
109 return 1
110 fi
111
112 local currentWorkDir=$(pwd)
113 debug "maven compile ${src} worl dir $(pwd)"
114 cd ${src}
115 if [[ $? != 0 ]]; then
116 error "get project is not exists to build with maven."
117 fi
118
119 notHasCommandExit 'mvn' #yum install maven
120 mvn clean
121 if [[ $pomXml == '' ]]; then
122 mvn package -DskipTests -f pom.xml -X
123 else
124 mvn package -DskipTests -f pomPackage.xml -X
125 fi
126
127 cd ${currentWorkDir}
128 echo $(pwd)
129 if [[ -f "${src}/target/${jarName}" ]]; then
130 ok "build ${src} with ${pomXml} success!"
131 else
132 echo "${src}/target/${jarName}"
133 checkResultAndEchoFailMsgAndExit 1 "build ${src} fail."
134 fi
135
136
137 return 0
138 }
139
140 ## 编译golang 项目
141 function buildGOlang(){
142 local projDir=$1
143 local projName=$2
144 local outDir=$3
145
146 if [[ ${projDir} == '' ]]; then
147 error "build golang project fail, projDir is null"
148 return 1
149 fi
150
151 if [[ ${projName} == '' ]]; then
152 error "build golang project fail, projName is null"
153 return 1
154 fi
155
156 notHasCommandExit 'go'
157 checkResultAndEchoFailMsgAndExit $? "not found golang command."
158 export GOPATH=$(pwd)/${projDir}
159 go install ${projName}
160 checkResultAndEchoFailMsgAndExit $? "build golang project fail, project: ${projDir} ,project name:${projName}"
161 ok "build golang project success, project: ${projDir} ,project name:${projName}"
162 if [[ ${outDir} != '' ]]; then
163 cp -rf ${projDir}/bin/${projName} ${outDir}
164 checkResultAndEchoFailMsgAndExit $? "copy (${projDir}/bin/${projName}) to (${outDir}) fail"
165 fi
166 return 0
167 }
168
169 ## 使用cmake 编译C++项目,编译成动态库
170 function buildCMakeShareLib(){
171 local cmakeDir=$1
172 local cmakeRetName=$2
173 local buildTo=$3
174
175 if [[ ${cmakeDir} == '' ]]; then
176 checkResultAndEchoFailMsgAndExit 1 "cmake folder is null"
177 fi
178
179 if [[ ${cmakeRetName} == '' ]]; then
180 checkResultAndEchoFailMsgAndExit 1 "cmakeRetName is null"
181 fi
182
183 if [[ ${buildTo} == '' ]]; then
184 checkResultAndEchoFailMsgAndExit 1 "buildTo is null"
185 fi
186
187 notHasCommandExit "cmake"
188
189 local workdir=$(pwd)
190 checkFolder ${cmakeDir}"buildTemp"
191 cd ${cmakeDir}"buildTemp"
192 ok "switch to cmake word dir:${cmakeDir}buildTemp"
193 cmake ../
194 checkResultAndEchoFailMsgAndExit $? "run cmake fail. projectName:"${cmakeRetName}
195 make
196 checkResultAndEchoFailMsgAndExit $? "run make fail. projectName:"${cmakeRetName}
197 checkFolder ${buildTo}
198 cp -p "lib"${cmakeRetName}".so" ${buildTo}/"lib"${cmakeRetName}".so"
199 local ret=$?
200 if [[ $ret != 0 ]]; then
201 error "compile "${cmakeRetName}" (c/c++) fail."
202 else
203 ok "compile "${cmakeRetName}" (c/c++)success."
204 fi
205 cd ${workdir}
206 return ${ret}
207 }
208
209 # 使用make 编译c++项目
210 function buildMake(){
211 local makeDir=$1
212 local makeRetName=$2
213 local buildTo=$3
214
215 if [[ ${makeDir} == '' ]]; then
216 checkResultAndEchoFailMsgAndExit 1 "make folder is null"
217 fi
218
219 if [[ ${makeRetName} == '' ]]; then
220 checkResultAndEchoFailMsgAndExit 1 "makeRetName is null"
221 fi
222
223 if [[ ${buildTo} == '' ]]; then
224 checkResultAndEchoFailMsgAndExit 1 "buildTo is null"
225 fi
226
227 notHasCommandExit 'make'
228 local workdir=$(pwd)
229 cd ${makeDir}
230 ok "make compiler workspace:"$(pwd)
231 make
232 checkResultAndEchoFailMsgAndExit $? "run make fail."${makeRetName}
233 checkFolder ${buildTo}
234 debug "copy "${makeRetName} ${buildTo}/${makeRetName}
235 cp -p ${makeRetName} ${buildTo}/${makeRetName}
236 local ret=$?
237 if [[ $ret != 0 ]]; then
238 error "compile "${makeRetName}" (c/c++) fail."
239 else
240 ok "compile "${makeRetName}" (c/c++)success."
241 fi
242 cd ${workdir}
243 return ${ret}
244 }
245
246 #使用make编译c++项目为动态库:目前只支持linux版本
247 function buildMakeSharLib(){
248 local makeDir=$1
249 local makeRetName=$2
250 local buildTo=$3
251
252 if [[ ${makeDir} == '' ]]; then
253 checkResultAndEchoFailMsgAndExit 1 "make folder is null"
254 fi
255
256 if [[ ${makeRetName} == '' ]]; then
257 checkResultAndEchoFailMsgAndExit 1 "makeRetName is null"
258 fi
259
260 if [[ ${buildTo} == '' ]]; then
261 checkResultAndEchoFailMsgAndExit 1 "buildTo is null"
262 fi
263
264 notHasCommandExit 'make'
265 local workdir=$(pwd)
266 cd ${makeDir}
267 ok "make compiler workspace:"$(pwd)
268 checkResultAndEchoFailMsgAndExit $? "run cmake fail."${makeRetName}
269 make
270 checkResultAndEchoFailMsgAndExit $? "run make fail."${makeRetName}
271 checkFolder ${buildTo}
272 echo "copy lib"${makeRetName}".so" ${buildTo}/"lib"${makeRetName}".so"
273 cp -p "lib"${makeRetName}".so" ${buildTo}/"lib"${makeRetName}".so"
274 local ret=$?
275 if [[ $ret != 0 ]]; then
276 error "compile "${makeRetName}" (c/c++) fail."
277 else
278 ok "compile "${makeRetName}" (c/c++) success."
279 fi
280 cd ${workdir}
281 return ${ret}
282 }
283 #使用make编译c++项目为动态库:目前只支持linux版本
284 function buildMakeSharLib2(){
285 local makeDir=$1
286 if [[ ${makeDir} == '' ]]; then
287 checkResultAndEchoFailMsgAndExit 1 "make folder is null"
288 fi
289
290 notHasCommandExit 'make'
291 local workdir=$(pwd)
292 cd ${makeDir}
293 ok "make compiler workspace:"$(pwd)
294 checkResultAndEchoFailMsgAndExit $? "run cmake fail."${makeRetName}
295 make
296 checkResultAndEchoFailMsgAndExit $? "run make fail."${makeRetName}
297 cd ${workdir}
298 return 0
299 }
300
301 #使用cmake编译安装库
302 function buildMakeInstall(){
303 local makeDir=$1
304 if [[ ${makeDir} == '' ]]; then
305 checkResultAndEchoFailMsgAndExit 1 "make project folder is null"
306 fi
307 notHasCommandExit 'make'
308 local workdir=$(pwd)
309 cd ${makeDir}
310 ok "make compile & make install:"${pwd}
311 make
312 # make install
313 checkResultAndEchoFailMsgAndExit $? "make&make install makeDir fail. projName:"${cmakeRetName}
314 cd ${workdir}
315 }
316
317 ##更新SVN到本地,如果没有更新过会先checkout最新的版本
318 function updateSVN(){
319 local svndir=$1
320 local url=$2
321 export LC_CTYPE=zh_CN.UTF-8 #设置编码
322 debug "update from svn:"${url}
323 if [ ! -d "${svndir}" ]; then
324 svn co ${url}
325 else
326 svn up ${svndir}
327 fi
328 if [[ $? == 0 ]]; then
329 ok "update from svn success, url:"${url}
330 else
331 ok "update from svn fail, url:"${url}
332 fi
333 }
334
335 ## 进度条
336 function processBar(){
337 b=''
338 for ((i=0;$i<=100;i+=2))
339 do
340 printf "progress:[%-50s]%d%%\r" $b $i
341 sleep 0.1
342 b=#$b
343 done
344 echo
345 }
346
347 ##显示进度条
348 b=''
349 function showProgress(){
350 local val=$1 #0~100
351 if [[ ${val} > 0 || ${val} <=100 ]]; then
352 printf "progress:[%-50s]%d%%\r" $b ${val}
353 b=#$b
354 else
355 error "process value verify fail. value:"$val
356 fi
357
358 #完成后换行
359 if [[ ${val} == 100 ]] ; then
360 echo
361 fi
362 }
363
364
365
366 # ret=$(getTimerFolderName)
367 # echo ${ret}
368
369 ####################################################################################
370 ### 下面的全为业务代码
371 VERSION=0.0.1 #当前版本
372 BACKUP_DIR="backup" #备份目录
373 PUBLIC_DIR="public/"${VERSION} #发布目录
374 PUBLIC_TARGET_PLATFORM="linux" #当前版本为linux版本
375 ENABLE_BACK=false #编译前是否备份
376
377 function checkEvn(){
378
379 checkFolder ${PUBLIC_DIR}
380 checkResultAndEchoFailMsgAndExit $? "检查发布版本目录失败!"
381
382 checkFolder ${BACKUP_DIR}
383 checkResultAndEchoFailMsgAndExit $? "检查备份目录失败!"
384
385 return 0
386 }
387
388 function checkBackupVersion(){
389 local max_backup_days=$1 #保留版本的天数
390 ##TODO 删除太旧的版本,只保留规定时间内的版本
391 }
392
393 function main(){
394 debug ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
395 debug "检查编译环境"
396 checkEvn
397
398 debug "开始编译"
399 #备份game server
400 if [[ ${ENABLE_BACK} == true ]]; then
401 if [[ -d 'sg_svr' ]]; then
402 local backup_dir=${BACKUP_DIR}/$(date "+%Y-%m-%d%H%M%S")
403 debug '备份目录:'${backup_dir}
404 mkdir -p ${backup_dir}
405 backup 'sg_svr' "${backup_dir}/sg_svr"
406 checkResultAndEchoFailMsgAndExit $? "备份sg_svr败!"
407 else
408 debug "本地没有版本,无须备份!"
409 fi
410 fi
411
412 local GAMEBLL_PROJ='https://192.168.10.234:8443/svn/kdyg/kd_project/SourceCode/server/sg_svr'
413
414 #更新golang 项目
415 updateSVN 'sg_svr' $GAMEBLL_PROJ
416 checkResultAndEchoFailMsgAndExit $? "从svn更新sg_svr失败!"
417
418 # #编译golang依赖的一些本地库,无论是否有新版本都会检查一下并编译,如果为新版本,编译时间可能很长。
419 debug ">>>>> current workdir $(pwd)"
420 # buildMakeInstall "sg_svr/c_battle/src/luajit-2.1"
421 buildMakeSharLib2 "sg_svr/c_battle/src/luajit-2.1"
422 checkResultAndEchoFailMsgAndExit $? "编译luajit-2.1失败!"
423 debug ">>>>> current workdir $(pwd)"
424 cp -rf "sg_svr/c_battle/src/luajit-2.1/src/libluajit.so" "sg_svr/bin/${PUBLIC_TARGET_PLATFORM}/libluajit-5.1.so.2"
425 checkResultAndEchoFailMsgAndExit $? "copy luajit-2.1失败!"
426 buildMakeSharLib "sg_svr/c_battle/linux/" "c_battle" $(pwd)"/sg_svr/bin/"${PUBLIC_TARGET_PLATFORM}
427 checkResultAndEchoFailMsgAndExit $? "编译c_battle失败!"
428 debug ">>>>> current workdir $(pwd)"
429 buildCMakeShareLib "sg_svr/c_lua51/src/" "lua51" $(pwd)"/sg_svr/bin/"${PUBLIC_TARGET_PLATFORM}
430 checkResultAndEchoFailMsgAndExit $? "编译lua51失败!"
431 #编译golang 登陆网关服
432 debug ">>>>> current workdir $(pwd)"
433 buildGOlang "sg_svr" "sg_loginsvr" $(pwd)"/sg_svr/bin/"${PUBLIC_TARGET_PLATFORM}
434 checkResultAndEchoFailMsgAndExit $? "编译sg_logsvr失败!"
435 #编译golang 游戏服
436 debug ">>>>> current workdir $(pwd)"
437 buildGOlang "sg_svr" "sg_gamesvr" $(pwd)"/sg_svr/bin/"${PUBLIC_TARGET_PLATFORM}
438 checkResultAndEchoFailMsgAndExit $? "编译sg_gamesvr失败!"
439
440 #备份gmserver
441 if [[ ${ENABLE_BACK} == true ]]; then
442 if [[ -d "server" ]]; then
443 backup 'server' "${backup_dir}/server"
444 checkResultAndEchoFailMsgAndExit $? "备份server失败!"
445 else
446 debug "本地无版本,无须备份."
447 fi
448 fi
449
450 updateSVN 'server' 'https://192.168.10.234:8443/svn/kdyg/HYSDK/server'
451 checkResultAndEchoFailMsgAndExit $? "从svn更新server失败!"
452 debug ">>>>> current workdir $(pwd)"
453 buildJavaMvn 'server/gm-tools' 'pomPackage.xml' "gametool-${VERSION}-SNAPSHOT.jar"
454 checkResultAndEchoFailMsgAndExit $? "编译gm-tools失败!"
455 buildJavaMvn 'server/sdkservices' 'pomPackage.xml' "sdkservices-${VERSION}-SNAPSHOT.jar"
456 checkResultAndEchoFailMsgAndExit $? "编译sdkservices失败!"
457 #copy 编译版本到发布目录
458 ok "编译完成"
459
460 debug "发布版本"
461 # local currentDir=$(pwd)
462 if [[ -d ${PUBLIC_DIR}/${PUBLIC_TARGET_PLATFORM} ]]; then
463 read -r -p "目录已存在,是否删除旧的版本? [Y/n] " input
464 case $input in
465 [yY][eE][sS]|[yY])
466 #echo "Yes"
467 rm -rf ${PUBLIC_DIR}/${PUBLIC_TARGET_PLATFORM}
468 ;;
469
470 [nN][oO]|[nN])
471 #echo "No"
472 exit 0;
473 ;;
474
475 *)
476 echo "Invalid input..."
477 ;;
478 esac
479 fi
480
481 cp -rf "sg_svr/bin/${PUBLIC_TARGET_PLATFORM}/" "${PUBLIC_DIR}/${PUBLIC_TARGET_PLATFORM}"
482 checkResultAndEchoFailMsgAndExit $? "发布sg_svr失败!"
483 cp -rf "server/gm-tools/target/gametool-${VERSION}-SNAPSHOT.jar" "${PUBLIC_DIR}/${PUBLIC_TARGET_PLATFORM}/gametool-${VERSION}-SNAPSHOT.jar"
484 checkResultAndEchoFailMsgAndExit $? "发布gm-tools失败!"
485 cp -rf "server/sdkservices/target/sdkservices-${VERSION}-SNAPSHOT.jar" "${PUBLIC_DIR}/${PUBLIC_TARGET_PLATFORM}/sdkservices-${VERSION}-SNAPSHOT.jar"
486 checkResultAndEchoFailMsgAndExit $? "发布sdkservices失败!"
487
488 #检查备份处理的情况,如果大陈旧就删除
489 checkBackupVersion
490
491 ok "发布成功,当前版本为:${VERSION}"
492 }
493 main