Repository navigation
Expand file tree
/
Copy pathcvm.sh
More file actions
executable file
·474 lines (427 loc) · 13.5 KB
/
Copy pathcvm.sh
File metadata and controls
executable file
·474 lines (427 loc) · 13.5 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
#!/bin/sh
#H#
#H# cvm.sh — Cursor version manager
#H#
#H# Examples:
#H# ./cvm.sh --version
#H# sh cvm.sh --list-local
#H# bash cvm.sh --use 0.40.4
#H#
#H# Notice*:
#H# The AppImage files are downloaded from the official Cursor releases.
#H# The list of download sources can be found at https://github.com/oslook/cursor-ai-downloads
#H#
#H# Options:
#H# --list-local Lists locally available versions
#H# --list-remote Lists versions available for download
#H# --download <version> Downloads a version
#H# --update Downloads and selects the latest version
#H# --use <version> Selects a locally available version
#H# --active Shows the currently selected version
#H# --remove <version> Removes a locally available version
#H# --install Adds an alias `cursor` and downloads the latest version
#H# --uninstall Removes the Cursor version manager directory and alias
#H# --update-script Updates the (cvm.sh) script to the latest version
#H# -v --version Shows the current and latest versions for cvm.sh and Cursor
#H# -h --help Shows this message
#
# Constants
#
CURSOR_DIR="$HOME/.local/share/cvm"
DOWNLOADS_DIR="$CURSOR_DIR/app-images"
CVM_VERSION="1.4.0"
_CACHE_FILE="/tmp/cursor_versions.json"
VERSION_HISTORY_URL="https://raw.githubusercontent.com/oslook/cursor-ai-downloads/refs/heads/main/version-history.json"
GITHUB_API_URL="https://api.github.com/repos/ivstiv/cursor-version-manager/releases/latest"
# Color definitions
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
NC='\033[0m' # No Color
#
# Functions
#
help() {
sed -rn 's/^#H# ?//;T;p' "$0"
}
print_color() {
color=$1
text=$2
printf "%b%s%b\n" "$color" "$text" "$NC"
}
getLatestScriptVersion() {
# Fetch latest release version from GitHub API
latest_version=$(wget -qO- "$GITHUB_API_URL" | jq -r '.tag_name' 2>/dev/null)
if [ -n "$latest_version" ]; then
echo "$latest_version"
return 0
else
return 1
fi
}
getVersionHistory() {
# Check if cache file exists and is less than 15 min old
if [ -f "$_CACHE_FILE" ] && [ -n "$(find "$_CACHE_FILE" -mmin -15 2>/dev/null)" ]; then
cat "$_CACHE_FILE"
return 0
fi
# Fetch JSON directly from remote and cache it
# echo "Fetching version history..." >&2
if wget -qO "$_CACHE_FILE.tmp" "$VERSION_HISTORY_URL"; then
mv "$_CACHE_FILE.tmp" "$_CACHE_FILE"
cat "$_CACHE_FILE"
return 0
else
rm -f "$_CACHE_FILE.tmp"
echo "Error: Failed to fetch version history" >&2
return 1
fi
}
getPlatform() {
architecture=$(uname -m)
case "$architecture" in
x86_64)
echo "linux-x64"
;;
aarch64|arm64)
echo "linux-arm64"
;;
*)
echo "Unsupported architecture: $architecture"
;;
esac
}
platform=$(getPlatform)
getRemoteVersions() {
getVersionHistory | \
jq -r ".versions[] | select(.platforms[\"$platform\"] != null) | .version" \
| sort -V
}
getLatestRemoteVersion() {
getVersionHistory | \
jq -r ".versions[] | select(.platforms[\"$platform\"] != null) | .version" \
| sort -V \
| tail -n1
}
getLatestLocalVersion() {
# shellcheck disable=SC2010
ls -1 "$DOWNLOADS_DIR" \
| grep -oP 'cursor-\K[0-9.]+(?=\.)' \
| sort -r \
| head -n 1
}
downloadVersion() {
version=$1 # e.g. 2.1.0
if [ -z "$version" ]; then
echo "Error: Version number is required, use \`cvm --list-remote\` to see available versions" >&2
return 1
fi
localFilename="cursor-$version.AppImage"
url=$(
getVersionHistory | \
jq -r --arg v "$version" --arg platform "$platform" '.versions[] | select(.version == $v and .platforms[$platform] != null) | .platforms[$platform]'
)
echo "Downloading Cursor $version..."
wget -O "$DOWNLOADS_DIR/$localFilename" "$url"
chmod +x "$DOWNLOADS_DIR/$localFilename"
echo "Cursor $version downloaded to $DOWNLOADS_DIR/$localFilename"
}
selectVersion() {
version=$1 # e.g. 2.1.0
filename="cursor-$version.AppImage"
appimage_path="$DOWNLOADS_DIR/$filename"
ln -sf "$appimage_path" "$CURSOR_DIR/active"
echo "Symlink created: $CURSOR_DIR/active -> $appimage_path"
}
getActiveVersion() {
if [ -L "$CURSOR_DIR/active" ]; then
appimage_path=$(readlink -f "$CURSOR_DIR/active")
version=$(basename "$appimage_path" | sed -E 's/cursor-([0-9.]+)\.AppImage/\1/')
echo "$version"
else
echo "No active version. Use \`cvm --use <version>\` to select one."
exit 1
fi
}
exitIfVersionNotInstalled() {
version=$1
appimage_path="$DOWNLOADS_DIR/cursor-$version.AppImage"
if [ ! -f "$appimage_path" ]; then
echo "Version $version not found locally. Use \`cvm --list-local\` to list available versions."
exit 1
fi
}
installCVM() {
latestRemoteVersion=$(getLatestRemoteVersion)
latestLocalVersion=$(getLatestLocalVersion)
if [ "$latestRemoteVersion" != "$latestLocalVersion" ]; then
downloadVersion "$latestRemoteVersion"
fi
selectVersion "$latestRemoteVersion"
echo "Cursor $latestRemoteVersion installed and activated."
echo "Adding alias to your shell config..."
case "$(basename "$SHELL")" in
sh|dash)
if ! grep -q "alias cursor='$CURSOR_DIR/active'" "$HOME/.profile"; then
echo "alias cursor='$CURSOR_DIR/active'" >> "$HOME/.profile"
fi
;;
bash)
if ! grep -q "alias cursor='$CURSOR_DIR/active'" "$HOME/.bashrc"; then
echo "alias cursor='$CURSOR_DIR/active'" >> "$HOME/.bashrc"
fi
;;
zsh)
if ! grep -q "alias cursor='$CURSOR_DIR/active'" "$HOME/.zshrc"; then
echo "alias cursor='$CURSOR_DIR/active'" >> "$HOME/.zshrc"
fi
;;
fish)
if [ ! -f "$HOME/.config/fish/functions/cursor.fish" ] || ! grep -q "function cursor" "$HOME/.config/fish/functions/cursor.fish"; then
mkdir -p "$HOME/.config/fish/functions"
echo "function cursor" > "$HOME/.config/fish/functions/cursor.fish"
echo " $CURSOR_DIR/active \$argv" >> "$HOME/.config/fish/functions/cursor.fish"
echo "end" >> "$HOME/.config/fish/functions/cursor.fish"
fi
;;
esac
echo "Alias added. You can now use 'cursor' to run Cursor."
case "$(basename "$SHELL")" in
sh|dash)
echo "Run '. ~/.profile' to apply the changes or restart your shell."
;;
bash)
echo "Run 'source ~/.bashrc' to apply the changes or restart your shell."
;;
zsh)
echo "Run 'source ~/.zshrc' to apply the changes or restart your shell."
;;
fish)
echo "The cursor function has been added in ~/.config/fish/functions/cursor.fish. You can use it immediately."
;;
esac
}
uninstallCVM() {
rm -rf "$CURSOR_DIR"
case "$(basename "$SHELL")" in
sh|dash)
if grep -q "alias cursor='$CURSOR_DIR/active'" "$HOME/.profile"; then
sed -i "\#alias cursor='$CURSOR_DIR/active'#d" "$HOME/.profile"
echo "Alias removed from ~/.profile"
echo "Run '. ~/.profile' to apply the changes or restart your shell."
fi
;;
bash)
if grep -q "alias cursor='$CURSOR_DIR/active'" "$HOME/.bashrc"; then
sed -i "\#alias cursor='$CURSOR_DIR/active'#d" "$HOME/.bashrc"
echo "Alias removed from ~/.bashrc"
echo "Run 'source ~/.bashrc' to apply the changes or restart your shell."
fi
;;
zsh)
if grep -q "alias cursor='$CURSOR_DIR/active'" "$HOME/.zshrc"; then
sed -i "\#alias cursor='$CURSOR_DIR/active'#d" "$HOME/.zshrc"
echo "Alias removed from ~/.zshrc"
echo "Run 'source ~/.zshrc' to apply the changes or restart your shell."
fi
;;
fish)
if [ -f "$HOME/.config/fish/functions/cursor.fish" ]; then
rm "$HOME/.config/fish/functions/cursor.fish"
echo "Cursor function removed from ~/.config/fish/functions/cursor.fish"
fi
;;
esac
echo "Cursor version manager uninstalled."
}
checkDependencies() {
mainShellPID="$$"
printf "sed\ngrep\njq\nfind\nwget\n" | while IFS= read -r program; do
if ! [ -x "$(command -v "$program")" ]; then
echo "Error: $program is not installed." >&2
kill -9 "$mainShellPID"
fi
done
}
isShellSupported() {
case "$(basename "$SHELL")" in
sh|dash|bash|zsh|fish)
return 0
;;
*)
return 1
;;
esac
}
cleanupAppImages() {
for build_file in "$DOWNLOADS_DIR"/cursor-*-build-*-x86_64.AppImage; do
# Skip if no files match the pattern
[ -e "$build_file" ] || continue
# Extract version number from build file
version=$(basename "$build_file" | sed -E 's/cursor-([0-9.]+)-build.*/\1/')
regular_file="$DOWNLOADS_DIR/cursor-$version.AppImage"
if [ -f "$regular_file" ]; then
# If regular version exists, remove build version
rm "$build_file"
# echo "Removed build version for $version (regular version exists)"
else
# If only build version exists, rename it to regular format
mv "$build_file" "$regular_file"
# echo "Renamed build version to regular format for $version"
fi
done
}
updateScript() {
version=$(getLatestScriptVersion)
if [ -z "$version" ]; then
echo "Error: Failed to determine version to download" >&2
return 1
fi
# Get the download URL from the release assets
download_url=$(
wget -O- "$GITHUB_API_URL" \
| jq -r '.assets[] | select(.name == "cvm.sh") | .browser_download_url'
)
if [ -z "$download_url" ]; then
echo "Error: Failed to find download URL for cvm.sh" >&2
return 1
fi
echo "Downloading CVM version ${version}..."
# Download to a temporary file in the same directory
script_dir=$(dirname "$0")
temp_file="${script_dir}/cvm.sh.new"
if wget -qO "$temp_file" "$download_url"; then
chmod +x "$temp_file"
mv "$temp_file" "$0"
echo "Successfully updated to version ${version}"
echo "Please run the script again to use the new version"
return 0
else
rm -f "$temp_file"
echo "Error: Failed to download version ${version}" >&2
return 1
fi
}
#
# Execution
#
if ! isShellSupported; then
echo "Error: Unsupported shell. Please use bash, zsh, fish, or sh."
echo "Currently using: $(basename "$SHELL")"
echo "Open a github issue if you want to add support for your shell:"
echo "https://github.com/ivstiv/cursor-version-manager/issues"
exit 1
fi
checkDependencies
mkdir -p "$DOWNLOADS_DIR"
cleanupAppImages
case "$1" in
--help|-h)
help
;;
--version|-v)
echo "Cursor Version Manager (cvm.sh):"
echo " - Current version: $CVM_VERSION"
if latest_version=$(getLatestScriptVersion); then
if [ "$latest_version" != "$CVM_VERSION" ]; then
echo " - Latest version: $latest_version"
print_color "$ORANGE" "There is a newer cvm.sh version available for download!"
print_color "$ORANGE" "You can update the script with: $0 --update-script"
else
print_color "$GREEN" "You are running the latest cvm.sh version!"
fi
else
echo "Failed to check for latest cvm.sh version"
fi
echo ""
echo "Cursor App Information:"
latestRemoteVersion=$(getLatestRemoteVersion)
latestLocalVersion=$(getLatestLocalVersion)
activeVersion=$(getActiveVersion 2>/dev/null || echo "None")
echo " - Latest remote version: $latestRemoteVersion"
echo " - Latest locally available: $latestLocalVersion"
echo " - Currently active: $activeVersion"
if [ "$latestRemoteVersion" != "$latestLocalVersion" ]; then
print_color "$ORANGE" "There is a newer Cursor version available for download!"
print_color "$ORANGE" "You can download and activate it with \`cvm --update\`"
elif [ "$latestRemoteVersion" != "$activeVersion" ]; then
print_color "$ORANGE" "There is a newer Cursor version already installed!"
print_color "$ORANGE" "You can activate it with \`cvm --use $latestRemoteVersion\`"
else
print_color "$GREEN" "You are running the latest Cursor version!"
fi
;;
--update)
latestVersion=$(getLatestRemoteVersion)
downloadVersion "$latestVersion"
selectVersion "$version"
;;
--list-local)
echo "Locally available versions:"
# shellcheck disable=SC2010
ls -1 "$DOWNLOADS_DIR" \
| grep -oP 'cursor-\K[0-9.]+(?=\.)' \
| sed 's/^/ - /'
;;
--list-remote)
echo "Remote versions:"
getRemoteVersions | sed 's/^/ - /'
;;
--download)
version=$2
if [ -z "$version" ]; then
echo "Usage: $0 --download <version>"
exit 1
fi
# check if version is available for download
if ! getRemoteVersions | grep -q "^$version\$"; then
echo "Version $version not found for download."
exit 1
fi
# check if version is already downloaded
if [ -f "$DOWNLOADS_DIR/cursor-$version.AppImage" ]; then
echo "Version $version already downloaded."
else
downloadVersion "$version"
fi
echo "To select the downloaded version, run \`cvm --use $version\`"
;;
--active)
getActiveVersion
;;
--use)
version=$2
if [ -z "$version" ]; then
echo "Usage: $0 --use <version>"
exit 1
fi
exitIfVersionNotInstalled "$version"
selectVersion "$version"
;;
--remove)
version=$2c
if [ -z "$version" ]; then
echo "Usage: $0 --remove <version>"
exit 1
fi
exitIfVersionNotInstalled "$version"
activeVersion=$(getActiveVersion)
if [ "$activeVersion" = "$version" ]; then
rm "$CURSOR_DIR/active"
fi
rm "$DOWNLOADS_DIR/cursor-$version.AppImage"
;;
--install)
installCVM
;;
--uninstall)
uninstallCVM
;;
--update-script)
updateScript
;;
*)
echo "Unknown command: $1"
help
exit 1
;;
esac