-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·280 lines (239 loc) · 6.65 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·280 lines (239 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
# MDCLI - Markdown CLI 工具编译脚本
# 支持 AMD64 和 ARM64 架构
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 项目信息
APP_NAME="mdcli"
VERSION="1.0"
BUILD_DIR="build"
SOURCE_FILE="."
# 获取 Git 信息
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "none")
BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
# 打印带颜色的信息
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# 显示帮助信息
show_help() {
cat << EOF
MDCLI 工具编译脚本
用法: $0 [选项]
选项:
-h, --help 显示此帮助信息
-a, --all 编译所有平台和架构
-l, --linux 编译 Linux 平台(amd64 和 arm64)
-m, --macos 编译 macOS 平台(amd64 和 arm64)
-w, --windows 编译 Windows 平台(amd64 和 arm64)
-c, --clean 清理构建目录
--linux-amd64 仅编译 Linux AMD64
--linux-arm64 仅编译 Linux ARM64
--darwin-amd64 仅编译 macOS AMD64 (Intel)
--darwin-arm64 仅编译 macOS ARM64 (Apple Silicon)
--windows-amd64 仅编译 Windows AMD64
--windows-arm64 仅编译 Windows ARM64
示例:
$0 --all # 编译所有平台
$0 --linux # 编译 Linux 平台
$0 --darwin-arm64 # 仅编译 macOS ARM64
$0 --clean # 清理构建目录
EOF
}
# 清理构建目录
clean_build() {
print_info "清理构建目录..."
if [ -d "$BUILD_DIR" ]; then
rm -rf "$BUILD_DIR"
print_success "构建目录已清理"
else
print_warning "构建目录不存在"
fi
}
# 创建构建目录
prepare_build_dir() {
if [ ! -d "$BUILD_DIR" ]; then
mkdir -p "$BUILD_DIR"
print_info "创建构建目录: $BUILD_DIR"
fi
}
# 编译函数
build_binary() {
local os=$1
local arch=$2
local output_name="${APP_NAME}"
# Windows 需要 .exe 后缀
if [ "$os" = "windows" ]; then
output_name="${output_name}.exe"
fi
local output_path="${BUILD_DIR}/${APP_NAME}-${os}-${arch}"
mkdir -p "$output_path"
local output_file="${output_path}/${output_name}"
print_info "编译 ${os}/${arch}..."
# 设置环境变量并编译
GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build \
-mod=vendor \
-ldflags="-s -w -X mdcli/pkg/app.Version=v${VERSION} -X mdcli/pkg/app.Commit=${GIT_COMMIT} -X mdcli/pkg/app.BuildDate=${BUILD_DATE}" \
-o "$output_file" \
"$SOURCE_FILE"
if [ $? -eq 0 ]; then
local size=$(du -h "$output_file" | cut -f1)
print_success "编译成功: $output_file (大小: $size)"
# 创建压缩包
cd "$BUILD_DIR"
local archive_name="${APP_NAME}-${os}-${arch}-v${VERSION}"
if [ "$os" = "windows" ]; then
zip -q -r "${archive_name}.zip" "${APP_NAME}-${os}-${arch}"
print_success "已创建压缩包: ${archive_name}.zip"
else
tar -czf "${archive_name}.tar.gz" "${APP_NAME}-${os}-${arch}"
print_success "已创建压缩包: ${archive_name}.tar.gz"
fi
cd ..
else
print_error "编译失败: ${os}/${arch}"
return 1
fi
}
# 编译所有平台
build_all() {
print_info "开始编译所有平台..."
echo ""
# Linux
build_binary "linux" "amd64"
build_binary "linux" "arm64"
# macOS
build_binary "darwin" "amd64"
build_binary "darwin" "arm64"
# Windows
build_binary "windows" "amd64"
build_binary "windows" "arm64"
echo ""
print_success "所有平台编译完成!"
show_build_summary
}
# 编译 Linux 平台
build_linux() {
print_info "编译 Linux 平台..."
build_binary "linux" "amd64"
build_binary "linux" "arm64"
show_build_summary
}
# 编译 macOS 平台
build_macos() {
print_info "编译 macOS 平台..."
build_binary "darwin" "amd64"
build_binary "darwin" "arm64"
show_build_summary
}
# 编译 Windows 平台
build_windows() {
print_info "编译 Windows 平台..."
build_binary "windows" "amd64"
build_binary "windows" "arm64"
show_build_summary
}
# 显示编译摘要
show_build_summary() {
echo ""
print_info "编译摘要:"
echo "----------------------------------------"
if [ -d "$BUILD_DIR" ]; then
ls -lh "$BUILD_DIR" | grep -E "\.(tar\.gz|zip)$" | awk '{print " " $9 " - " $5}'
fi
echo "----------------------------------------"
print_info "构建目录: $BUILD_DIR"
}
# 检查 Go 环境
check_go() {
if ! command -v go &> /dev/null; then
print_error "未找到 Go 环境,请先安装 Go"
exit 1
fi
local go_version=$(go version | awk '{print $3}')
print_info "Go 版本: $go_version"
}
# 主函数
main() {
echo ""
print_info "========================================"
print_info " MDCLI 工具编译脚本 v${VERSION}"
print_info "========================================"
echo ""
# 检查 Go 环境
check_go
# 如果没有参数,显示帮助
if [ $# -eq 0 ]; then
show_help
exit 0
fi
# 准备构建目录
prepare_build_dir
# 解析参数
case "$1" in
-h|--help)
show_help
;;
-c|--clean)
clean_build
;;
-a|--all)
build_all
;;
-l|--linux)
build_linux
;;
-m|--macos)
build_macos
;;
-w|--windows)
build_windows
;;
--linux-amd64)
build_binary "linux" "amd64"
show_build_summary
;;
--linux-arm64)
build_binary "linux" "arm64"
show_build_summary
;;
--darwin-amd64)
build_binary "darwin" "amd64"
show_build_summary
;;
--darwin-arm64)
build_binary "darwin" "arm64"
show_build_summary
;;
--windows-amd64)
build_binary "windows" "amd64"
show_build_summary
;;
--windows-arm64)
build_binary "windows" "arm64"
show_build_summary
;;
*)
print_error "未知选项: $1"
echo ""
show_help
exit 1
;;
esac
}
# 运行主函数
main "$@"