Skip to content

Commit dfb0e82

Browse files
perf: 优化构建脚本
1 parent 61c307f commit dfb0e82

1 file changed

Lines changed: 56 additions & 56 deletions

File tree

build.sh

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,101 @@
11
#!/bin/bash
22

33

4-
# 定义函数
54
build_and_zip() {
6-
local config1_file="${base_dir}/config/machine-demo.csv"
7-
local config2_file="${base_dir}/config/machine-demo.yml"
8-
95
local goos=$1
106
local arch=$2
117
local output_binary=$3
128
local version=$4
139
local zip_file="jms_inspect_${goos}_${arch}"
1410

1511
if [ -n "$version" ]; then
16-
zip_file="${zip_file}_${version}"
12+
zip_file="${zip_file}_${version}"
1713
fi
1814
zip_file="${zip_file}.zip"
1915

2016
echo "开始编译 ${goos}-${arch} 版本脚本"
21-
bash -c "CGO_ENABLED=0 GOOS=${goos} GOARCH=${arch} go build -o ${output_binary} ${base_dir}/pkg/cmd/inspect.go"
17+
CGO_ENABLED=0 GOOS=${goos} GOARCH=${arch} go build -o ${output_binary} ${base_dir}/pkg/cmd/inspect.go
2218

23-
# shellcheck disable=SC2181
2419
if [ $? -ne 0 ]; then
2520
echo "编译失败,请检查错误信息。"
2621
exit 1
2722
fi
2823

29-
zip -r "${zip_file}" "${output_binary}" "${config1_file}" "${config2_file}"
24+
local temp_dir=$(mktemp -d)
25+
mkdir -p "${temp_dir}/config"
26+
cp "${output_binary}" "${temp_dir}/"
27+
cp "${base_dir}/config/machine-demo.csv" "${temp_dir}/config/"
28+
cp "${base_dir}/config/machine-demo.yml" "${temp_dir}/config/"
29+
30+
(cd "${temp_dir}" && zip -r "${base_dir}/${zip_file}" .)
3031

3132
if [ $? -eq 0 ]; then
32-
mv "${zip_file}" "${base_dir}/release/"
33-
rm "${output_binary}"
34-
echo -e "${goos} 版本脚本编译完成,生成的 ZIP 文件为 ${zip_file}\n"
33+
mv "${base_dir}/${zip_file}" "${release_dir}/"
34+
rm -rf "${temp_dir}" "${output_binary}"
35+
echo "${goos}-${arch} 版本脚本编译完成,生成的 ZIP 文件为 ${release_dir}/${zip_file}"
3536
else
3637
echo "压缩失败,请检查错误信息。"
38+
rm -rf "${temp_dir}"
3739
exit 1
3840
fi
3941
}
4042

4143
change_version() {
42-
local version=$1
43-
if [ -n "$version" ]; then
44-
if [[ $(uname) == "Darwin" ]]; then
45-
sed -i '' "s/const version = \"dev\"/const version = \"$version\"/" "pkg/cmd/inspect.go"
46-
else
47-
sed -i "s/const version = \"dev\"/const version = \"$VERSION\"/" "pkg/cmd/inspect.go"
44+
local version=$1
45+
if [ -n "$version" ]; then
46+
local sed_file="${base_dir}/pkg/cmd/inspect.go"
47+
if [[ $(uname) == "Darwin" ]]; then
48+
sed -i '' "s/const version = \"dev\"/const version = \"$version\"/" "$sed_file"
49+
else
50+
sed -i "s/const version = \"dev\"/const version = \"$version\"/" "$sed_file"
51+
fi
4852
fi
49-
fi
5053
}
5154

5255
create_dirs() {
53-
release_dir="${base_dir}/release"
54-
mkdir -p "$release_dir"
56+
release_dir="${base_dir}/release"
57+
mkdir -p "$release_dir"
5558
}
5659

5760
compile() {
58-
local version=$1
59-
os_arch_combinations=(
60-
"darwin amd64"
61-
"darwin arm64"
62-
"linux amd64"
63-
"linux arm64"
64-
"windows amd64"
65-
)
66-
for combination in "${os_arch_combinations[@]}"; do
67-
IFS=' ' read -r goos arch <<< "$combination"
68-
if [ "$goos" = "windows" ]; then
69-
output_binary="jms_inspect.exe"
70-
else
71-
output_binary="jms_inspect"
72-
fi
73-
build_and_zip "$goos" "$arch" "$output_binary" "$version"
74-
done
61+
local version=$1
62+
local os_arch_combinations=(
63+
"darwin amd64"
64+
"darwin arm64"
65+
"linux amd64"
66+
"linux arm64"
67+
"windows amd64"
68+
)
69+
for combination in "${os_arch_combinations[@]}"; do
70+
IFS=' ' read -r goos arch <<< "$combination"
71+
output_binary="jms_inspect${goos == "windows" ? ".exe" : ""}"
72+
build_and_zip "$goos" "$arch" "$output_binary" "$version"
73+
done
7574
}
7675

7776
download_dependencies() {
78-
local echarts_url="https://cdn.staticfile.net/echarts/5.4.1/echarts.min.js"
79-
local target_dir="${base_dir}/pkg/report/templates"
80-
if ! ls "$target_dir" | grep -qi "echarts"; then
81-
echo "未找到文件名包含 echarts 的文件,开始下载..."
82-
wget -P "$target_dir" "$echarts_url"
83-
if [ $? -eq 0 ]; then
84-
echo "下载成功,文件已保存到 $target_dir"
85-
else
86-
echo "下载失败,请检查网络或 URL 是否正确。"
87-
fi
88-
else
89-
echo "目录下已存在文件名包含 echarts 的文件,无需下载。"
90-
fi
77+
local echarts_url="https://cdn.staticfile.net/echarts/5.4.1/echarts.min.js"
78+
local target_dir="${base_dir}/pkg/report/templates"
79+
mkdir -p "$target_dir"
80+
if ! ls "$target_dir" | grep -qi "echarts"; then
81+
echo "未找到echarts文件,开始下载..."
82+
wget -P "$target_dir" "$echarts_url"
83+
if [ $? -eq 0 ]; then
84+
echo "下载成功,文件已保存到 $target_dir"
85+
else
86+
echo "下载失败,请检查网络或URL"
87+
fi
88+
else
89+
echo "echarts文件已存在,跳过下载"
90+
fi
9191
}
9292

9393
build() {
94-
base_dir=$(dirname "$(realpath "$0")")
95-
create_dirs
96-
download_dependencies
97-
change_version "$VERSION"
98-
compile "$VERSION"
94+
base_dir=$(dirname "$(realpath "$0")")
95+
create_dirs
96+
download_dependencies
97+
change_version "$VERSION"
98+
compile "$VERSION"
9999
}
100100

101-
build "$@"
101+
build "$@"

0 commit comments

Comments
 (0)