51 lines
1.5 KiB
Bash
51 lines
1.5 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
PUBSPEC="pubspec.yaml"
|
||
|
|
if [[ ! -f "$PUBSPEC" ]]; then
|
||
|
|
echo "No $PUBSPEC found in $(pwd); skipping version bump"
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Read version line
|
||
|
|
ver_line=$(grep -E '^version:\s*' "$PUBSPEC" || true)
|
||
|
|
if [[ -z "$ver_line" ]]; then
|
||
|
|
echo "No version line found in $PUBSPEC; skipping"
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Extract version string (e.g., 1.2.3 or 1.2.3+4)
|
||
|
|
if [[ "$ver_line" =~ ([0-9]+)\.([0-9]+)\.([0-9]+)(\+[0-9]+)? ]]; then
|
||
|
|
major=${BASH_REMATCH[1]}
|
||
|
|
minor=${BASH_REMATCH[2]}
|
||
|
|
patch=${BASH_REMATCH[3]}
|
||
|
|
build=${BASH_REMATCH[4]:-}
|
||
|
|
else
|
||
|
|
echo "Failed to parse version from: $ver_line"
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
newpatch=$((patch + 1))
|
||
|
|
if [[ -n "$build" ]]; then
|
||
|
|
newver="${major}.${minor}.${newpatch}${build}"
|
||
|
|
else
|
||
|
|
newver="${major}.${minor}.${newpatch}"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Replace the first 'version:' line with the new version preserving leading whitespace
|
||
|
|
tmpfile=$(mktemp)
|
||
|
|
awk -v newver="$newver" 'BEGIN{re="^[[:space:]]*version:[[:space:]]*"} { if($0 ~ re && !done){ sub(re, substr($0,1,index($0,":"))+"version: " newver); done=1 } print }' "$PUBSPEC" > "$tmpfile"
|
||
|
|
# Fallback: simpler replace if awk didn't work
|
||
|
|
if ! grep -q "version: $newver" "$tmpfile"; then
|
||
|
|
sed -E "0,/^([[:space:]]*)version:/s//\1version: $newver/" "$PUBSPEC" > "$tmpfile"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Move replacement into place
|
||
|
|
mv "$tmpfile" "$PUBSPEC"
|
||
|
|
|
||
|
|
# Stage the change so it is included in the commit
|
||
|
|
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||
|
|
git add "$PUBSPEC" || true
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "pubspec version bumped to $newver"
|