aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/release.sh
blob: 2d7ef1a2de9fd638d3b442a7937fc5c0d41980ee (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env bash
set -euo pipefail

readonly HOMEBREW_REPO_URL="git@git.sr.ht:~eugenetriguba/homebrew-justify"

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!"
  echo ""

  bump_homebrew_formula "$version"
}

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"
}

bump_homebrew_formula() {
  local version="$1"

  echo "Updating Homebrew formula..."

  local temp_dir
  temp_dir=$(mktemp -d)

  if ! git clone "$HOMEBREW_REPO_URL" "$temp_dir" 2>/dev/null; then
    echo "Error: Failed to clone Homebrew repository" >&2
    exit 1
  fi

  local formula_file="$temp_dir/Formula/justify.rb"
  if [ ! -f "$formula_file" ]; then
    echo "Error: Formula file not found at $formula_file" >&2
    exit 1
  fi

  local tarball_url="https://git.sr.ht/~eugenetriguba/justify/archive/v${version}.tar.gz"
  local sha256
  sha256=$(calculate_tarball_sha256 "$tarball_url")

  update_homebrew_formula "$formula_file" "$version" "$tarball_url" "$sha256"

  echo "  Version: $version"
  echo "  URL: $tarball_url"
  echo "  SHA256: $sha256"
  echo ""

  commit_and_push_formula "$temp_dir" "$version"
}

calculate_tarball_sha256() {
  local url="$1"
  local temp_file
  temp_file=$(mktemp)

  if ! curl -sSL "$url" -o "$temp_file" 2>/dev/null; then
    echo "Error: Failed to download tarball from $url" >&2
    echo "Make sure the tag exists in the repository." >&2
    rm -f "$temp_file"
    exit 1
  fi

  local sha256
  sha256=$(sha256sum "$temp_file" | awk '{print $1}')

  rm -f "$temp_file"
  echo "$sha256"
}

update_homebrew_formula() {
  local formula_file="$1"
  local version="$2"
  local url="$3"
  local sha256="$4"

  local temp_file
  temp_file=$(mktemp)

  sed \
    -e "s|url \".*\"|url \"$url\"|" \
    -e "s|sha256 \".*\"|sha256 \"$sha256\"|" \
    -e "s|version \".*\"|version \"$version\"|" \
    "$formula_file" >"$temp_file"

  mv "$temp_file" "$formula_file"
}

commit_and_push_formula() {
  local repo_path="$1"
  local version="$2"

  cd "$repo_path"

  read -p "Push Homebrew formula update? (y/N): " -n 1 -r
  echo ""

  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Homebrew formula updated locally but not pushed."
    echo "Repository location: $repo_path"
    return 0
  fi

  if ! git add .; then
    echo "Error: Failed to stage files" >&2
    exit 1
  fi

  if ! git commit -m "Bump justify to $version"; then
    echo "Error: Failed to create commit" >&2
    exit 1
  fi

  if ! git push; then
    echo "Error: Failed to push changes" >&2
    exit 1
  fi

  echo "Successfully updated Homebrew formula to version $version"
}

main "$@"