Skip to content

Commit a1008c1

Browse files
committed
chore: version number script
1 parent 3ac8e90 commit a1008c1

File tree

1 file changed

+234
-0
lines changed

1 file changed

+234
-0
lines changed

scripts/update-readme-version.sh

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#!/bin/bash
2+
3+
# ==============================================================================
4+
# README Version Updater for CC-Switch CLI
5+
# ==============================================================================
6+
# This script automatically updates version numbers in README.md and README_ZH.md
7+
#
8+
# Usage:
9+
# ./scripts/update-readme-version.sh # Auto-detect from Cargo.toml
10+
# ./scripts/update-readme-version.sh 4.2.0 # Specify version manually
11+
#
12+
# What it updates:
13+
# - Badge version in both READMEs
14+
# - All download links (macOS, Linux x64/ARM64, Windows)
15+
# - All extraction commands
16+
# ==============================================================================
17+
18+
set -e # Exit on error
19+
20+
# Colors for output
21+
RED='\033[0;31m'
22+
GREEN='\033[0;32m'
23+
YELLOW='\033[1;33m'
24+
BLUE='\033[0;34m'
25+
NC='\033[0m' # No Color
26+
27+
# ==============================================================================
28+
# Helper Functions
29+
# ==============================================================================
30+
31+
log_info() {
32+
echo -e "${BLUE}${NC} $1"
33+
}
34+
35+
log_success() {
36+
echo -e "${GREEN}${NC} $1"
37+
}
38+
39+
log_warning() {
40+
echo -e "${YELLOW}${NC} $1"
41+
}
42+
43+
log_error() {
44+
echo -e "${RED}${NC} $1"
45+
}
46+
47+
# ==============================================================================
48+
# Get Current Version from Cargo.toml
49+
# ==============================================================================
50+
51+
get_cargo_version() {
52+
local cargo_toml="src-tauri/Cargo.toml"
53+
54+
if [[ ! -f "$cargo_toml" ]]; then
55+
log_error "Cargo.toml not found at: $cargo_toml"
56+
exit 1
57+
fi
58+
59+
# Extract version from Cargo.toml (format: version = "X.X.X")
60+
local version=$(grep '^version = ' "$cargo_toml" | head -1 | sed 's/.*"\(.*\)".*/\1/')
61+
62+
if [[ -z "$version" ]]; then
63+
log_error "Failed to extract version from $cargo_toml"
64+
exit 1
65+
fi
66+
67+
echo "$version"
68+
}
69+
70+
# ==============================================================================
71+
# Validate Version Format
72+
# ==============================================================================
73+
74+
validate_version() {
75+
local version=$1
76+
77+
# Check if version matches X.X.X format (semantic versioning)
78+
if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
79+
log_error "Invalid version format: $version"
80+
log_error "Expected format: X.X.X (e.g., 4.1.0)"
81+
exit 1
82+
fi
83+
}
84+
85+
# ==============================================================================
86+
# Extract Current Version from README
87+
# ==============================================================================
88+
89+
get_readme_version() {
90+
local readme_file=$1
91+
92+
if [[ ! -f "$readme_file" ]]; then
93+
log_error "README not found: $readme_file"
94+
exit 1
95+
fi
96+
97+
# Extract version from badge line
98+
local version=$(grep -m 1 'img.shields.io/badge/version-' "$readme_file" | sed 's/.*version-\([0-9.]*\)-.*/\1/')
99+
100+
if [[ -z "$version" ]]; then
101+
log_warning "Could not detect current version in $readme_file"
102+
echo "unknown"
103+
else
104+
echo "$version"
105+
fi
106+
}
107+
108+
# ==============================================================================
109+
# Update README File
110+
# ==============================================================================
111+
112+
update_readme() {
113+
local readme_file=$1
114+
local old_version=$2
115+
local new_version=$3
116+
117+
if [[ ! -f "$readme_file" ]]; then
118+
log_error "File not found: $readme_file"
119+
return 1
120+
fi
121+
122+
# Create backup
123+
cp "$readme_file" "${readme_file}.bak"
124+
log_info "Created backup: ${readme_file}.bak"
125+
126+
# Use sed to replace all version occurrences
127+
# Note: Using | as delimiter to avoid conflicts with / in URLs
128+
129+
# 1. Update badge version (version-X.X.X-blue.svg)
130+
sed -i '' "s|version-[0-9.]*-blue\.svg|version-${new_version}-blue.svg|g" "$readme_file"
131+
132+
# 2. Update macOS download link and tar command
133+
sed -i '' "s|cc-switch-cli-v[0-9.]*-darwin-universal\.tar\.gz|cc-switch-cli-v${new_version}-darwin-universal.tar.gz|g" "$readme_file"
134+
135+
# 3. Update Linux x64 download link and tar command
136+
sed -i '' "s|cc-switch-cli-v[0-9.]*-linux-x64-musl\.tar\.gz|cc-switch-cli-v${new_version}-linux-x64-musl.tar.gz|g" "$readme_file"
137+
138+
# 4. Update Linux ARM64 download link and tar command
139+
sed -i '' "s|cc-switch-cli-v[0-9.]*-linux-arm64-musl\.tar\.gz|cc-switch-cli-v${new_version}-linux-arm64-musl.tar.gz|g" "$readme_file"
140+
141+
# 5. Update Windows download link
142+
sed -i '' "s|cc-switch-cli-v[0-9.]*-windows-x64\.zip|cc-switch-cli-v${new_version}-windows-x64.zip|g" "$readme_file"
143+
144+
# Verify that changes were made
145+
if diff -q "$readme_file" "${readme_file}.bak" > /dev/null 2>&1; then
146+
log_warning "No changes made to $readme_file (version might already be up-to-date)"
147+
rm "${readme_file}.bak"
148+
return 0
149+
fi
150+
151+
log_success "Updated: $readme_file"
152+
153+
# Show diff summary
154+
local changes=$(diff -u "${readme_file}.bak" "$readme_file" | grep -c '^[-+]' || echo "0")
155+
log_info " Changed lines: $((changes / 2))"
156+
}
157+
158+
# ==============================================================================
159+
# Main Script
160+
# ==============================================================================
161+
162+
main() {
163+
echo ""
164+
log_info "CC-Switch README Version Updater"
165+
echo "========================================"
166+
echo ""
167+
168+
# Determine target version
169+
local new_version=""
170+
171+
if [[ $# -eq 0 ]]; then
172+
# No argument provided, read from Cargo.toml
173+
log_info "Reading version from src-tauri/Cargo.toml..."
174+
new_version=$(get_cargo_version)
175+
log_success "Detected version: $new_version"
176+
elif [[ $# -eq 1 ]]; then
177+
# Version provided as argument
178+
new_version=$1
179+
log_info "Using provided version: $new_version"
180+
else
181+
log_error "Usage: $0 [version]"
182+
log_error "Example: $0 4.2.0"
183+
exit 1
184+
fi
185+
186+
# Validate version format
187+
validate_version "$new_version"
188+
189+
echo ""
190+
log_info "Checking current README versions..."
191+
192+
# Get current versions from READMEs
193+
local readme_en="README.md"
194+
local readme_zh="README_ZH.md"
195+
196+
local old_version_en=$(get_readme_version "$readme_en")
197+
local old_version_zh=$(get_readme_version "$readme_zh")
198+
199+
echo ""
200+
log_info "Version Summary:"
201+
echo " README.md: $old_version_en$new_version"
202+
echo " README_ZH.md: $old_version_zh$new_version"
203+
204+
# Check if update is needed
205+
if [[ "$old_version_en" == "$new_version" && "$old_version_zh" == "$new_version" ]]; then
206+
echo ""
207+
log_success "All README files are already up-to-date with version $new_version"
208+
exit 0
209+
fi
210+
211+
echo ""
212+
log_info "Starting update process..."
213+
echo ""
214+
215+
# Update both README files
216+
update_readme "$readme_en" "$old_version_en" "$new_version"
217+
update_readme "$readme_zh" "$old_version_zh" "$new_version"
218+
219+
echo ""
220+
log_success "All README files updated successfully!"
221+
echo ""
222+
log_info "Updated files:"
223+
echo " - README.md"
224+
echo " - README_ZH.md"
225+
echo ""
226+
log_info "Next steps:"
227+
echo " 1. Review changes: git diff README.md README_ZH.md"
228+
echo " 2. Commit changes: git add README*.md && git commit -m 'chore: bump version to $new_version'"
229+
echo " 3. If needed, restore backups: mv README.md.bak README.md && mv README_ZH.md.bak README_ZH.md"
230+
echo ""
231+
}
232+
233+
# Run main function
234+
main "$@"

0 commit comments

Comments
 (0)