-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathupdate_version.sh
executable file
·146 lines (123 loc) · 3.45 KB
/
update_version.sh
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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat << EOF
Usage: $0 [OPTIONS] <MODULE> <VERSION>
Update or check the version in a module's README.md file.
Options:
-c, --check Check if README.md version matches VERSION without updating
-h, --help Display this help message and exit
Examples:
$0 code-server 1.2.3 # Update version in code-server/README.md
$0 --check code-server 1.2.3 # Check if version matches 1.2.3
EOF
exit "${1:-0}"
}
is_valid_version() {
if ! [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Error: Version must be in format X.Y.Z (e.g., 1.2.3)" >&2
return 1
fi
}
update_version() {
local file="$1" current_tag="$2" latest_tag="$3" tmpfile
tmpfile=$(mktemp)
echo "Updating version in $file from $current_tag to $latest_tag..."
awk -v tag="$latest_tag" '
BEGIN { in_code_block = 0; in_nested_block = 0 }
{
# Detect the start and end of Markdown code blocks.
if ($0 ~ /^```/) {
in_code_block = !in_code_block
# Reset nested block tracking when exiting a code block.
if (!in_code_block) {
in_nested_block = 0
}
}
# Handle nested blocks within a code block.
if (in_code_block) {
# Detect the start of a nested block (skipping "module" blocks).
if ($0 ~ /{/ && !($1 == "module" || $1 ~ /^[a-zA-Z0-9_]+$/)) {
in_nested_block++
}
# Detect the end of a nested block.
if ($0 ~ /}/ && in_nested_block > 0) {
in_nested_block--
}
# Update "version" only if not in a nested block.
if (!in_nested_block && $1 == "version" && $2 == "=") {
sub(/"[^"]*"/, "\"" tag "\"")
}
}
print
}
' "$file" > "$tmpfile" && mv "$tmpfile" "$file"
}
get_readme_version() {
grep -o 'version *= *"[0-9]\+\.[0-9]\+\.[0-9]\+"' "$1" \
| head -1 \
| grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+' \
|| echo "0.0.0"
}
# Set defaults.
check_only=false
# Parse command-line options.
while [[ $# -gt 0 ]]; do
case "$1" in
-c | --check)
check_only=true
shift
;;
-h | --help)
usage 0
;;
-*)
echo "Error: Unknown option: $1" >&2
usage 1
;;
*)
break
;;
esac
done
if [[ $# -ne 2 ]]; then
echo "Error: MODULE and VERSION are required" >&2
usage 1
fi
module_name="$1"
version="$2"
if [[ ! -d $module_name ]]; then
echo "Error: Module directory '$module_name' not found" >&2
echo >&2
echo "Available modules:" >&2
echo >&2
find . -type d -mindepth 1 -maxdepth 1 -not -path "*/\.*" | sed 's|^./|\t|' | sort >&2
exit 1
fi
if ! is_valid_version "$version"; then
exit 1
fi
readme_path="$module_name/README.md"
if [[ ! -f $readme_path ]]; then
echo "Error: README.md not found in '$module_name' directory" >&2
exit 1
fi
readme_version=$(get_readme_version "$readme_path")
# In check mode, just return success/failure based on version match.
if [[ $check_only == true ]]; then
if [[ $readme_version == "$version" ]]; then
echo "✅ Success: Version in $readme_path matches $version"
exit 0
else
echo "❌ Error: Version mismatch in $readme_path"
echo "Expected: $version"
echo "Found: $readme_version"
exit 1
fi
fi
if [[ $readme_version != "$version" ]]; then
update_version "$readme_path" "$readme_version" "$version"
echo "✅ Version updated successfully to $version"
else
echo "ℹ️ Version in $readme_path already set to $version, no update needed"
fi