-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrelease.sh
executable file
·196 lines (166 loc) · 4.44 KB
/
release.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
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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<EOF
Usage: $0 [OPTIONS] [<MODULE> <VERSION>]
Create annotated git tags for module releases.
This script is used by maintainers to create annotated tags for module
releases. When a tag is pushed, it triggers a GitHub workflow that
updates README versions.
Options:
-l, --list List all modules with their versions
-n, --dry-run Show what would be done without making changes
-p, --push Push the created tag to the remote repository
-h, --help Show this help message
Examples:
$0 --list
$0 nodejs 1.2.3
$0 nodejs 1.2.3 --push
$0 --dry-run nodejs 1.2.3
EOF
exit "${1:-0}"
}
check_getopt() {
# Check if we have GNU or BSD getopt.
if getopt --test >/dev/null 2>&1; then
# Exit status 4 means GNU getopt is available.
if [[ $? -ne 4 ]]; then
echo "Error: GNU getopt is not available." >&2
echo "On macOS, you can install GNU getopt and add it to your PATH:" >&2
echo
echo $'\tbrew install gnu-getopt' >&2
echo $'\texport PATH="$(brew --prefix gnu-getopt)/bin:$PATH"' >&2
exit 1
fi
fi
}
maybe_dry_run() {
if [[ $dry_run == true ]]; then
echo "[DRY RUN] $*"
return 0
fi
"$@"
}
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"
}
list_modules() {
printf "\nListing all modules and their latest versions:\n"
printf "%s\n" "--------------------------------------------------------------"
printf "%-30s %-15s %-15s\n" "MODULE" "README VERSION" "LATEST TAG"
printf "%s\n" "--------------------------------------------------------------"
# Process each module directory.
for dir in */; do
# Skip non-module directories.
[[ ! -d $dir || ! -f ${dir}README.md || $dir == ".git/" ]] && continue
module="${dir%/}"
readme_version=$(get_readme_version "${dir}README.md")
latest_tag=$(git tag -l "release/${module}/v*" | sort -V | tail -n 1)
tag_version="none"
if [[ -n $latest_tag ]]; then
tag_version="${latest_tag#"release/${module}/v"}"
fi
printf "%-30s %-15s %-15s\n" "$module" "$readme_version" "$tag_version"
done
printf "%s\n" "--------------------------------------------------------------"
}
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
}
get_tag_name() {
local module="$1"
local version="$2"
local tag_name="release/$module/v$version"
local readme_path="$module/README.md"
if [[ ! -d $module || ! -f $readme_path ]]; then
echo "Error: Module '$module' not found or missing README.md" >&2
return 1
fi
local readme_version
readme_version=$(get_readme_version "$readme_path")
{
echo "Module: $module"
echo "Current README version: $readme_version"
echo "New tag version: $version"
echo "Tag name: $tag_name"
} >&2
echo "$tag_name"
}
# Ensure getopt is available.
check_getopt
# Set defaults.
list=false
dry_run=false
push=false
module=
version=
# Parse command-line options.
if ! temp=$(getopt -o ldph --long list,dry-run,push,help -n "$0" -- "$@"); then
echo "Error: Failed to parse arguments" >&2
usage 1
fi
eval set -- "$temp"
while true; do
case "$1" in
-l | --list)
list=true
shift
;;
-d | --dry-run)
dry_run=true
shift
;;
-p | --push)
push=true
shift
;;
-h | --help)
usage
;;
--)
shift
break
;;
*)
echo "Error: Internal error!" >&2
exit 1
;;
esac
done
if [[ $list == true ]]; then
list_modules
exit 0
fi
if [[ $# -ne 2 ]]; then
echo "Error: MODULE and VERSION are required when not using --list" >&2
usage 1
fi
module="$1"
version="$2"
if ! is_valid_version "$version"; then
exit 1
fi
if ! tag_name=$(get_tag_name "$module" "$version"); then
exit 1
fi
if git rev-parse -q --verify "refs/tags/$tag_name" >/dev/null 2>&1; then
echo "Notice: Tag '$tag_name' already exists" >&2
else
maybe_dry_run git tag -a "$tag_name" -m "Release $module v$version"
if [[ $push == true ]]; then
maybe_dry_run echo "Tag '$tag_name' created."
else
maybe_dry_run echo "Tag '$tag_name' created locally. Use --push to push it to remote."
maybe_dry_run "ℹ️ Note: Remember to push the tag when ready."
fi
fi
if [[ $push == true ]]; then
maybe_dry_run git push origin "$tag_name"
maybe_dry_run echo "Success! Tag '$tag_name' pushed to remote."
fi