diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-12 10:03:45 -0400 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-12 10:16:43 -0400 |
| commit | 01ef625e60e48193cf8d02b9426a585778a4a2ca (patch) | |
| tree | f886d7911136c4e0ca4fc6cadb829c43ef028a54 | |
| parent | Add builds badge to readme (diff) | |
| download | cli-01ef625e60e48193cf8d02b9426a585778a4a2ca.zip | |
Add release script
| -rwxr-xr-x | scripts/release.sh | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/scripts/release.sh b/scripts/release.sh new file mode 100755 index 0000000..2c45e5c --- /dev/null +++ b/scripts/release.sh @@ -0,0 +1,260 @@ +#!/usr/bin/env bash +set -euo pipefail + +main() { + if [ $# -ne 1 ]; then + usage + fi + + local version="$1" + + if ! git rev-parse --git-dir >/dev/null 2>&1; then + echo "Error: Not in a git repository" >&2 + exit 1 + fi + + if ! git diff-index --quiet HEAD --; then + echo "Error: You have uncommitted changes. Please commit or stash them first." >&2 + exit 1 + fi + + validate_version "$version" + + echo "Creating release $version..." + update_cmake_version "$version" + update_man_version "$version" + update_changelog "$version" + create_commit "$version" + create_tag "$version" + push_changes "$version" + + echo "" + echo "Release $version created successfully!" +} + +usage() { + cat <<EOF +Usage: $0 <version> + +Create a new release by: + - Bumping version in CMakeLists.txt + - Bumping version in doc/justify.1 man page + - Updating CHANGELOG.md with version and date + - Creating a git commit + - Creating a git tag + - Pushing commit and tag to remote + +Arguments: + version Semantic version number (e.g., 0.2.0, 1.0.0) + +Example: + $0 0.2.0 +EOF + exit 1 +} + +validate_version() { + local version="$1" + if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "Error: Invalid version format. Expected semver (e.g., 0.2.0)" >&2 + exit 1 + fi +} + +update_cmake_version() { + local version="$1" + local file="CMakeLists.txt" + local cmake_version_regex="^project(justify VERSION [0-9.]*)" + local cmake_new_version_regex="project(justify VERSION $version)" + + if ! [ -f "$file" ]; then + echo "Error: $file not found" >&2 + exit 1 + fi + + if ! grep -q "$cmake_version_regex" "$file"; then + echo "Error: Could not find version pattern in $file" >&2 + exit 1 + fi + + if ! sed "s/$cmake_version_regex/$cmake_new_version_regex/" "$file" >"$file.tmp"; then + echo "Error: Failed to update version in $file" >&2 + exit 1 + fi + mv "$file.tmp" "$file" + + if ! grep -q "^$cmake_new_version_regex" "$file"; then + echo "Error: Version update in $file failed verification" >&2 + exit 1 + fi + + echo "Updated version in $file to $version" +} + +update_man_version() { + local version="$1" + local date_str + date_str=$(date +"%B %Y") + local file="doc/justify.1" + local man_page_version_regex="^\.TH JUSTIFY 1 \"[^\"]*\" \"justify [0-9.]*\"" + local man_page_new_version_regex="\.TH JUSTIFY 1 \"$date_str\" \"justify $version\"" + + if ! [ -f "$file" ]; then + echo "Error: $file not found" >&2 + exit 1 + fi + + if ! grep -q "$man_page_version_regex" "$file"; then + echo "Error: Could not find version pattern in $file" >&2 + exit 1 + fi + + if ! sed "s/$man_page_version_regex/$man_page_new_version_regex/" "$file" >"$file.tmp"; then + echo "Error: Failed to update version in $file" >&2 + exit 1 + fi + mv "$file.tmp" "$file" + + if ! grep -q "$man_page_new_version_regex" "$file"; then + echo "Error: Version update in $file failed verification" >&2 + exit 1 + fi + + echo "Updated version in $file to $version" +} + +update_changelog() { + local version="$1" + local date + date=$(date +%Y-%m-%d) + local file="CHANGELOG.md" + + if ! [ -f "$file" ]; then + echo "Error: $file not found" >&2 + exit 1 + fi + + if ! grep -q "## \[Unreleased\]" "$file"; then + echo "Error: No [Unreleased] section found in $file" >&2 + exit 1 + fi + + if grep -q "^## $version - " "$file"; then + echo "Error: Version $version already exists in $file" >&2 + exit 1 + fi + + local temp_file + temp_file=$(mktemp) + + if ! awk -v version="$version" -v date="$date" ' + /## \[Unreleased\]/ { + print "## [Unreleased]\n" + print "## " version " - " date + found = 1 + next + } + !found || !/## \[Unreleased\]/ { + print + } + ' "$file" >"$temp_file"; then + echo "Error: Failed to process $file" >&2 + rm -f "$temp_file" + exit 1 + fi + + if ! mv "$temp_file" "$file"; then + echo "Error: Failed to write updated $file" >&2 + rm -f "$temp_file" + exit 1 + fi + + if ! grep -q "^## $version - $date" "$file"; then + echo "Error: Changelog update failed verification" >&2 + exit 1 + fi + + echo "Updated $file with version $version and date $date" +} + +create_commit() { + local version="$1" + + if ! git add .; then + echo "Error: Failed to stage files" >&2 + exit 1 + fi + + if ! git diff --cached --quiet; then + echo "" + echo "About to create commit with the following changes:" + git diff --cached --stat + echo "" + read -p "Create commit for version $version? (y/N): " -n 1 -r + echo "" + + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo "Commit cancelled. Restoring all changes..." + git reset --hard HEAD + exit 1 + fi + + if ! git commit -m "Release version $version"; then + echo "Error: Failed to create commit" >&2 + git reset --hard HEAD + exit 1 + fi + + echo "Created commit for version $version" + else + echo "Error: No changes to commit" >&2 + exit 1 + fi +} + +create_tag() { + 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 + + if ! git tag -a "v$version" -m "Release version $version"; then + echo "Error: Failed to create tag v$version" >&2 + exit 1 + fi + + echo "Created tag v$version" +} + +push_changes() { + local version="$1" + + echo "" + read -p "Push commit and tag v$version to remote? (y/N): " -n 1 -r + echo "" + + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo "Push cancelled. Commit and tag created locally but not pushed." + echo "To push later, run:" + echo " git push && git push origin v$version" + exit 0 + fi + + echo "Pushing commit and tag to remote..." + if ! git push; then + echo "Error: Failed to push commit" >&2 + exit 1 + fi + + if ! git push origin "v$version"; then + echo "Error: Failed to push tag v$version" >&2 + echo "Note: Commit was pushed successfully, but tag push failed." >&2 + exit 1 + fi + + echo "Successfully pushed version $version" +} + +main "$@" |