Skip to content

Commit 0e7face

Browse files
committed
Update release script to be more intuitive
1 parent 18fe810 commit 0e7face

File tree

1 file changed

+203
-15
lines changed

1 file changed

+203
-15
lines changed

scripts/release.sh

Lines changed: 203 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,166 @@ set -e
55
# This script handles only the GitHub release creation.
66
# Building and NPM publishing are handled by GitHub workflows.
77
#
8-
# Usage: ./scripts/release.sh <version> [--dry-run]
9-
VERSION=$1
8+
# Usage: ./scripts/release.sh [VERSION|BUMP_TYPE] [OPTIONS]
9+
# Run with --help for detailed usage information
10+
FIRST_ARG=$1
1011
DRY_RUN=false
12+
VERSION=""
13+
BUMP_TYPE=""
1114

12-
# Validate version format
13-
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+\.[0-9]+)?$ ]]; then
14-
echo "❌ Invalid version format: $VERSION"
15-
echo "Version must be in format: x.y.z or x.y.z-tag.n (e.g., 1.4.0 or 1.4.0-beta.3)"
16-
exit 1
17-
fi
15+
# Function to show help
16+
show_help() {
17+
cat << 'EOF'
18+
📦 GitHub Release Creator
19+
20+
Creates releases with automatic semver bumping. Only handles GitHub release
21+
creation - building and NPM publishing are handled by workflows.
22+
23+
USAGE:
24+
[VERSION|BUMP_TYPE] [OPTIONS]
25+
26+
ARGUMENTS:
27+
VERSION Explicit version (e.g., 1.5.0, 2.0.0-beta.1)
28+
BUMP_TYPE major | minor [default] | patch
29+
30+
OPTIONS:
31+
--dry-run Preview without executing
32+
-h, --help Show this help
33+
34+
EXAMPLES:
35+
(no args) Interactive minor bump
36+
major Interactive major bump
37+
1.5.0 Use specific version
38+
patch --dry-run Preview patch bump
39+
40+
EOF
41+
42+
local highest_version=$(get_highest_version)
43+
if [[ -n "$highest_version" ]]; then
44+
echo "CURRENT: $highest_version"
45+
echo "NEXT: major=$(bump_version "$highest_version" "major") | minor=$(bump_version "$highest_version" "minor") | patch=$(bump_version "$highest_version" "patch")"
46+
else
47+
echo "No existing version tags found"
48+
fi
49+
echo ""
50+
}
51+
52+
# Function to get the highest version from git tags
53+
get_highest_version() {
54+
git tag | grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+\.[0-9]+)?$' | sed 's/^v//' | sort -V | tail -1
55+
}
56+
57+
# Function to parse version components
58+
parse_version() {
59+
local version=$1
60+
echo "$version" | sed -E 's/^([0-9]+)\.([0-9]+)\.([0-9]+)(-.*)?$/\1 \2 \3 \4/'
61+
}
62+
63+
# Function to bump version based on type
64+
bump_version() {
65+
local current_version=$1
66+
local bump_type=$2
67+
68+
local parsed=($(parse_version "$current_version"))
69+
local major=${parsed[0]}
70+
local minor=${parsed[1]}
71+
local patch=${parsed[2]}
72+
local prerelease=${parsed[3]:-""}
73+
74+
# Remove prerelease for stable version bumps
75+
case $bump_type in
76+
major)
77+
echo "$((major + 1)).0.0"
78+
;;
79+
minor)
80+
echo "${major}.$((minor + 1)).0"
81+
;;
82+
patch)
83+
echo "${major}.${minor}.$((patch + 1))"
84+
;;
85+
*)
86+
echo "❌ Unknown bump type: $bump_type" >&2
87+
exit 1
88+
;;
89+
esac
90+
}
91+
92+
# Function to validate version format
93+
validate_version() {
94+
local version=$1
95+
if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+\.[0-9]+)?$ ]]; then
96+
echo "❌ Invalid version format: $version"
97+
echo "Version must be in format: x.y.z or x.y.z-tag.n (e.g., 1.4.0 or 1.4.0-beta.3)"
98+
return 1
99+
fi
100+
return 0
101+
}
102+
103+
# Function to compare versions (returns 1 if first version is greater, 0 if equal, -1 if less)
104+
compare_versions() {
105+
local version1=$1
106+
local version2=$2
107+
108+
# Remove prerelease parts for comparison
109+
local v1_stable=$(echo "$version1" | sed -E 's/(-.*)?$//')
110+
local v2_stable=$(echo "$version2" | sed -E 's/(-.*)?$//')
111+
112+
if [[ "$v1_stable" == "$v2_stable" ]]; then
113+
echo 0
114+
return
115+
fi
116+
117+
# Use sort -V to compare versions
118+
local sorted=$(printf "%s\n%s" "$v1_stable" "$v2_stable" | sort -V)
119+
if [[ "$(echo "$sorted" | head -1)" == "$v1_stable" ]]; then
120+
echo -1
121+
else
122+
echo 1
123+
fi
124+
}
125+
126+
# Function to ask for confirmation
127+
ask_confirmation() {
128+
local suggested_version=$1
129+
echo ""
130+
echo "🚀 Suggested next version: $suggested_version"
131+
read -p "Do you want to use this version? (y/N): " -n 1 -r
132+
echo
133+
if [[ $REPLY =~ ^[Yy]$ ]]; then
134+
return 0
135+
else
136+
return 1
137+
fi
138+
}
139+
140+
# Function to get version interactively
141+
get_version_interactively() {
142+
echo ""
143+
echo "Please enter the version manually:"
144+
while true; do
145+
read -p "Version: " manual_version
146+
if validate_version "$manual_version"; then
147+
local highest_version=$(get_highest_version)
148+
if [[ -n "$highest_version" ]]; then
149+
local comparison=$(compare_versions "$manual_version" "$highest_version")
150+
if [[ $comparison -le 0 ]]; then
151+
echo "❌ Version $manual_version is not newer than the highest existing version $highest_version"
152+
continue
153+
fi
154+
fi
155+
VERSION="$manual_version"
156+
break
157+
fi
158+
done
159+
}
160+
161+
# Check for help flags first
162+
for arg in "$@"; do
163+
if [[ "$arg" == "-h" ]] || [[ "$arg" == "--help" ]]; then
164+
show_help
165+
exit 0
166+
fi
167+
done
18168

19169
# Check for arguments and set flags
20170
for arg in "$@"; do
@@ -23,16 +173,54 @@ for arg in "$@"; do
23173
fi
24174
done
25175

26-
if [ -z "$VERSION" ]; then
27-
echo "Usage: $0 <version> [--dry-run]"
28-
echo ""
29-
echo "This script creates a GitHub release and tag. The GitHub workflow will handle:"
30-
echo " - Building the project"
31-
echo " - Bundling AXe artifacts"
32-
echo " - Publishing to NPM"
176+
# Determine version or bump type (ignore --dry-run flag)
177+
if [[ -z "$FIRST_ARG" ]] || [[ "$FIRST_ARG" == "--dry-run" ]]; then
178+
# No argument provided, default to minor bump
179+
BUMP_TYPE="minor"
180+
elif [[ "$FIRST_ARG" == "major" ]] || [[ "$FIRST_ARG" == "minor" ]] || [[ "$FIRST_ARG" == "patch" ]]; then
181+
# Bump type provided
182+
BUMP_TYPE="$FIRST_ARG"
183+
else
184+
# Version string provided
185+
if validate_version "$FIRST_ARG"; then
186+
VERSION="$FIRST_ARG"
187+
else
188+
exit 1
189+
fi
190+
fi
191+
192+
# If bump type is set, calculate the suggested version
193+
if [[ -n "$BUMP_TYPE" ]]; then
194+
HIGHEST_VERSION=$(get_highest_version)
195+
if [[ -z "$HIGHEST_VERSION" ]]; then
196+
echo "❌ No existing version tags found. Please provide a version manually."
197+
get_version_interactively
198+
else
199+
SUGGESTED_VERSION=$(bump_version "$HIGHEST_VERSION" "$BUMP_TYPE")
200+
201+
if ask_confirmation "$SUGGESTED_VERSION"; then
202+
VERSION="$SUGGESTED_VERSION"
203+
else
204+
get_version_interactively
205+
fi
206+
fi
207+
fi
208+
209+
# Final validation and version comparison
210+
if [[ -z "$VERSION" ]]; then
211+
echo "❌ No version determined"
33212
exit 1
34213
fi
35214

215+
HIGHEST_VERSION=$(get_highest_version)
216+
if [[ -n "$HIGHEST_VERSION" ]]; then
217+
COMPARISON=$(compare_versions "$VERSION" "$HIGHEST_VERSION")
218+
if [[ $COMPARISON -le 0 ]]; then
219+
echo "❌ Version $VERSION is not newer than the highest existing version $HIGHEST_VERSION"
220+
exit 1
221+
fi
222+
fi
223+
36224
# Detect current branch
37225
BRANCH=$(git rev-parse --abbrev-ref HEAD)
38226

0 commit comments

Comments
 (0)