aboutsummaryrefslogtreecommitdiffstats
path: root/release.sh
blob: e19c244a7e1b33a9831e90fdaef61e2cc4322307 (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
#!/usr/bin/env bash
set -eou pipefail

main() {
  if [ $# -eq 0 ]; then
    echo "Error: No version specified" >&2
    echo "Usage: $0 <version>" >&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 "$@"