-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci.sh
More file actions
executable file
·264 lines (236 loc) · 6.7 KB
/
Copy pathci.sh
File metadata and controls
executable file
·264 lines (236 loc) · 6.7 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
#!/usr/bin/env bash
#
# 本地 CI 脚本 - 模拟 GitHub Actions CI 工作流
#
# 用法:
# ./ci.sh # 运行所有检查
# ./ci.sh check # 仅运行 cargo check
# ./ci.sh fmt # 仅检查格式
# ./ci.sh clippy # 仅运行 clippy
# ./ci.sh test # 仅运行测试
# ./ci.sh build # 仅构建 release
# ./ci.sh quick # 快速检查 (fmt + clippy + check)
# ./ci.sh full # 完整检查 (所有步骤 + fulltest)
#
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# 计时
SECONDS=0
# 打印带颜色的消息
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
success() {
echo -e "${GREEN}[✓]${NC} $1"
}
error() {
echo -e "${RED}[✗]${NC} $1"
}
warn() {
echo -e "${YELLOW}[!]${NC} $1"
}
header() {
echo ""
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${CYAN} $1${NC}"
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}
# 检查依赖
check_dependencies() {
header "检查依赖"
local missing=0
if command -v cargo &> /dev/null; then
success "cargo $(cargo --version | cut -d' ' -f2)"
else
error "cargo 未安装"
missing=1
fi
if command -v rustfmt &> /dev/null; then
success "rustfmt $(rustfmt --version | cut -d' ' -f2)"
else
warn "rustfmt 未安装 (运行: rustup component add rustfmt)"
missing=1
fi
if command -v cargo-clippy &> /dev/null || cargo clippy --version &> /dev/null; then
success "clippy $(cargo clippy --version 2>/dev/null | cut -d' ' -f2 || echo 'installed')"
else
warn "clippy 未安装 (运行: rustup component add clippy)"
missing=1
fi
if command -v rg &> /dev/null; then
success "ripgrep $(rg --version | head -1 | cut -d' ' -f2)"
else
warn "ripgrep 未安装 (部分测试可能跳过)"
fi
if command -v sg &> /dev/null; then
success "ast-grep $(sg --version 2>/dev/null | head -1 || echo 'installed')"
else
warn "ast-grep 未安装 (部分测试可能跳过)"
fi
if [ $missing -eq 1 ]; then
error "缺少必要依赖,请先安装"
exit 1
fi
}
# cargo check
do_check() {
header "Cargo Check"
info "运行 cargo check --all-features..."
cargo check --all-features
success "cargo check 通过"
}
# 格式检查
do_fmt() {
header "格式检查"
info "运行 cargo fmt --all -- --check..."
if cargo fmt --all -- --check; then
success "代码格式正确"
else
error "代码格式不正确,运行 'cargo fmt' 修复"
exit 1
fi
}
# Clippy
do_clippy() {
header "Clippy 检查"
info "运行 cargo clippy --all-features -- -D warnings..."
cargo clippy --all-features -- -D warnings
success "clippy 检查通过"
}
# 测试
do_test() {
header "运行测试"
info "运行 cargo test --all-features..."
cargo test --all-features
success "所有测试通过"
}
# 构建
do_build() {
header "构建 Release"
info "运行 cargo build --release --all-features..."
cargo build --release --all-features
local binary="target/release/misec"
if [ -f "$binary" ]; then
local size=$(du -h "$binary" | cut -f1)
success "构建完成: $binary ($size)"
else
error "构建失败,找不到二进制文件"
exit 1
fi
}
# 完整测试
do_fulltest() {
header "完整测试套件"
if [ -f "./fulltest.sh" ]; then
info "运行 ./fulltest.sh..."
./fulltest.sh
success "完整测试通过"
else
warn "fulltest.sh 不存在,跳过"
fi
}
# 显示帮助
show_help() {
echo "mise 本地 CI 脚本"
echo ""
echo "用法: $0 [命令]"
echo ""
echo "命令:"
echo " (无参数) 运行标准 CI 流程 (check + fmt + clippy + test + build)"
echo " check 仅运行 cargo check"
echo " fmt 仅检查代码格式"
echo " clippy 仅运行 clippy linter"
echo " test 仅运行测试"
echo " build 仅构建 release 版本"
echo " quick 快速检查 (fmt + clippy + check)"
echo " full 完整检查 (所有步骤 + fulltest.sh)"
echo " deps 仅检查依赖"
echo " help 显示此帮助信息"
echo ""
echo "示例:"
echo " ./ci.sh # 提交前的标准检查"
echo " ./ci.sh quick # 快速检查,用于开发中"
echo " ./ci.sh full # 发布前的完整检查"
}
# 显示总结
show_summary() {
local duration=$SECONDS
echo ""
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN} ✓ CI 检查全部通过!${NC}"
echo -e "${CYAN} 耗时: ${duration}s${NC}"
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}
# 主函数
main() {
cd "$(dirname "$0")"
case "${1:-}" in
help|--help|-h)
show_help
exit 0
;;
deps)
check_dependencies
;;
check)
check_dependencies
do_check
;;
fmt)
check_dependencies
do_fmt
;;
clippy)
check_dependencies
do_clippy
;;
test)
check_dependencies
do_test
;;
build)
check_dependencies
do_build
;;
quick)
check_dependencies
do_fmt
do_clippy
do_check
show_summary
;;
full)
check_dependencies
do_fmt
do_clippy
do_check
do_test
do_build
do_fulltest
show_summary
;;
"")
# 默认:标准 CI 流程
check_dependencies
do_fmt
do_clippy
do_check
do_test
do_build
show_summary
;;
*)
error "未知命令: $1"
echo ""
show_help
exit 1
;;
esac
}
main "$@"