-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-release.sh
More file actions
executable file
·484 lines (435 loc) · 14.4 KB
/
test-release.sh
File metadata and controls
executable file
·484 lines (435 loc) · 14.4 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/bin/sh
# Smart shell detection: try zsh first, then bash
if [ -z "${BASH_VERSION}" -a -z "${ZSH_VERSION}" ]; then
# We're in a basic shell, try to find zsh or bash
if command -v zsh >/dev/null 2>&1; then
exec zsh "$0" "$@"
elif command -v bash >/dev/null 2>&1; then
exec bash "$0" "$@"
else
echo "Error: Neither zsh nor bash found. Please install one of them."
exit 1
fi
fi
# Now we're guaranteed to be in either bash or zsh
set -euo pipefail
# Global test status variables
VERIFY_NODE_STATUS="unknown"
VERIFY_GO_STATUS="unknown"
BUILD_STATUS="unknown"
FINAL_JOB_STATUS="unknown"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
LIGHT_BLUE='\033[0;36m'
PURPLE='\033[0;35m'
GREY='\033[0;90m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
NC='\033[0m' # No Color
# Detect the correct Go executable from the launching shell
GO_EXECUTABLE=""
if [ -n "${ZSH_VERSION:-}" ]; then
# We're in zsh, use zsh to find go
GO_EXECUTABLE=$(zsh -c 'command -v go')
elif [ -n "${BASH_VERSION:-}" ]; then
# We're in bash, use bash to find go
GO_EXECUTABLE=$(bash -c 'command -v go')
fi
if [ -z "$GO_EXECUTABLE" ]; then
echo -e "${RED}Error: Go executable not found in current shell environment${NC}"
exit 1
fi
echo "${BLUE}==> 1. Environment Snapshot${NC}"
echo "[${GREEN}✓${NC}] Operating System: $(uname -s) ($(uname -m))"
echo "[${GREEN}✓${NC}] Go Version: $($GO_EXECUTABLE version)"
echo "[${GREEN}✓${NC}] Node.js Version: $(node --version)"
echo "[${GREEN}✓${NC}] Project Version: $(grep '"version"' package.json | cut -d'"' -f4)"
echo "${BLUE}==> 2. Environment Checks${NC}"
# Check prerequisites
check_prerequisites() {
local issues=()
if ! command -v node >/dev/null 2>&1; then
issues+=("Node.js not found")
fi
if ! command -v npm >/dev/null 2>&1; then
issues+=("npm not found")
fi
if ! command -v git >/dev/null 2>&1; then
issues+=("git not found")
fi
if [ ${#issues[@]} -eq 0 ]; then
echo "[${GREEN}✓${NC}] Prerequisites: All required tools available"
VERIFY_NODE_STATUS="success"
return 0
else
echo "[${RED}✗${NC}] Prerequisites: ${issues[*]}"
VERIFY_NODE_STATUS="failed"
return 1
fi
}
# Check Node.js dependencies (read-only)
check_node_dependencies() {
local output
local has_warnings=false
# Check if package.json exists
if [ ! -f "package.json" ]; then
echo -e "${RED}[✗] Node.js: package.json not found${NC}"
VERIFY_NODE_STATUS="failed"
return 1
fi
# Check if version exists in package.json
local version
if ! version=$(node -p "require('./package.json').version" 2>/dev/null); then
echo -e "${RED}[✗] Node.js: Invalid package.json or version not found${NC}"
VERIFY_NODE_STATUS="failed"
return 1
fi
# Check lock file strategy
if [ -f "yarn.lock" ] && [ ! -f "package-lock.json" ]; then
if command -v yarn >/dev/null 2>&1; then
echo "[${GREEN}✓${NC}] Node.js: Using ${BOLD}yarn${NC} (yarn.lock detected, version: ${BOLD}$version${NC})"
else
echo "${YELLOW}[!] Node.js: yarn.lock detected but yarn not found, will use"
echo "${YELLOW} will use ${BOLD}npm install${NC} (version: ${BOLD}$version${NC})${NC}"
has_warnings=true
fi
elif [ -f "package-lock.json" ]; then
echo "[${GREEN}✓${NC}] Node.js: Using ${BOLD}npm ci${NC} (package-lock.json detected, version: ${BOLD}$version${NC})"
else
echo "${YELLOW}[!] Node.js: No lock file found, will use"
echo "${YELLOW} will use ${BOLD}npm install${NC} (version: ${BOLD}$version${NC})${NC}"
has_warnings=true
fi
# Test dependency installation (dry run)
if [ -f "yarn.lock" ] && command -v yarn >/dev/null 2>&1; then
if ! output=$(yarn install --dry-run 2>&1); then
echo "${RED}[✗] Node.js: Dependency check failed (yarn dry-run)${NC}"
echo "Details: $output"
VERIFY_NODE_STATUS="failed"
return 1
fi
else
if ! output=$(npm install --dry-run 2>&1); then
echo "${RED}[✗] Node.js: Dependency check failed (npm dry-run)${NC}"
echo "Details: $output"
VERIFY_NODE_STATUS="failed"
return 1
fi
fi
echo "[${GREEN}✓${NC}] Node.js: Dependencies consistent"
if [ "$has_warnings" = "true" ]; then
VERIFY_NODE_STATUS="warning"
else
VERIFY_NODE_STATUS="success"
fi
return 0
}
# Check Go modules consistency (read-only)
check_go_modules_consistency() {
local output
output=$($GO_EXECUTABLE mod tidy -diff 2>&1)
if [ -n "$output" ]; then
echo "${RED}[✗] Go Modules: Inconsistent (diff check failed)${NC}"
echo "Details: $output"
VERIFY_GO_STATUS="failed"
return 1
else
echo "[${GREEN}✓${NC}] Go Modules: Consistent"
VERIFY_GO_STATUS="success"
return 0
fi
}
# Interactive fixes
run_interactive_fixes() {
local has_issues=false
if ! check_prerequisites; then
has_issues=true
echo "${YELLOW}==> Would you like to install missing tools? (y/n)${NC}"
if prompt_user; then
echo "Please install the missing tools manually and run this script again."
exit 1
fi
fi
if ! check_node_dependencies; then
has_issues=true
echo "${YELLOW}==> Would you like to fix Node.js dependency issues? (y/n)${NC}"
if prompt_user; then
if [ -f "yarn.lock" ] && command -v yarn >/dev/null 2>&1; then
echo "Running: yarn install"
yarn install
else
echo "Running: npm install"
npm install
fi
echo "${GREEN}Node.js dependencies updated.${NC}"
fi
else
# Don't override the status if it's already set
if [ -z "$VERIFY_NODE_STATUS" ]; then
VERIFY_NODE_STATUS="success"
fi
fi
if ! check_go_modules_consistency; then
has_issues=true
echo "${YELLOW}==> Would you like to fix Go module inconsistencies? (y/n)${NC}"
if prompt_user; then
echo "Running: $GO_EXECUTABLE mod tidy"
$GO_EXECUTABLE mod tidy
echo "${GREEN}Go modules updated. Please commit the changes:${NC}"
echo "git add go.mod go.sum"
echo "git commit -m 'chore: update go modules'"
fi
else
# Ensure status is set even when check passes
if [ -z "$VERIFY_GO_STATUS" ]; then
VERIFY_GO_STATUS="success"
fi
fi
if [ "$has_issues" = false ]; then
echo "[${GREEN}✓${NC}] All checks passed, no fixes needed"
fi
}
# Build phase
run_build_phase() {
echo "${BLUE}==> 3. Build Phase${NC}"
echo "${YELLOW}==> Would you like to proceed with the build test? (y/n)${NC}"
if ! prompt_user; then
echo "Build phase skipped"
return 0
fi
# Clean previous builds
rm -rf test-build
mkdir -p test-build
# Build native binary first
echo "${LIGHT_BLUE}Building native binary...${NC}"
if $GO_EXECUTABLE build -ldflags="-s -w" -o "test-build/git-status-dash-go-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m)" .; then
echo "[${GREEN}✓${NC}] Native binary built successfully"
# Create symlink for consistent testing
ln -sf "git-status-dash-go-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m)" "test-build/git-status-dash"
echo "[${GREEN}✓${NC}] Created symlink: test-build/git-status-dash"
# Test the binary
# echo "${LIGHT_BLUE}Testing binary functionality...${NC}"
# if ./test-build/git-status-dash --version >/dev/null 2>&1; then
# echo "[${GREEN}✓${NC}] Version command works"
# else
# echo "[${RED}✗${NC}] Version command failed"
# fi
if ./test-build/git-status-dash config init >/dev/null 2>&1; then
echo "[${GREEN}✓${NC}] Config init command works"
else
echo "[${RED}✗${NC}] Config init command failed"
fi
else
echo "[${RED}✗${NC}] Native binary build failed"
return 1
fi
# Cross-compilation test (matching release.yml)
echo "${LIGHT_BLUE}Testing cross-compilation...${NC}"
local platforms=(
"linux/amd64"
"linux/arm64"
"darwin/amd64"
"darwin/arm64"
"windows/amd64"
)
local success_count=0
for platform in "${platforms[@]}"; do
IFS='/' read -r os arch <<< "$platform"
local output_name="test-build/git-status-dash-go-${os}-${arch}"
if [ "$os" = "windows" ]; then
output_name="${output_name}.exe"
fi
if GOOS="$os" GOARCH="$arch" $GO_EXECUTABLE build -ldflags="-s -w" -o "$output_name" .; then
echo "[${GREEN}✓${NC}] $platform: $(stat -c%s "$output_name" 2>/dev/null || stat -f%z "$output_name") bytes"
((success_count++))
else
echo "[${RED}✗${NC}] $platform: Build failed"
fi
done
echo "[${GREEN}✓${NC}] Cross-compilation: $success_count/${#platforms[@]} platforms successful"
if [ "$success_count" -eq "${#platforms[@]}" ]; then
BUILD_STATUS="success"
elif [ "$success_count" -gt 0 ]; then
BUILD_STATUS="warning"
else
BUILD_STATUS="failed"
fi
return 0
}
# Calculate the final job status based on all stages
calculate_final_status() {
if [ "$VERIFY_NODE_STATUS" = "failed" ] || \
[ "$VERIFY_GO_STATUS" = "failed" ] || \
[ "$BUILD_STATUS" = "failed" ]; then
FINAL_JOB_STATUS="failed"
elif [ "$VERIFY_NODE_STATUS" = "warning" ] || \
[ "$VERIFY_GO_STATUS" = "warning" ] || \
[ "$BUILD_STATUS" = "warning" ]; then
FINAL_JOB_STATUS="warning"
elif [ "$VERIFY_NODE_STATUS" = "success" ] && \
[ "$VERIFY_GO_STATUS" = "success" ] && \
[ "$BUILD_STATUS" = "success" ]; then
FINAL_JOB_STATUS="success"
else
# Default to unknown if states are mixed or not set
FINAL_JOB_STATUS="unknown"
fi
}
# Generate final report
generate_final_report() {
# First, calculate the final aggregated status
calculate_final_status
echo "${BLUE}==> 4. Final Report${NC}"
echo
# Node.js verification status
case "$VERIFY_NODE_STATUS" in
"success")
echo "${GREEN}verify-node${NC}"
;;
"warning")
echo "${YELLOW}verify-node${NC}"
;;
"failed")
echo "${RED}verify-node${NC}"
;;
*)
echo "${GREY}verify-node${NC}"
;;
esac
echo " ${BOLD}↓${NC}"
case "$VERIFY_NODE_STATUS" in
"success")
echo "${GREEN}release-nodejs${NC}"
;;
"warning")
echo "${YELLOW}release-nodejs${NC}"
;;
"failed")
echo "${RED}release-nodejs${NC}"
;;
*)
echo "${GREY}release-nodejs${NC}"
;;
esac
echo " ${BOLD}↓${NC}"
# Go verification status
case "$VERIFY_GO_STATUS" in
"success")
echo "${GREEN}verify-go${NC}"
;;
"warning")
echo "${YELLOW}verify-go${NC}"
;;
"failed")
echo "${RED}verify-go${NC}"
;;
*)
echo "${GREY}verify-go${NC}"
;;
esac
echo " ${BOLD}↓${NC}"
case "$VERIFY_GO_STATUS" in
"success")
echo "${GREEN}release-go${NC}"
;;
"warning")
echo "${YELLOW}release-go${NC}"
;;
"failed")
echo "${RED}release-go${NC}"
;;
*)
echo "${GREY}release-go${NC}"
;;
esac
echo " ${BOLD}↓${NC}"
# Final release status
case "$BUILD_STATUS" in
"success")
echo "${GREEN}create-release${NC}"
;;
"warning")
echo "${YELLOW}create-release${NC}"
;;
"failed")
echo "${RED}create-release${NC}"
;;
*)
echo "${GREY}create-release${NC}"
;;
esac
echo " ${BOLD}↓${NC}"
case "$FINAL_JOB_STATUS" in
"success")
echo "${BOLD}${GREEN}Success${NC}${NC}"
;;
"warning")
echo "${BOLD}${YELLOW}Warning${NC}${NC}"
;;
"failed")
echo "${BOLD}${RED}Failed${NC}${NC}"
;;
*)
echo "${BOLD} ${PURPLE}Unknown${NC}${NC}"
;;
esac
echo
# Environment summary
echo "[${BOLD}OS${NC}]: $(uname -s) ($(uname -m))"
echo "[${BOLD}Go${NC}]: $($GO_EXECUTABLE version) at $GO_EXECUTABLE"
echo "[${BOLD}Node.js${NC}]: $(node --version)"
echo "[${BOLD}Project${NC}]: $(grep '"version"' package.json | cut -d'"' -f4)"
# Build details
if [ -d "test-build" ]; then
local file_count=$(find test-build -type f | wc -l | tr -d ' ')
case "$FINAL_JOB_STATUS" in
"success")
echo "[${BOLD}Status${NC}]: ${GREEN}Success${NC}"
;;
"warning")
echo "[${BOLD}Status${NC}]: ${YELLOW}Warning${NC}"
;;
"failed")
echo "[${BOLD}Status${NC}]: ${RED}Failed${NC}"
;;
*)
echo "[${BOLD}Status${NC}]: ${PURPLE}Unknown${NC}"
;;
esac
echo "[${BOLD}Files${NC}]: $file_count"
echo "[${BOLD}Path${NC}]: $(pwd)/test-build/"
echo "${PURPLE}Try with:${NC}${UNDERLINE}./test-build/git-status-dash .${NC}"
echo
else
echo "[${BOLD}Status${NC}]: ${RED}Not performed${NC}"
echo
fi
}
# User prompt function
prompt_user() {
while true; do
read -p " " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
else
echo "${YELLOW}Please answer y or n${NC}"
fi
done
echo
}
# Main execution
main() {
# Run interactive fixes if needed
run_interactive_fixes
# Run build phase
run_build_phase
# Generate final report
generate_final_report
}
main "$@"