-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·547 lines (468 loc) · 15.9 KB
/
Copy pathrelease.sh
File metadata and controls
executable file
·547 lines (468 loc) · 15.9 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/bin/bash
# release.sh - Automates the asteroid_app release workflow
# Usage: ./release.sh 1.1.3
# Options: ./release.sh --help
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
VERSION_FILES=(
package.json
src-tauri/Cargo.toml
src-tauri/Cargo.lock
src-tauri/tauri.conf.json
CHANGELOG.md
)
# Helper functions
info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
success() {
echo -e "${GREEN}✅ $1${NC}"
}
error() {
echo -e "${RED}❌ $1${NC}"
exit 1
}
warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
# Banner
print_banner() {
cat << "EOF"
╔═══════════════════════════════════════╗
║ asteroid_app Release Manager 🚀 ║
╚═══════════════════════════════════════╝
EOF
}
# Help
show_help() {
cat << EOF
Usage: ./release.sh [VERSION] [OPTIONS]
Examples:
./release.sh 1.1.3 # Release version 1.1.3
./release.sh --patch # Auto-increment patch (1.1.2 → 1.1.3)
./release.sh --minor # Auto-increment minor (1.1.0 → 1.2.0)
./release.sh --major # Auto-increment major (1.0.0 → 2.0.0)
./release.sh --dry-run 1.1.3 # Preview changes without committing
Options:
--help Show this help message
--dry-run Show what would be done without making changes
--no-tag Create release but don't push the tag
--push-only Only push existing tag (for recovery)
Files modified:
- package.json, src-tauri/Cargo.toml, src-tauri/tauri.conf.json (version sync)
- src-tauri/Cargo.lock (via cargo update)
- CHANGELOG.md (new section prepended): built from commits after the tag for the version in package.json prior to bump (vX.Y.Z..HEAD); feat/fix/docs map to Features/Bug Fixes/etc.; merges, chore: release*, version bumps ignored.
Requirements:
- git
- grep/sed
- node (read package.json version)
- cargo (update src-tauri/Cargo.lock)
- Current branch: main (or master)
- src-tauri/Cargo.lock tracked in git
EOF
}
# Validate semantic version format
validate_version() {
local version=$1
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
error "Invalid version format: $version (use X.Y.Z format)"
fi
}
# Read current version from package.json
get_current_version() {
node -p "require('./package.json').version"
}
# Increment version (major/minor/patch)
increment_version() {
local current=$1
local type=$2
IFS='.' read -r major minor patch <<< "$current"
case $type in
patch)
patch=$((patch + 1))
;;
minor)
minor=$((minor + 1))
patch=0
;;
major)
major=$((major + 1))
minor=0
patch=0
;;
*)
error "Unknown increment type: $type"
;;
esac
echo "$major.$minor.$patch"
}
# True when subject looks like a version bump commit
is_version_bump_commit() {
local s="$1"
[[ "$s" =~ Cargo\.toml ]] ||
[[ "$s" =~ package\.json ]] ||
[[ "$s" =~ tauri\.conf\.json ]] ||
[[ "$s" =~ version[[:space:]]*= ]]
}
# Validate repo is ready for release
validate_repo() {
local dry_run=$1
info "Validating repository state..."
# Ensure we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
error "Not a git repository"
fi
# Warn if not on default branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" != "main" ] && [ "$current_branch" != "master" ]; then
if [ "$dry_run" = "true" ]; then
warning "Current branch is '$current_branch' (expected 'main' or 'master'); continuing dry-run"
else
warning "Current branch is '$current_branch' (expected 'main' or 'master')"
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
error "Aborted by user"
fi
fi
fi
# Check working tree (skipped in dry-run so preview works with local changes)
if ! git diff-index --quiet HEAD --; then
if [ "$dry_run" = "true" ]; then
warning "Working directory has uncommitted changes; dry-run will not commit"
else
error "Working directory has uncommitted changes. Please commit first."
fi
fi
# Check remote
if ! git remote | grep -q 'origin'; then
error "No 'origin' remote configured"
fi
success "Repository validation passed"
}
# Bump version in tracked files
update_version() {
local new_version=$1
local dry_run=$2
info "Updating version to $new_version..."
if [ "$dry_run" = "true" ]; then
sed -E -e 's/"version"[[:space:]]*:[[:space:]]*"[0-9]+\.[0-9]+\.[0-9]+"/"version": "'"$new_version"'"/' package.json | head -5
sed -E -e 's/^version[[:space:]]*=.*/version = "'"$new_version"'"/' src-tauri/Cargo.toml | head -5
sed -E -e 's/"version"[[:space:]]*:[[:space:]]*"[0-9]+\.[0-9]+\.[0-9]+"/"version": "'"$new_version"'"/' src-tauri/tauri.conf.json | head -25
echo "Would run: (cd src-tauri && cargo update -p asteroid_app --precise $new_version)"
else
sed -i -E 's/"version"[[:space:]]*:[[:space:]]*"[0-9]+\.[0-9]+\.[0-9]+"/"version": "'"$new_version"'"/' package.json
sed -i -E 's/^version[[:space:]]*=.*/version = "'"$new_version"'"/' src-tauri/Cargo.toml
sed -i -E 's/"version"[[:space:]]*:[[:space:]]*"[0-9]+\.[0-9]+\.[0-9]+"/"version": "'"$new_version"'"/' src-tauri/tauri.conf.json
(cd src-tauri && cargo update -p asteroid_app --precise "$new_version")
fi
success "Version updated to $new_version"
}
# GitHub https base URL from origin (https://github.com/owner/repo), or empty
get_github_https_base() {
local remote
remote=$(git remote get-url origin 2>/dev/null || echo "")
if [[ "$remote" =~ github\.com[:/]([^/]+)/([^/.]+)(\.git)?$ ]]; then
echo "https://github.com/${BASH_REMATCH[1]}/${BASH_REMATCH[2]%.git}"
else
echo ""
fi
}
# Maps conventional commit subjects to changelog sections (added/changed/fixed/...)
classify_commit_subject() {
local s="$1"
if [[ "$s" != *:* ]]; then
local lc
lc=$(printf '%s\n' "$s" | tr '[:upper:]' '[:lower:]')
if [[ "$lc" == bump* ]] && is_version_bump_commit "$s"; then
echo "skip"
else
echo "changed"
fi
return
fi
local prefix="${s%%:*}"
local body="${s#*:}"
local body_trim="${body#"${body%%[![:space:]]*}"}"
local body_lower
body_lower=$(printf '%s\n' "$body_trim" | tr '[:upper:]' '[:lower:]')
case "$prefix" in
Merge*)
echo "skip"
return
;;
esac
local raw_type="${prefix%%(*}"
local type="$raw_type"
type="${type%\!}"
case "$type" in
feat | Feat | feature | Feature) echo added ;;
fix | Fix) echo fixed ;;
docs | Docs) echo documentation ;;
test | tests | Test | Tests) echo tests ;;
chore | Chore)
if [[ "$body_lower" == release* ]] || { [[ "$body_lower" == bump* ]] && is_version_bump_commit "$s"; }; then
echo skip
else
echo chores
fi
;;
style | Style | refactor | Refactor | perf | Perf | ci | CI | build | Build | revert | Revert) echo changed ;;
*) echo changed ;;
esac
}
# Build markdown changelog body from git (v<previous_version>..HEAD unless fallback)
build_changelog_notes() {
local previous_version=$1
local github_base=$2
local log_range=""
local tmpdir
tmpdir=$(mktemp -d)
if git rev-parse "v${previous_version}^{commit}" >/dev/null 2>&1; then
log_range="v${previous_version}..HEAD"
elif last=$(git describe --tags --abbrev=0 --match 'v*' 2>/dev/null); then
warning "Tag v${previous_version} not found; using revision range ${last}..HEAD"
log_range="${last}..HEAD"
else
warning "No v* tag found; falling back to the last 30 commits"
log_range="-n 30"
fi
: >"$tmpdir/added"
: >"$tmpdir/changed"
: >"$tmpdir/fixed"
: >"$tmpdir/documentation"
: >"$tmpdir/tests"
: >"$tmpdir/chores"
local -a log_cmd
if [[ "$log_range" == "-n 30" ]]; then
log_cmd=(git log -n 30 --no-merges --pretty=format:'%H|%s')
else
log_cmd=(git log "$log_range" --no-merges --pretty=format:'%H|%s')
fi
while IFS= read -r line; do
[[ -z "$line" ]] && continue
local hash="${line%%|*}"
local subject="${line#*|}"
local section
section=$(classify_commit_subject "$subject")
[[ "$section" == "skip" ]] && continue
local short
short=$(git rev-parse --short "$hash" 2>/dev/null || echo "${hash:0:7}")
local link_suffix=""
if [[ -n "$github_base" ]]; then
link_suffix=" ([${short}](${github_base}/commit/${hash}))"
fi
echo "- ${subject}${link_suffix}" >>"$tmpdir/$section"
done < <("${log_cmd[@]}")
append_section() {
local title=$1
local file=$2
if [[ -s "$file" ]]; then
printf '%s\n\n%s\n\n' "### $title" "$(cat "$file")" >>"$tmpdir/outbuf"
fi
}
: >"$tmpdir/outbuf"
append_section "Features" "$tmpdir/added"
append_section "Bug Fixes" "$tmpdir/fixed"
append_section "Code Refactoring" "$tmpdir/changed"
append_section "Documentation" "$tmpdir/documentation"
append_section "Tests" "$tmpdir/tests"
append_section "Chores" "$tmpdir/chores"
if [[ ! -s "$tmpdir/outbuf" ]]; then
echo "### Code Refactoring"
echo ""
echo "- No commits left after filtering (merge/release/version bump only). Check tags or edit this entry manually."
echo ""
rm -rf "$tmpdir"
else
cat "$tmpdir/outbuf"
rm -rf "$tmpdir"
fi
}
# Prepend generated release section to CHANGELOG.md
update_changelog() {
local previous_version=$1
local new_version=$2
local dry_run=$3
local date
date=$(date +%Y-%m-%d)
info "Updating CHANGELOG.md..."
local github_base
github_base=$(get_github_https_base)
local heading
if [[ -n "$github_base" ]]; then
heading="## [$new_version](${github_base}/releases/tag/v${new_version}) (${date})"
else
heading="## [$new_version] (${date})"
fi
local body
body=$(build_changelog_notes "$previous_version" "$github_base")
body="${body%"${body##*[![:space:]]}"}"$'\n'
local changelog_entry="${heading}"$'\n\n'"${body}"$'---'$'\n\n'
if [ "$dry_run" = "true" ]; then
echo "CHANGELOG.md would be updated with:"
echo "$changelog_entry"
else
temp=$(mktemp)
{
echo "$changelog_entry"
cat CHANGELOG.md
} >"$temp"
mv "$temp" CHANGELOG.md
fi
success "CHANGELOG.md updated"
}
# Stage and commit version + changelog
create_commit() {
local version=$1
local dry_run=$2
info "Creating release commit..."
if [ "$dry_run" = "true" ]; then
echo "Would run:"
echo " git add ${VERSION_FILES[*]}"
echo " git commit -m 'chore(release): $version'"
else
git add "${VERSION_FILES[@]}"
git commit -m "chore(release): $version"
success "Commit created"
fi
}
# Create annotated git tag
create_tag() {
local version=$1
local dry_run=$2
info "Creating git tag..."
if [ "$dry_run" = "true" ]; then
echo "Would run:"
echo " git tag -a v$version -m 'Release version $version'"
echo " git push origin main v$version"
else
# Annotated tag points at release commit (package.json version)
git tag -a "v$version" -m "Release version $version"
success "Tag created: v$version"
fi
}
# Push
push_changes() {
local version=$1
local dry_run=$2
local no_tag=$3
info "Pushing changes to origin..."
if [ "$dry_run" = "true" ]; then
echo "Would run:"
echo " git push origin main"
if [ "$no_tag" != "true" ]; then
echo " git push origin v$version"
fi
else
git push origin main
if [ "$no_tag" != "true" ]; then
git push origin "v$version"
fi
success "Changes pushed to origin"
fi
}
# Main
main() {
print_banner
local new_version=""
local dry_run=false
local no_tag=false
local push_only=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--help)
show_help
exit 0
;;
--dry-run)
dry_run=true
shift
;;
--no-tag)
no_tag=true
shift
;;
--push-only)
push_only=true
shift
;;
--patch|--minor|--major)
current_version=$(get_current_version)
increment_type=${1#--}
new_version=$(increment_version "$current_version" "$increment_type")
shift
;;
*)
new_version=$1
shift
;;
esac
done
# Require explicit version (--patch|--minor|--major handled above)
if [ -z "$new_version" ]; then
current=$(get_current_version)
error "No version specified. Current version: $current\nRun with --help for usage"
fi
# Semantic version sanity check
validate_version "$new_version"
# Info
current_version=$(get_current_version)
info "Current version: $current_version"
info "New version: $new_version"
if [ "$dry_run" = "true" ]; then
warning "DRY RUN MODE - no changes will be made"
if [ ! -f src-tauri/Cargo.lock ] || ! git ls-files --error-unmatch src-tauri/Cargo.lock >/dev/null 2>&1; then
warning "src-tauri/Cargo.lock is not tracked in git; commit it before a real release"
fi
fi
# Push-only: skip changelog/version steps
if [ "$push_only" = "true" ]; then
info "Push-only mode: skipping validation and updates"
push_changes "$new_version" "$dry_run" "$no_tag"
success "Tag pushed"
return 0
fi
# Prerequisites
validate_repo "$dry_run"
local previous_version
previous_version=$(get_current_version)
# Version file, changelog, commit
update_version "$new_version" "$dry_run"
update_changelog "$previous_version" "$new_version" "$dry_run"
create_commit "$new_version" "$dry_run"
# Tag must attach to the commit that touches package.json (CI expects this)
if [ "$dry_run" != "true" ]; then
if ! git show --pretty="" --name-only HEAD | grep -qx 'package.json'; then
error "Release commit missing package.json — refusing to tag (fix release.sh)"
fi
committed_ver=$(node -p "JSON.parse(require('child_process').execSync('git show HEAD:package.json', {encoding:'utf8'})).version")
if [ "$committed_ver" != "$new_version" ]; then
error "Committed package.json version ($committed_ver) != release target ($new_version)"
fi
fi
create_tag "$new_version" "$dry_run"
push_changes "$new_version" "$dry_run" "$no_tag"
# Summary
echo ""
if [ "$dry_run" = "true" ]; then
warning "DRY RUN completed. No changes were made."
info "Run again without --dry-run to make changes"
else
success "Release v$new_version created successfully!"
echo ""
echo "Next steps:"
echo " 1. Jenkins will detect the tag and run tests, Tauri build, packaging, and GitHub release upload"
echo " 2. Monitor the Jenkins pipeline until it completes successfully"
echo ""
info "Release process initiated! 🚀"
fi
}
# Entry point
main "$@"