aboutsummaryrefslogtreecommitdiffstats
path: root/nix-conf/pkgs/pacs/update.sh
blob: c017862f65b78e9b6f42108d6a6c81919e3d5db9 (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
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
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p coreutils curl gnugrep gnused jq nodejs_24 pnpm_10 nixfmt
#shellcheck shell=bash

set -eu -o pipefail

force=0
while getopts ":f" opt; do
    case "$opt" in
        f) force=1 ;;
        *) exit 2 ;;
    esac
done

dir="$(cd "$(dirname "$0")" && pwd)"
derivation="$dir/default.nix"
lockfile="$dir/pnpm-lock.yaml"
netrc="$HOME/.config/nix/netrc"

set_pnpm_deps_hash() {
    local newHash="$1"
    sed -i \
        '/^[[:space:]]*pnpmDeps[[:space:]]*=[[:space:]]*fetchPnpmDeps[[:space:]]*{/,/^[[:space:]]*};/ {
      s|^[[:space:]]*hash = "[^"]*";|    hash = "'"$newHash"'";|
    }' \
        "$derivation"
}

metadata="$(curl -sS --netrc-file "$netrc" "https://npm.pkg.github.com/@adzerk%2Fpacs-client")"
latestVersion="$(jq -r '.["dist-tags"].latest' <<<"$metadata")"
if [[ -z "$latestVersion" || "$latestVersion" == "null" ]]; then
    echo "error: couldn't find latest version from repository metadata" >&2
    exit 1
fi

currentVersion="$(sed -nE 's/^[[:space:]]*version = "([^"]+)".*/\1/p' "$derivation" | head -n1)"
if [[ "$latestVersion" == "$currentVersion" && "$force" -eq 0 ]]; then
    echo "pacs-client is already up-to-date: $currentVersion"
    exit 0
fi

tarballUrl="$(jq -r --arg v "$latestVersion" '.versions[$v].dist.tarball' <<<"$metadata")"
if [[ -z "$tarballUrl" || "$tarballUrl" == "null" ]]; then
    echo "error: couldn't find tarball URL for version $latestVersion" >&2
    exit 1
fi

downloadId="$(sed -n 's|.*/'"$latestVersion"'/\([^/?]*\).*|\1|p' <<<"$tarballUrl")"
narHash="$(nix store prefetch-file --json --unpack "$tarballUrl" | jq -r '.hash')"

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

curl -fsSL --netrc-file "$netrc" "$tarballUrl" -o "$tmp/pkg.tgz"
tar -xzf "$tmp/pkg.tgz" -C "$tmp"

if [[ ! -f "$tmp/package/package.json" ]]; then
    echo "error: package/package.json missing from tarball" >&2
    exit 1
fi

cd "$tmp/package"
export HOME="$tmp/home"
pnpm install --lockfile-only --prod
cp -f pnpm-lock.yaml "$lockfile"

echo "Updating $derivation ..."

sed -i \
    -e "s|^[[:space:]]*version = \"[^\"]*\";|  version = \"${latestVersion}\";|g" \
    -e "s|^[[:space:]]*downloadId = \"[^\"]*\";|  downloadId = \"${downloadId}\";|g" \
    -e "s|^[[:space:]]*narHash = \"[^\"]*\";|    narHash = \"${narHash}\";|g" \
    "$derivation"

# Force mismatch to get correct pnpmDeps.hash
set_pnpm_deps_hash "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="

set +e
out="$(cd "$dir/../.." && nix build --no-link .#additions.pacs.pnpmDeps 2>&1)"
status=$?
set -e

pnpmHash="$(grep -oE 'got:[[:space:]]*sha256-[A-Za-z0-9+/=]+' <<<"$out" | head -n1 | sed -E 's/got:[[:space:]]*//')"

if [[ -z "$pnpmHash" ]]; then
    echo "error: couldn't extract 'got: sha256-...' from nix build output (exit $status)" >&2
    echo "$out" | tail -n 120 >&2
    exit 1
fi

set_pnpm_deps_hash "$pnpmHash"

nixfmt "$derivation"

echo "Updated pacs-client: $currentVersion -> $latestVersion"