about summary refs log tree commit diff
path: root/nixpkgs/pkgs/by-name/wa/warp-terminal/update.sh
blob: d60e0f1f5aa902b6ae0747846fe583dfa2d2e7bc (plain) (blame)
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
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl jq moreutils nix-prefetch
#shellcheck shell=bash
set -eu -o pipefail

dirname="$(dirname "$0")"

err() {
    echo "$*" >&2
}

json_get() {
    jq -r "$1" < "$dirname/versions.json"
}

json_set() {
    jq --arg x "$2" "$1 = \$x" < "$dirname/versions.json" | sponge "$dirname/versions.json"
}

resolve_url() {
    local pkg sfx url
    local -i i max_redirects
    case "$1" in
        darwin)
            pkg=macos
            sfx=dmg
            ;;
        linux)
            pkg=pacman
            sfx=pkg.tar.zst
            ;;
        *)
            err "Unexpected download type: $1"
            exit 1
            ;;
    esac
    url="https://app.warp.dev/download?package=${pkg}"
    ((max_redirects = 15))
    for ((i = 0; i < max_redirects; i++)); do
        url=$(curl -s -o /dev/null -w '%{redirect_url}' "${url}")
        [[ ${url} != *.${sfx} ]] || break
    done
    ((i < max_redirects)) || { err "too many redirects"; exit 1; }
    echo "${url}"
}

get_version() {
    echo "$1" | grep -oP -m 1 '(?<=/v)[\d.\w]+(?=/)'
}

for sys in darwin linux; do
    url=$(resolve_url ${sys})
    version=$(get_version "${url}")
    if [[ ${version} != "$(json_get ".${sys}.version")" ]];
        then
            sri=$(nix hash to-sri --type sha256 "$(nix-prefetch-url --type sha256 "${url}")")
            json_set ".${sys}.version" "${version}"
            json_set ".${sys}.hash" "${sri}"
    fi
done