Files
chain33-dtalk/script/build/util.sh
2022-03-17 15:59:24 +08:00

131 lines
3.4 KiB
Bash

#!/bin/bash
# 检查服务名称是否合法
checkValid() {
valid_services=("backend")
#"apply" "department" "enterprise" "staff")
service_name="$1"
is_valid="no"
for valid_service in "${valid_services[@]}"; do
if [ "${valid_service}" == "${service_name}" ]; then
is_valid="yes"
fi
done
if [ "${is_valid}" == "no" ]; then
return 1
else
return 0
fi
}
# 获取编译参数
getFlags() {
main_path="main"
go_version=$(go version | awk '{ print $3 }')
build_time=$(date "+%Y-%m-%d %H:%M:%S %Z")
git_commit=$(git rev-parse --short=10 HEAD)
flags="-X '${main_path}.goVersion=${go_version}' -X '${main_path}.buildTime=${build_time}' -X '${main_path}.gitCommit=${git_commit}' -X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'"
echo "${flags}"
}
# 创建目标目录
mkPkg() {
file_path="$1"
pkg_name="$2"
rm -rf ${file_path}/${pkg_name}
mkdir -pv "${file_path}/${pkg_name}"/bin
mkdir -pv "${file_path}/${pkg_name}"/etc
}
# 编译服务
buildService() {
file_path="$1"
pkg_name="$2"
service_name="$3"
version="$4"
sub_file_path="$5"
flags=$(getFlags)
suffix=""
if [ -n "${version}" ]; then
suffix="-${version}"
fi
cd "${file_path}"/service/"${sub_file_path}""${service_name}"/cmd || exit
echo "start building ${service_name} service"
# echo ${flags}
# go build -ldflags "${flags}" -o "${file_path}/${pkg_name}/bin/${service_name}${suffix}" || exit
go build -ldflags "${flags}" -o "${file_path}/${pkg_name}/bin/${service_name}${suffix}" || exit
echo "building ${service_name} service success"
cp "${file_path}/service/${sub_file_path}${service_name}/config/${service_name}".toml "${file_path}/${pkg_name}/etc/${service_name}".toml
}
# 编译gateway服务
buildGateway() {
file_path="$1"
pkg_name="$2"
service_name="$3"
version="$4"
flags=$(getFlags)
suffix=""
if [ -n "${version}" ]; then
suffix="-${version}"
fi
cd "${file_path}/gateway/api/${version}" || exit
echo "start building ${service_name} service"
go build -ldflags "${flags}" -o "${file_path}/${pkg_name}/bin/${service_name}" || exit
echo "building ${service_name} service success"
cp "${file_path}/gateway/api/${version}/etc/${service_name}".toml "${file_path}/${pkg_name}/etc/${service_name}".toml
}
# 复制所选文件
cpIfExist() {
file_path="$1"
pkg_name="$2"
files="$3"
for file in "${file_path}"/configs/${files}; do
if [ -f "${file}" ]; then
cp "${file}" "${file_path}/${pkg_name}"/etc
fi
done
}
# 打包目标目录
tarPkg() {
file_path="$1"
pkg_name="$2"
file_name="$3"
is_single_service="$4"
# if [ "${is_single_service}" == "yes" ]; then
# content="$(cat "${file_path}"/configs/Makefile-single-service)"
# echo "${content//single/${file_name}}" > "${file_path}/${pkg_name}"/Makefile
# else
# cp "${file_path}/configs/Makefile-${file_name}" "${file_path}/${pkg_name}"/Makefile
# fi
# cp "${file_path}"/README.md "${file_path}/${pkg_name}"
tar -zcvPf "${file_path}/${pkg_name}".tar.gz -C "${file_path}" "${pkg_name}"/etc "${pkg_name}"/bin
# "${pkg_name}"/Makefile
# rm -r "${file_path}/${pkg_name}"
}
# 设置目标打包环境
# 默认 linux amd64
initOS() {
env_type="amd64"
if [ -n "$1" ]; then
env_type="$1"
fi
export GOOS=linux
export GOARCH=${env_type}
export GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn
echo "linux_${env_type}"
}