forked from jnsahaj/lumen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·480 lines (381 loc) · 13 KB
/
release.sh
File metadata and controls
executable file
·480 lines (381 loc) · 13 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
#!/bin/bash
set -e
# Auto mode flag
AUTO_MODE=false
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Paths
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOMEBREW_REPO="../homebrew-lumen"
FORMULA_PATH="$HOMEBREW_REPO/Formula/lumen.rb"
# Helper functions
info() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
confirm() {
local prompt="$1"
local response
if [[ "$AUTO_MODE" == true ]]; then
echo -e "${YELLOW}$prompt [y/N]${NC} y (auto)"
return 0
fi
echo -en "${YELLOW}$prompt [y/N]${NC} "
read -r response
[[ "$response" =~ ^[Yy]$ ]]
}
prompt_input() {
local prompt="$1"
local var_name="$2"
local default="$3"
local response
if [[ -n "$default" ]]; then
echo -en "${BLUE}$prompt${NC} [${default}]: "
else
echo -en "${BLUE}$prompt${NC}: "
fi
read -r response
if [[ -z "$response" && -n "$default" ]]; then
eval "$var_name='$default'"
else
eval "$var_name='$response'"
fi
}
# Get current version from Cargo.toml
get_current_version() {
grep '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/'
}
# Validate semantic version
validate_version() {
local version="$1"
if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
error "Invalid version format: $version. Expected format: X.Y.Z"
fi
}
# Check prerequisites
check_prerequisites() {
info "Checking prerequisites..."
# Check if we're in a git repo
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
error "Not in a git repository"
fi
# Check for uncommitted changes (excluding Cargo.toml and Cargo.lock)
if [[ -n $(git status --porcelain | grep -v 'Cargo.toml' | grep -v 'Cargo.lock') ]]; then
warn "You have uncommitted changes (other than Cargo.toml/Cargo.lock):"
git status --short | grep -v 'Cargo.toml' | grep -v 'Cargo.lock'
if ! confirm "Continue anyway?"; then
exit 1
fi
fi
# Check if cargo is installed
if ! command -v cargo &>/dev/null; then
error "cargo is not installed"
fi
# Check if gh CLI is installed
if ! command -v gh &>/dev/null; then
error "GitHub CLI (gh) is not installed. Install with: brew install gh"
fi
# Check if gh is authenticated
if ! gh auth status &>/dev/null; then
error "GitHub CLI is not authenticated. Run: gh auth login"
fi
# Check if homebrew repo exists
if [[ ! -d "$HOMEBREW_REPO" ]]; then
error "Homebrew repo not found at $HOMEBREW_REPO"
fi
# Check if Formula file exists
if [[ ! -f "$FORMULA_PATH" ]]; then
error "Formula file not found at $FORMULA_PATH"
fi
success "All prerequisites met"
}
# Step 1: Update version in Cargo.toml and Cargo.lock
update_cargo_version() {
local new_version="$1"
info "Updating Cargo.toml version to $new_version..."
sed -i '' "s/^version = \".*\"/version = \"$new_version\"/" Cargo.toml
# Verify the change
local updated_version
updated_version=$(get_current_version)
if [[ "$updated_version" != "$new_version" ]]; then
error "Failed to update Cargo.toml version"
fi
# Update Cargo.lock - need to update the package version directly
info "Updating Cargo.lock..."
# Find and replace the version for the lumen package in Cargo.lock
# The format is: name = "lumen" followed by version = "X.Y.Z"
sed -i '' '/^name = "lumen"$/{n;s/^version = ".*"/version = "'"$new_version"'"/;}' Cargo.lock
# Run cargo check to ensure Cargo.lock is valid and update any dependency changes
cargo check --quiet 2>/dev/null || cargo check
success "Cargo.toml and Cargo.lock updated"
}
# Step 2: Commit version changes
commit_version_changes() {
local version="$1"
info "Committing version changes..."
git add Cargo.toml Cargo.lock
git commit -m "chore: bump version to $version"
success "Version changes committed"
}
# Step 3: Publish to crates.io
publish_to_crates() {
info "Publishing to crates.io..."
if [[ "$AUTO_MODE" != true ]]; then
if confirm "Run 'cargo publish --dry-run' first?"; then
cargo publish --dry-run
if ! confirm "Dry run successful. Proceed with actual publish?"; then
error "Aborted by user"
fi
fi
fi
cargo publish
success "Published to crates.io"
}
# Target architectures for macOS
TARGETS=("x86_64-apple-darwin" "aarch64-apple-darwin")
# Step 4: Build release binaries for all targets
build_release() {
info "Building release binaries for all architectures..."
for target in "${TARGETS[@]}"; do
info "Building for $target..."
# Ensure the target is installed
if ! rustup target list --installed | grep -q "$target"; then
info "Installing target $target..."
rustup target add "$target"
fi
cargo build --release --target "$target"
if [[ ! -f "target/$target/release/lumen" ]]; then
error "Release binary not found at target/$target/release/lumen"
fi
success "Built for $target"
done
success "All release binaries built"
}
# Step 5: Create tarballs for each architecture
create_tarball() {
info "Creating tarballs for all architectures..."
for target in "${TARGETS[@]}"; do
info "Creating tarball for $target..."
cd "target/$target/release"
tar -czf "lumen-$target.tar.gz" lumen
cd "$SCRIPT_DIR"
if [[ ! -f "target/$target/release/lumen-$target.tar.gz" ]]; then
error "Failed to create tarball for $target"
fi
success "Tarball created for $target"
done
success "All tarballs created"
}
# Step 6: Calculate SHA256 for each architecture
calculate_sha256() {
local target="$1"
local sha256
sha256=$(shasum -a 256 "target/$target/release/lumen-$target.tar.gz" | awk '{print $1}')
echo "$sha256"
}
# Generate release notes from commits since last tag
generate_release_notes() {
local version="$1"
local last_tag
local notes="## What's Changed\n\n"
# Get the last tag (most recent tag before HEAD)
last_tag=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
if [[ -z "$last_tag" ]]; then
# No previous tag, get all commits
info "No previous tag found, including all commits" >&2
while IFS= read -r line; do
local hash=$(echo "$line" | cut -d' ' -f1)
local message=$(echo "$line" | cut -d' ' -f2-)
notes+="* $message ([${hash:0:7}](https://github.com/jnsahaj/lumen/commit/$hash))\n"
done < <(git log --oneline --format="%H %s")
else
info "Generating changelog since $last_tag" >&2
while IFS= read -r line; do
local hash=$(echo "$line" | cut -d' ' -f1)
local message=$(echo "$line" | cut -d' ' -f2-)
notes+="* $message ([${hash:0:7}](https://github.com/jnsahaj/lumen/commit/$hash))\n"
done < <(git log --oneline --format="%H %s" "$last_tag"..HEAD)
fi
echo -e "$notes"
}
# Step 7: Create GitHub release and upload
create_github_release() {
local version="$1"
local tag="v$version"
info "Creating GitHub release $tag..."
# Check if tag already exists
if git tag -l | grep -q "^$tag$"; then
warn "Tag $tag already exists locally"
if ! confirm "Delete and recreate tag?"; then
error "Aborted by user"
fi
git tag -d "$tag"
fi
# Create and push tag
git tag "$tag"
git push origin "$tag"
# Generate release notes
local release_notes
release_notes=$(generate_release_notes "$version")
# Collect all tarballs
local tarballs=()
for target in "${TARGETS[@]}"; do
tarballs+=("target/$target/release/lumen-$target.tar.gz")
done
# Create release with gh CLI
gh release create "$tag" \
--title "v$version" \
--notes "$release_notes" \
"${tarballs[@]}"
success "GitHub release created and tarballs uploaded"
}
# Step 8: Update homebrew formula
update_homebrew_formula() {
local version="$1"
local sha256_intel="$2"
local sha256_arm="$3"
local url_intel="https://github.com/jnsahaj/lumen/releases/download/v$version/lumen-x86_64-apple-darwin.tar.gz"
local url_arm="https://github.com/jnsahaj/lumen/releases/download/v$version/lumen-aarch64-apple-darwin.tar.gz"
info "Updating homebrew formula..."
cd "$HOMEBREW_REPO"
# Pull latest changes
git pull origin main --rebase
# Generate new formula content
cat > Formula/lumen.rb << EOF
class Lumen < Formula
desc "lumen is a command-line tool that can show pretty diff, generate commit messages with AI, summarise diffs / commits, and more without requiring an API key."
homepage "https://github.com/jnsahaj/lumen"
version "$version"
on_intel do
url "$url_intel"
sha256 "$sha256_intel"
end
on_arm do
url "$url_arm"
sha256 "$sha256_arm"
end
def install
bin.install "lumen"
end
end
EOF
# Show the diff
echo ""
info "Changes to Formula/lumen.rb:"
git diff Formula/lumen.rb
echo ""
if ! confirm "Commit and push these changes?"; then
git checkout Formula/lumen.rb
cd "$SCRIPT_DIR"
error "Aborted by user"
fi
# Commit and push
git add Formula/lumen.rb
git commit -m "chore: Bump ver to $version"
git push origin main
cd "$SCRIPT_DIR"
success "Homebrew formula updated and pushed"
}
# Push main branch changes
push_main_changes() {
info "Pushing changes to main branch..."
git push origin main
success "Changes pushed to main"
}
# Main release flow
main() {
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--auto|-a)
AUTO_MODE=true
shift
;;
*)
shift
;;
esac
done
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Lumen Release Script${NC}"
echo -e "${GREEN}========================================${NC}"
if [[ "$AUTO_MODE" == true ]]; then
echo -e "${GREEN} (Auto Mode)${NC}"
fi
echo ""
# Change to script directory
cd "$SCRIPT_DIR"
# Check prerequisites
check_prerequisites
# Get current version
local current_version
current_version=$(get_current_version)
info "Current version: $current_version"
# Prompt for new version
local new_version
prompt_input "Enter new version" new_version ""
if [[ -z "$new_version" ]]; then
error "Version cannot be empty"
fi
validate_version "$new_version"
if [[ "$new_version" == "$current_version" ]]; then
error "New version is the same as current version"
fi
echo ""
echo -e "${YELLOW}Release Plan:${NC}"
echo " 1. Update Cargo.toml version to $new_version"
echo " 2. Commit Cargo.toml and Cargo.lock"
echo " 3. Publish to crates.io"
echo " 4. Build release binaries (Intel + ARM)"
echo " 5. Create tarballs for each architecture"
echo " 6. Create GitHub release v$new_version and upload tarballs"
echo " 7. Push main branch changes"
echo " 8. Update homebrew formula (with arch-specific URLs)"
echo ""
if ! confirm "Proceed with release?"; then
error "Aborted by user"
fi
echo ""
# Execute release steps
update_cargo_version "$new_version"
echo ""
commit_version_changes "$new_version"
echo ""
publish_to_crates
echo ""
build_release
echo ""
create_tarball
echo ""
local sha256_intel sha256_arm
sha256_intel=$(calculate_sha256 "x86_64-apple-darwin")
sha256_arm=$(calculate_sha256 "aarch64-apple-darwin")
info "SHA256 (Intel): $sha256_intel"
info "SHA256 (ARM): $sha256_arm"
echo ""
create_github_release "$new_version"
echo ""
push_main_changes
echo ""
update_homebrew_formula "$new_version" "$sha256_intel" "$sha256_arm"
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Release v$new_version Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "Summary:"
echo " - Cargo.toml updated to v$new_version"
echo " - Published to crates.io"
echo " - GitHub release: https://github.com/jnsahaj/lumen/releases/tag/v$new_version"
echo " - Homebrew formula updated"
echo ""
echo "Users can now install with: brew install jnsahaj/lumen/lumen"
echo ""
}
# Run main function
main "$@"