2017-04-25 01:29:14 +00:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
2017-04-25 09:22:54 +00:00
|
|
|
|
# set -x -u
|
2017-04-25 01:29:14 +00:00
|
|
|
|
# 构建应用, 生成压缩包 gocron.zip或gocron.tar.gz
|
2017-04-25 09:22:54 +00:00
|
|
|
|
# ./build.sh -p windows -a amd64
|
2017-04-25 12:06:11 +00:00
|
|
|
|
# 参数含义
|
2017-05-08 06:07:06 +00:00
|
|
|
|
# -p 指定平台(windows|linux|darwin)
|
2017-04-25 12:06:11 +00:00
|
|
|
|
# -a 指定体系架构(amd64|386), 默认amd64
|
|
|
|
|
|
2017-04-25 01:29:14 +00:00
|
|
|
|
|
|
|
|
|
TEMP_DIR=`date +%s`-temp-`echo $RANDOM`
|
|
|
|
|
|
|
|
|
|
# 目标平台 windows,linux,darwin
|
|
|
|
|
OS=''
|
|
|
|
|
# 目标平台架构
|
|
|
|
|
ARCH=''
|
|
|
|
|
# 应用名称
|
|
|
|
|
APP_NAME='gocron'
|
|
|
|
|
# 可执行文件名
|
|
|
|
|
EXEC_NAME=''
|
|
|
|
|
# 压缩包名称
|
|
|
|
|
COMPRESS_FILE=''
|
|
|
|
|
|
2017-04-25 11:54:06 +00:00
|
|
|
|
|
2017-04-25 01:29:14 +00:00
|
|
|
|
# -p 平台 -a 架构
|
|
|
|
|
while getopts "p:a:" OPT;
|
|
|
|
|
do
|
|
|
|
|
case $OPT in
|
|
|
|
|
p) OS=$OPTARG
|
|
|
|
|
;;
|
|
|
|
|
a) ARCH=$OPTARG
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [[ -z $OS ]];then
|
|
|
|
|
echo "平台不能为空"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ $OS != 'windows' && $OS != 'linux' && $OS != 'darwin' ]];then
|
|
|
|
|
echo '平台错误,支持的平台 windows linux darmin(osx)'
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ -z $ARCH ]];then
|
|
|
|
|
ARCH='amd64'
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ $ARCH != '386' && $ARCH != 'amd64' ]];then
|
|
|
|
|
echo 'arch错误,仅支持 386 amd64'
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo '开始编译'
|
2017-05-17 05:34:44 +00:00
|
|
|
|
GOOS=$OS GOARCH=$ARCH go build -ldflags '-w'
|
|
|
|
|
if [[ $? != 0 ]];then
|
2017-04-25 01:29:14 +00:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
echo '编译完成'
|
|
|
|
|
|
|
|
|
|
if [[ $OS = 'windows' ]];then
|
|
|
|
|
EXEC_NAME=${APP_NAME}.exe
|
2017-05-05 09:10:14 +00:00
|
|
|
|
COMPRESS_FILE=${APP_NAME}-${OS}-${ARCH}.zip
|
2017-04-25 01:29:14 +00:00
|
|
|
|
else
|
|
|
|
|
EXEC_NAME=${APP_NAME}
|
2017-05-05 09:10:14 +00:00
|
|
|
|
COMPRESS_FILE=${APP_NAME}-${OS}-${ARCH}.tar.gz
|
2017-04-25 01:29:14 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2017-04-25 11:54:06 +00:00
|
|
|
|
mkdir -p $TEMP_DIR/$APP_NAME
|
2017-05-17 05:34:44 +00:00
|
|
|
|
if [[ $? != 0 ]]; then
|
2017-04-25 01:29:14 +00:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 需要打包的文件
|
2017-04-26 01:03:54 +00:00
|
|
|
|
PACKAGE_FILENAME=(conf log public data templates ${EXEC_NAME})
|
2017-04-25 01:29:14 +00:00
|
|
|
|
|
|
|
|
|
echo '复制文件到临时目录'
|
|
|
|
|
# 复制文件到临时目录
|
|
|
|
|
for i in ${PACKAGE_FILENAME[*]}
|
|
|
|
|
do
|
2017-04-25 11:54:06 +00:00
|
|
|
|
cp -r $i $TEMP_DIR/$APP_NAME
|
2017-04-25 01:29:14 +00:00
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# 删除运行时产生的文件
|
2017-04-25 11:54:06 +00:00
|
|
|
|
rm -rf $TEMP_DIR/$APP_NAME/conf/*
|
|
|
|
|
rm -rf $TEMP_DIR/$APP_NAME/log/*
|
2017-04-26 01:03:54 +00:00
|
|
|
|
rm -rf $TEMP_DIR/$APP_NAME/data/sessions/*
|
2017-05-01 06:12:21 +00:00
|
|
|
|
rm -rf $TEMP_DIR/$APP_NAME/data/ssh/password/*
|
|
|
|
|
rm -rf $TEMP_DIR/$APP_NAME/data/ssh/private_key/*
|
2017-04-25 01:29:14 +00:00
|
|
|
|
|
|
|
|
|
echo '压缩文件'
|
|
|
|
|
# 压缩文件
|
|
|
|
|
cd $TEMP_DIR
|
|
|
|
|
if [[ $OS = 'windows' ]];then
|
2017-04-25 11:54:06 +00:00
|
|
|
|
zip -rq $COMPRESS_FILE *
|
2017-04-25 01:29:14 +00:00
|
|
|
|
else
|
2017-04-25 11:54:06 +00:00
|
|
|
|
tar czf $COMPRESS_FILE *
|
2017-04-25 01:29:14 +00:00
|
|
|
|
fi
|
|
|
|
|
mv $COMPRESS_FILE ../
|
2017-04-25 11:54:06 +00:00
|
|
|
|
cd ../
|
2017-04-25 01:29:14 +00:00
|
|
|
|
|
|
|
|
|
rm $EXEC_NAME
|
|
|
|
|
rm -rf $TEMP_DIR
|
2017-04-25 11:54:06 +00:00
|
|
|
|
|
|
|
|
|
echo '打包完成'
|
|
|
|
|
echo '生成压缩文件--' $COMPRESS_FILE
|