1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p curl jq common-updater-scripts
3
4set -eu -o pipefail
5
6cd "$(dirname "${BASH_SOURCE[0]}")"
7
8# The released tagged as "latest" is always stable.
9LATEST_STABLE_VERSION=$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} --fail -sSL https://api.github.com/repos/coder/coder/releases/latest | jq -r '.tag_name | sub("^v"; "")')
10# The highest version that is not a pre-release is the latest mainline version.
11LATEST_MAINLINE_VERSION=$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} --fail -sSL https://api.github.com/repos/coder/coder/releases | jq -r 'map(select(.prerelease == false)) | sort_by(.tag_name | sub("^v"; "") | split(".") | map(tonumber)) | .[-1].tag_name | sub("^v"; "")')
12
13# Define the platforms
14declare -A ARCHS=(["x86_64-linux"]="linux_amd64.tar.gz"
15 ["aarch64-linux"]="linux_arm64.tar.gz"
16 ["x86_64-darwin"]="darwin_amd64.zip"
17 ["aarch64-darwin"]="darwin_arm64.zip")
18
19update_version_and_hashes() {
20 local version=$1
21 local channel=$2
22
23 # Update version number, using '#' as delimiter
24 sed -i "/${channel} = {/,/};/{
25 s#^\(\s*\)version = .*#\1version = \"$version\";#
26 }" ./default.nix
27
28 # Update hashes for each architecture
29 for ARCH in "${!ARCHS[@]}"; do
30 local URL="https://github.com/coder/coder/releases/download/v${version}/coder_${version}_${ARCHS[$ARCH]}"
31 echo "Fetching hash for $channel/$ARCH..."
32
33 # Fetch the new hash using nix-prefetch-url
34 local NEW_HASH=$(nix-prefetch-url --type sha256 $URL)
35 local SRI_HASH=$(nix hash to-sri --type sha256 $NEW_HASH)
36
37 # Update the Nix file with the new hash, using '#' as delimiter and preserving indentation
38 sed -i "/${channel} = {/,/};/{
39 s#^\(\s*\)${ARCH} = .*#\1${ARCH} = \"${SRI_HASH}\";#
40 }" ./default.nix
41 done
42}
43
44# Update stable channel
45update_version_and_hashes $LATEST_STABLE_VERSION "stable"
46
47# Update mainline channel
48update_version_and_hashes $LATEST_MAINLINE_VERSION "mainline"