-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·168 lines (136 loc) · 4.25 KB
/
Copy pathupdate.sh
File metadata and controls
executable file
·168 lines (136 loc) · 4.25 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
#!/usr/bin/env bash
# update.sh - Check for new VS Code versions and optionally update hashes
# Usage: ./update.sh - Check if update needed
# ./update.sh --update - Fetch hashes and update version.json
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VERSION_FILE="${SCRIPT_DIR}/version.json"
# Platform mappings: nix system -> VS Code download platform
declare -A PLATFORM_MAP=(
["x86_64-linux"]="linux-x64"
["aarch64-linux"]="linux-arm64"
["x86_64-darwin"]="darwin"
["aarch64-darwin"]="darwin-arm64"
)
# Get current version from version.json
get_current_version() {
if [[ ! -f "$VERSION_FILE" ]]; then
echo "Error: version.json not found at $VERSION_FILE" >&2
exit 1
fi
jq -r '.version' "$VERSION_FILE"
}
# Get latest VS Code version from GitHub API
# Uses /releases endpoint to find newest non-prerelease, non-draft release
# (doesn't rely on Microsoft marking a release as "latest")
get_latest_version() {
local tag_name
# Get all releases, filter to non-prerelease/non-draft, take first (most recent)
tag_name=$(curl -s "https://api.github.com/repos/microsoft/vscode/releases" | \
jq -r '[.[] | select(.prerelease == false and .draft == false)][0].tag_name')
if [[ -z "$tag_name" || "$tag_name" == "null" ]]; then
echo "Error: Could not get latest version from GitHub API" >&2
exit 1
fi
# Strip leading 'v' if present
echo "${tag_name#v}"
}
# Compare versions (returns 0 if v1 < v2, 1 otherwise)
version_lt() {
local v1="$1"
local v2="$2"
# If versions are equal, v1 is not less than v2
if [[ "$v1" == "$v2" ]]; then
return 1
fi
# Use sort -V for version comparison
local sorted
sorted=$(printf '%s\n%s' "$v1" "$v2" | sort -V | head -n1)
if [[ "$sorted" == "$v1" ]]; then
return 0 # v1 < v2
else
return 1 # v1 >= v2
fi
}
# Get download URL for a platform
get_download_url() {
local version="$1"
local platform="$2"
echo "https://update.code.visualstudio.com/${version}/${platform}/stable"
}
# Fetch hash for a platform using nix-prefetch-url
fetch_hash() {
local version="$1"
local nix_platform="$2"
local vscode_platform="${PLATFORM_MAP[$nix_platform]}"
local url
url=$(get_download_url "$version" "$vscode_platform")
echo "Fetching hash for $nix_platform ($vscode_platform)..." >&2
# Use nix-prefetch-url to download and hash
local base32_hash
base32_hash=$(nix-prefetch-url --type sha256 "$url" 2>/dev/null)
if [[ -z "$base32_hash" ]]; then
echo "Error: Failed to fetch hash for $nix_platform" >&2
return 1
fi
# Convert to SRI format
local sri_hash
sri_hash=$(nix-hash --type sha256 --to-sri "$base32_hash")
echo "$sri_hash"
}
# Fetch all hashes and update version.json
update_version_json() {
local new_version="$1"
echo "Fetching hashes for VS Code $new_version..." >&2
local hash_x86_64_linux hash_aarch64_linux hash_x86_64_darwin hash_aarch64_darwin
hash_x86_64_linux=$(fetch_hash "$new_version" "x86_64-linux")
hash_aarch64_linux=$(fetch_hash "$new_version" "aarch64-linux")
hash_x86_64_darwin=$(fetch_hash "$new_version" "x86_64-darwin")
hash_aarch64_darwin=$(fetch_hash "$new_version" "aarch64-darwin")
# Create new version.json
cat > "$VERSION_FILE" << EOF
{
"version": "$new_version",
"hashes": {
"x86_64-linux": "$hash_x86_64_linux",
"aarch64-linux": "$hash_aarch64_linux",
"x86_64-darwin": "$hash_x86_64_darwin",
"aarch64-darwin": "$hash_aarch64_darwin"
}
}
EOF
echo "Updated version.json to version $new_version" >&2
}
main() {
local do_update=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--update)
do_update=true
shift
;;
*)
echo "Unknown option: $1" >&2
echo "Usage: $0 [--update]" >&2
exit 1
;;
esac
done
local current_version
local latest_version
current_version=$(get_current_version)
latest_version=$(get_latest_version)
echo "Current version: $current_version" >&2
echo "Latest version: $latest_version" >&2
if version_lt "$current_version" "$latest_version"; then
echo "UPDATE_NEEDED=true"
echo "NEW_VERSION=$latest_version"
if $do_update; then
update_version_json "$latest_version"
fi
else
echo "UPDATE_NEEDED=false"
fi
}
main "$@"