#!/usr/bin/env bash set -eou pipefail main() { if [ $# -eq 0 ]; then echo "Error: No version specified" >&2 echo "Usage: $0 " >&2 echo "Example: $0 0.5.0" >&2 exit 1 fi local version=$1 local today today=$(date +%Y-%m-%d) validate_version_format "$version" ensure_on_main_branch ensure_no_uncommitted_changes ensure_tag_does_not_exist "$version" echo "Creating release v$version..." echo "Updating CHANGELOG.md..." update_changelog "$version" "$today" echo "Updating README.md..." update_readme_version_badge "$version" echo "Creating commit and tag..." create_release_commit_and_tag "$version" echo "Pushing to remote..." git push && git push --tags echo "Release v$version created and pushed successfully!" } validate_version_format() { local version=$1 if ! [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "Error: Invalid version format: $version" >&2 echo "Version must follow semantic versioning (e.g., 0.5.0)" >&2 exit 1 fi } ensure_on_main_branch() { local current_branch current_branch=$(git branch --show-current) if [ "$current_branch" != "main" ]; then echo "Error: Not on main branch (current: $current_branch)" >&2 exit 1 fi } ensure_no_uncommitted_changes() { if ! git diff-index --quiet HEAD --; then echo "Error: Uncommitted changes detected. Please commit or stash them first." >&2 exit 1 fi } ensure_tag_does_not_exist() { local version=$1 if git rev-parse "v$version" >/dev/null 2>&1; then echo "Error: Tag v$version already exists" >&2 exit 1 fi } update_changelog() { local version=$1 local today=$2 if ! grep -q "## \[Unreleased\]" CHANGELOG.md; then echo "Error: No [Unreleased] section found in CHANGELOG.md" >&2 exit 1 fi if [[ "$OSTYPE" == "darwin"* ]]; then sed -i '' "/## \[Unreleased\]/a\\ \\ ## [$version] - $today " CHANGELOG.md else sed -i "/## \[Unreleased\]/a\\\\n## [$version] - $today" CHANGELOG.md fi } update_readme_version_badge() { local version=$1 if ! grep -q "version-.*-blue" README.md; then echo "Error: Version badge not found in README.md" >&2 exit 1 fi if [[ "$OSTYPE" == "darwin"* ]]; then sed -i '' "s/version-[0-9]*\.[0-9]*\.[0-9]*-blue/version-$version-blue/" README.md else sed -i "s/version-[0-9]*\.[0-9]*\.[0-9]*-blue/version-$version-blue/" README.md fi } create_release_commit_and_tag() { local version=$1 git add CHANGELOG.md README.md git commit -m "Release version v$version" git tag "v$version" } main "$@"