Elffuss BraidHub

kikocis/continue public

e31fcddb0dec · 18.0 KiB · executableRawPermalink
1#!/usr/bin/env bash
2set -e
3
4# oneper - Install Continue VS Code extension from PR builds or latest pre-release
5# Usage:
6# oneper install --pr X - Install VSIX from PR number X
7# oneper install --latest - Install latest pre-release from marketplace
8
9REPO="continuedev/continue"
10ONEPER_DIR="$HOME/.continue/.utils/oneper"
11
12show_help() {
13 echo "oneper - Install Continue VS Code extension from builds or latest pre-release"
14 echo ""
15 echo "Usage: oneper <command> [options]"
16 echo " oneper -h | --help"
17 echo " oneper install # Install latest build from main branch (default)"
18 echo ""
19 echo "Commands:"
20 echo " install Download and install VSIX from latest main branch build (default)"
21 echo " install --pr <number> [--platform <platform>]"
22 echo " Download and install VSIX from specified PR number"
23 echo " Platform options: macos, linux (auto-detected by default)"
24 echo " install --latest Install latest pre-release from VS Code marketplace"
25 echo " setup-cron [--interval <minutes>] Set up automatic updates (default: 10 minutes)"
26 echo " remove-cron Remove automatic updates"
27 echo " clean Remove all downloaded VSIX files from oneper cache"
28 echo ""
29 echo "Options:"
30 echo " -h, --help Show this help message"
31 echo " --platform <platform> Specify platform for builds (macos, linux)
32 --interval <minutes> Set update interval in minutes (for setup-cron)"
33 echo ""
34 echo "Examples:"
35 echo " oneper install # Install latest build from main branch"
36 echo " oneper install --pr 123 # Install VSIX from PR #123"
37 echo " oneper install --pr 123 --platform linux # Install Linux VSIX from PR #123"
38 echo " oneper install --latest # Install latest pre-release"
39 echo " oneper setup-cron # Auto-update every 10 minutes
40 oneper setup-cron --interval 5 # Auto-update every 5 minutes"
41 echo " oneper remove-cron # Stop auto-updates"
42 echo " oneper clean # Clear cached VSIX files"
43 echo " oneper -h # Show help"
44 echo ""
45 echo "VSIX files are cached in: ~/.continue/.utils/oneper/"
46}
47
48detect_os() {
49 case "$(uname -s)" in
50 "Darwin")
51 echo "macos"
52 ;;
53 "Linux")
54 echo "linux"
55 ;;
56 *)
57 echo "❌ Unsupported operating system: $(uname -s)" >&2
58 echo " Supported platforms: macOS, Linux" >&2
59 exit 1
60 ;;
61 esac
62}
63
64check_dependencies() {
65 if ! command -v gh &> /dev/null; then
66 echo "❌ GitHub CLI (gh) is required but not installed."
67 echo " Install with: brew install gh"
68 exit 1
69 fi
70
71 if ! command -v code &> /dev/null; then
72 echo "❌ VS Code CLI (code) is required but not installed."
73 echo " Install VS Code and ensure 'code' command is in PATH"
74 exit 1
75 fi
76
77}
78
79ensure_oneper_dir() {
80 if [ ! -d "$ONEPER_DIR" ]; then
81 echo "📁 Creating oneper directory: $ONEPER_DIR"
82 mkdir -p "$ONEPER_DIR"
83 fi
84}
85
86install_from_main() {
87 local platform=${1:-$(detect_os)} # Auto-detect OS if not specified
88
89 # Map platform names to GitHub runner OS names
90 local artifact_suffix
91 case "$platform" in
92 "macos")
93 artifact_suffix="macOS"
94 ;;
95 "linux")
96 artifact_suffix="Linux"
97 ;;
98 *)
99 echo "❌ Unknown platform: $platform"
100 echo " Supported platforms: macos, linux"
101 exit 1
102 ;;
103 esac
104
105 echo "🔍 Finding latest main branch build..."
106
107 # Get the latest successful workflow run for main branch
108 local run_id=$(gh run list \
109 --repo "$REPO" \
110 --workflow="main-build.yaml" \
111 --branch="main" \
112 --json databaseId,status,conclusion \
113 --jq ".[] | select(.status == \"completed\" and .conclusion == \"success\") | .databaseId" \
114 | head -1)
115
116 if [ -z "$run_id" ]; then
117 echo "❌ No successful main branch build found"
118 echo " Make sure there are recent commits to main with successful builds"
119 exit 1
120 fi
121
122 echo "✅ Found workflow run: https://github.com/$REPO/actions/runs/$run_id"
123
124 # Try to get the original PR number from the workflow run
125 local original_pr=$(gh run view "$run_id" --repo "$REPO" --json displayTitle --jq '.displayTitle' | grep -o "pull request #[0-9]*" | grep -o "[0-9]*" || echo "")
126
127 if [ -n "$original_pr" ]; then
128 echo "📦 This contains artifacts originally from PR #$original_pr"
129 fi
130
131 echo "📥 Downloading vscode-extension-build-$artifact_suffix artifact..."
132
133 # Create temporary directory for download
134 local temp_dir=$(mktemp -d)
135
136 # Download the artifact
137 if ! gh run download "$run_id" \
138 --repo "$REPO" \
139 --name "vscode-extension-build-$artifact_suffix" \
140 --dir "$temp_dir"; then
141 echo "❌ Failed to download artifact vscode-extension-build-$artifact_suffix"
142 echo " Make sure the workflow run completed successfully for $platform"
143 rm -rf "$temp_dir"
144 exit 1
145 fi
146
147 # Find the VSIX file in the downloaded artifact
148 local vsix_file=$(find "$temp_dir" -name "*.vsix" | head -1)
149
150 if [ -z "$vsix_file" ]; then
151 echo "❌ No VSIX file found in artifact"
152 rm -rf "$temp_dir"
153 exit 1
154 fi
155
156 # Extract version from the original VSIX filename (e.g., continue-1.1.66.vsix -> 1.1.66)
157 local original_filename=$(basename "$vsix_file")
158 local version=$(echo "$original_filename" | sed 's/continue-\(.*\)\.vsix/\1/')
159
160 if [ -z "$version" ]; then
161 echo "⚠️ Could not extract version from filename: $original_filename"
162 version="unknown"
163 fi
164
165 # Create new VSIX name with version and "main" suffix
166 local target_path="$ONEPER_DIR/continue-${version}-main.vsix"
167
168 # Check if file already exists and notify about overwrite
169 if [ -f "$target_path" ]; then
170 echo "⚠️ Overwriting existing VSIX: $target_path"
171 fi
172
173 echo "📦 Moving VSIX to: $target_path"
174 mv "$vsix_file" "$target_path"
175
176 # Clean up temp directory
177 rm -rf "$temp_dir"
178
179 # Install the extension
180 echo "🚀 Installing VS Code extension..."
181 if code --install-extension "$target_path" --force; then
182 echo "✅ Successfully installed Continue extension from main branch"
183 echo "📄 VSIX file saved to: $target_path"
184 echo ""
185 echo "💡 To use the new extension version, reload your VS Code window:"
186 echo " • Press Ctrl+Shift+P (Cmd+Shift+P on Mac)"
187 echo " • Type 'Developer: Reload Window' and press Enter"
188 echo " • Or restart VS Code entirely"
189 else
190 echo "❌ Failed to install extension"
191 exit 1
192 fi
193}
194
195install_from_pr() {
196 local pr_number=$1
197 local platform=${2:-$(detect_os)} # Auto-detect OS if not specified
198
199 if [ -z "$pr_number" ]; then
200 echo "❌ PR number is required"
201 show_help
202 exit 1
203 fi
204
205 # Map platform names to artifact names for the new workflow
206 local artifact_name
207 case "$platform" in
208 "macos")
209 artifact_name="darwin-arm64-vsix"
210 ;;
211 "linux")
212 artifact_name="linux-x64-vsix"
213 ;;
214 *)
215 echo "❌ Unknown platform: $platform"
216 echo " Supported platforms: macos, linux"
217 exit 1
218 ;;
219 esac
220
221 echo "🔍 Getting branch name for PR #$pr_number..."
222
223 # First get the branch name for the PR
224 local branch_name=$(gh pr view "$pr_number" --repo "$REPO" --json headRefName --jq '.headRefName')
225
226 if [ -z "$branch_name" ]; then
227 echo "❌ Could not find PR #$pr_number"
228 exit 1
229 fi
230
231 echo "🔍 Finding latest workflow run for branch: $branch_name..."
232
233 # Get the latest successful workflow run for the branch
234 local run_id=$(gh run list \
235 --repo "$REPO" \
236 --workflow="pr-build-upload-vsix.yaml" \
237 --branch="$branch_name" \
238 --json databaseId,status,conclusion \
239 --jq ".[] | select(.status == \"completed\" and .conclusion == \"success\") | .databaseId" \
240 | head -1)
241
242 if [ -z "$run_id" ]; then
243 echo "❌ No successful workflow run found for PR #$pr_number"
244 echo " Make sure the PR exists and has a successful CI run"
245 exit 1
246 fi
247
248 echo "✅ Found workflow run: $run_id"
249 echo "📥 Downloading $artifact_name artifact..."
250
251 # Create temporary directory for download
252 local temp_dir=$(mktemp -d)
253
254 # Download the artifact
255 if ! gh run download "$run_id" \
256 --repo "$REPO" \
257 --name "$artifact_name" \
258 --dir "$temp_dir"; then
259 echo "❌ Failed to download artifact $artifact_name"
260 echo " Make sure the workflow run completed successfully for $platform"
261 rm -rf "$temp_dir"
262 exit 1
263 fi
264
265 # Find the VSIX file in the downloaded artifact
266 local vsix_file=$(find "$temp_dir" -name "*.vsix" | head -1)
267
268 if [ -z "$vsix_file" ]; then
269 echo "❌ No VSIX file found in artifact"
270 rm -rf "$temp_dir"
271 exit 1
272 fi
273
274 # Extract full version from the original VSIX filename (e.g., continue-1.1.66-a1b2c3d.vsix -> 1.1.66-a1b2c3d)
275 local original_filename=$(basename "$vsix_file")
276 local full_version=$(echo "$original_filename" | sed 's/continue-\(.*\)\.vsix/\1/')
277
278 if [ -z "$full_version" ]; then
279 echo "⚠️ Could not extract version from filename: $original_filename"
280 full_version="unknown-pr${pr_number}"
281 fi
282
283 # Use the full version (which includes commit SHA) for the target filename
284 local target_path="$ONEPER_DIR/continue-${full_version}.vsix"
285
286 # Check if file already exists and notify about overwrite
287 if [ -f "$target_path" ]; then
288 echo "⚠️ Overwriting existing VSIX: $target_path"
289 fi
290
291 echo "📦 Moving VSIX to: $target_path"
292 mv "$vsix_file" "$target_path"
293
294 # Clean up temp directory
295 rm -rf "$temp_dir"
296
297 # Install the extension
298 echo "🚀 Installing VS Code extension..."
299 if code --install-extension "$target_path"; then
300 echo "✅ Successfully installed Continue extension from PR #$pr_number"
301 echo "📄 VSIX file saved to: $target_path"
302 echo "🔖 Extension version: ${full_version} (includes commit SHA)"
303 echo ""
304 echo "💡 To use the new extension version, reload your VS Code window:"
305 echo " • Press Ctrl+Shift+P (Cmd+Shift+P on Mac)"
306 echo " • Type 'Developer: Reload Window' and press Enter"
307 echo " • Or restart VS Code entirely"
308 else
309 echo "❌ Failed to install extension"
310 exit 1
311 fi
312}
313
314install_latest() {
315 echo "🚀 Installing latest pre-release Continue extension..."
316 if code --install-extension --pre-release Continue.continue --force; then
317 echo "✅ Successfully installed latest pre-release Continue extension"
318 echo ""
319 echo "💡 To use the new extension version, reload your VS Code window:"
320 echo " • Press Ctrl+Shift+P (Cmd+Shift+P on Mac)"
321 echo " • Type 'Developer: Reload Window' and press Enter"
322 echo " • Or restart VS Code entirely"
323 else
324 echo "❌ Failed to install extension"
325 exit 1
326 fi
327}
328
329get_latest_main_run_id() {
330 gh run list \
331 --repo "$REPO" \
332 --workflow="main-build.yaml" \
333 --branch="main" \
334 --json databaseId,status,conclusion \
335 --jq ".[] | select(.status == \"completed\" and .conclusion == \"success\") | .databaseId" \
336 | head -1
337}
338
339check_and_install_if_new() {
340 local current_run_id=$(get_latest_main_run_id)
341
342 if [ -z "$current_run_id" ]; then
343 echo "$(date): ❌ No successful main branch build found" >&2
344 return 1
345 fi
346
347 local last_run_file="$ONEPER_DIR/last_installed_run"
348 local last_run_id=""
349
350 if [ -f "$last_run_file" ]; then
351 last_run_id=$(cat "$last_run_file")
352 fi
353
354 if [ "$current_run_id" != "$last_run_id" ]; then
355 echo "$(date): 🔄 New build detected (https://github.com/$REPO/runs/$current_run_id), installing..."
356
357 # Install from main silently
358 if install_from_main > /dev/null 2>&1; then
359 echo "$current_run_id" > "$last_run_file"
360 echo "$(date): ✅ Successfully installed new build from https://github.com/$REPO/actions/runs/$current_run_id"
361 else
362 echo "$(date): ❌ Failed to install new build" >&2
363 return 1
364 fi
365 else
366 echo "$(date): 🔍 No new builds (current: https://github.com/$REPO/actions/runs/$current_run_id)"
367 fi
368}
369
370setup_cron() {
371 local interval=${1:-10} # Default to 10 minutes if not specified
372 echo "🔧 Setting up automatic updates every $interval minutes..."
373
374 # Get the absolute path to this script
375 local script_path=$(realpath "$0")
376
377 # Create a wrapper script that calls the check function
378 local cron_script="$ONEPER_DIR/oneper-cron.sh"
379
380 cat > "$cron_script" << EOF
381#!/bin/bash
382# Auto-generated oneper cron script
383export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:\$PATH"
384cd "\$(dirname "$script_path")"
385"$script_path" --cron-check
386EOF
387
388 chmod +x "$cron_script"
389
390 # Create log file
391 local log_file="$ONEPER_DIR/oneper-cron.log"
392 touch "$log_file"
393
394 # Add to crontab
395 local cron_line="*/$interval * * * * $cron_script >> $log_file 2>&1"
396
397 # Check if cron job already exists
398 if crontab -l 2>/dev/null | grep -q "oneper-cron.sh"; then
399 echo "⚠️ Oneper cron job already exists. Removing old one..."
400 crontab -l 2>/dev/null | grep -v "oneper-cron.sh" | crontab -
401 fi
402
403 # Add new cron job
404 (crontab -l 2>/dev/null; echo "$cron_line") | crontab -
405
406 echo "✅ Automatic updates configured!"
407 echo "📊 Logs will be written to: $log_file"
408 echo "🔍 Check status with: tail -f $log_file"
409 echo ""
410 echo "💡 To remove automatic updates: oneper remove-cron"
411}
412
413remove_cron() {
414 echo "🗑️ Removing automatic updates..."
415
416 # Remove from crontab
417 if crontab -l 2>/dev/null | grep -q "oneper-cron.sh"; then
418 crontab -l 2>/dev/null | grep -v "oneper-cron.sh" | crontab -
419 echo "✅ Removed cron job"
420 else
421 echo "ℹ️ No oneper cron job found"
422 fi
423
424 # Clean up files
425 local cron_script="$ONEPER_DIR/oneper-cron.sh"
426 local log_file="$ONEPER_DIR/oneper-cron.log"
427 local last_run_file="$ONEPER_DIR/last_installed_run"
428
429 [ -f "$cron_script" ] && rm "$cron_script" && echo "🗑️ Removed cron script"
430 [ -f "$log_file" ] && rm "$log_file" && echo "🗑️ Removed log file"
431 [ -f "$last_run_file" ] && rm "$last_run_file" && echo "🗑️ Removed state file"
432
433 echo "✅ Automatic updates removed successfully"
434}
435
436clean_cache() {
437 if [ ! -d "$ONEPER_DIR" ]; then
438 echo "📁 Oneper cache directory doesn't exist: $ONEPER_DIR"
439 return 0
440 fi
441
442 local file_count=$(find "$ONEPER_DIR" -name "*.vsix" | wc -l | tr -d ' ')
443
444 if [ "$file_count" -eq 0 ]; then
445 echo "✨ Oneper cache is already empty"
446 return 0
447 fi
448
449 echo "🧹 Removing $file_count VSIX file(s) from oneper cache..."
450 rm -f "$ONEPER_DIR"/*.vsix
451
452 echo "✅ Oneper cache cleared successfully"
453 echo "📁 Cache directory: $ONEPER_DIR"
454}
455
456main() {
457 # Handle special internal commands first
458 case "$1" in
459 "--cron-check")
460 # Internal command used by cron job
461 ensure_oneper_dir
462 check_and_install_if_new
463 exit $?
464 ;;
465 "-h"|"--help")
466 show_help
467 exit 0
468 ;;
469 esac
470
471 # If no arguments, default to installing from main
472 if [ $# -eq 0 ]; then
473 check_dependencies
474 ensure_oneper_dir
475 install_from_main
476 exit 0
477 fi
478
479 check_dependencies
480 ensure_oneper_dir
481
482 case "$1" in
483 "install")
484 case "$2" in
485 "--pr")
486 # Parse PR number and optional platform
487 local pr_number="$3"
488 local platform=$(detect_os) # auto-detect OS
489
490 # Check if --platform is specified to override auto-detection
491 if [ "$4" = "--platform" ] && [ -n "$5" ]; then
492 platform="$5"
493 fi
494
495 install_from_pr "$pr_number" "$platform"
496 ;;
497 "--latest")
498 install_latest
499 ;;
500 "--platform")
501 # Handle: oneper install --platform <platform> (install from main with specific platform)
502 local platform="$3"
503 install_from_main "$platform"
504 ;;
505 "")
506 # Handle: oneper install (install from main with auto-detected platform)
507 install_from_main
508 ;;
509 *)
510 echo "❌ Unknown install option: $2"
511 show_help
512 exit 1
513 ;;
514 esac
515 ;;
516 "setup-cron")
517 # Check for --interval option
518 local interval=10
519 if [ "$2" = "--interval" ] && [ -n "$3" ]; then
520 interval="$3"
521 # Validate interval is a positive integer
522 if ! [[ "$interval" =~ ^[1-9][0-9]*$ ]]; then
523 echo "❌ Invalid interval: $interval. Must be a positive integer."
524 exit 1
525 fi
526 fi
527 setup_cron "$interval"
528 ;;
529 "remove-cron")
530 remove_cron
531 ;;
532 "clean")
533 clean_cache
534 ;;
535 *)
536 echo "❌ Unknown command: $1"
537 show_help
538 exit 1
539 ;;
540 esac
541}
542
543main "$@"