aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/release.sh
blob: 9e494331845b83b31cf9e1d8259274fcab391ae8 (plain) (blame)
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
#!/usr/bin/env bash
#
# Release script for ssg
# Updates version in CHANGELOG.md, creates git tag, and pushes

set -euo pipefail

# shellcheck disable=SC2155
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC2155
readonly PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
readonly CHANGELOG="${PROJECT_ROOT}/CHANGELOG.md"

usage() {
  cat <<EOF
Usage: $(basename "$0") VERSION

Create a new release with the specified version.

Arguments:
  VERSION    Semantic version number (e.g., 1.0.0, 0.2.1)

Examples:
  $(basename "$0") 1.0.0
  $(basename "$0") 0.2.1
EOF
  exit 1
}

main() {
  if [[ $# -ne 1 ]]; then
    usage
  fi

  local version="$1"

  validate_version "$version"
  check_clean_working_tree

  echo "Release plan:"
  echo "  Version: $version"
  echo "  Update: $CHANGELOG"
  echo "  Create: git commit"
  echo "  Create: git tag v${version}"
  echo "  Push:   commit and tag to origin/main"
  echo

  if ! confirm "Proceed with release?"; then
    echo "Release cancelled."
    exit 0
  fi

  update_changelog "$version"
  create_commit "$version"
  create_tag "$version"

  echo
  if confirm "Push to remote?"; then
    push_changes "$version"
    echo
    echo "v${version} released!"
  else
    echo
    echo "Changes committed and tagged locally."
    echo "To push manually, run:"
    echo "  git push origin main"
    echo "  git push origin v${version}"
  fi
}

validate_version() {
  local ver="$1"
  if [[ ! "$ver" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    error "Invalid version format: $ver (expected: X.Y.Z)"
  fi
}

error() {
  echo "Error: $*" >&2
  exit 1
}

confirm() {
  local prompt="$1"
  local response
  read -r -p "$prompt [y/N] " response
  [[ "$response" =~ ^[Yy]$ ]]
}

check_clean_working_tree() {
  if ! git diff-index --quiet HEAD --; then
    error "Working tree has uncommitted changes. Please commit or stash them first."
  fi
}

update_changelog() {
  local ver="$1"
  local date
  date="$(date +%Y-%m-%d)"

  if ! grep -q "## \[Unreleased\]" "$CHANGELOG"; then
    error "Could not find [Unreleased] section in $CHANGELOG"
  fi

  echo "Updating $CHANGELOG..."
  sed -i.bak "s/## \[Unreleased\]/## [Unreleased]\n\n## [$ver] - $date/" "$CHANGELOG"
  rm "${CHANGELOG}.bak"
}

create_commit() {
  local ver="$1"

  echo "Creating git commit..."
  git add "$CHANGELOG"
  git commit -m "Release v${ver}"
}

create_tag() {
  local ver="$1"

  echo "Creating git tag v${ver}..."
  git tag -a "v${ver}" -m "Release v${ver}"
}

push_changes() {
  local ver="$1"
  echo "Pushing commit and tag to remote..."
  git push origin main
  git push origin "v${ver}"
}

main "$@"