1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p curl gnugrep gnused jq nix-prefetch
3
4set -eu -o pipefail
5
6dirname=$(realpath "$(dirname "$0")")
7nixpkgs=$(realpath "${dirname}/../../../..")
8
9old_rover_version=$(nix eval --raw -f "$nixpkgs" rover.version)
10rover_url=https://api.github.com/repos/apollographql/rover/releases/latest
11rover_tag=$(curl "$rover_url" | jq --raw-output ".tag_name")
12rover_version="$(expr "$rover_tag" : 'v\(.*\)')"
13
14if [[ "$old_rover_version" == "$rover_version" ]]; then
15 echo "rover is up-to-date: ${old_rover_version}"
16 exit 0
17fi
18
19echo "Fetching rover"
20rover_tar_url="https://github.com/apollographql/rover/archive/refs/tags/${rover_tag}.tar.gz"
21{
22 read rover_hash
23 read repo
24} < <(nix-prefetch-url "$rover_tar_url" --unpack --type sha256 --print-path)
25
26# Convert hash to SRI representation
27rover_sri_hash=$(nix hash to-sri --type sha256 "$rover_hash")
28
29# Update rover version.
30sed --in-place \
31 "s|version = \"[0-9.]*\"|version = \"$rover_version\"|" \
32 "$dirname/default.nix"
33
34# Update rover hash.
35sed --in-place \
36 "s|sha256 = \"[a-zA-Z0-9\/+-=]*\"|sha256 = \"$rover_sri_hash\"|" \
37 "$dirname/default.nix"
38
39# Clear cargoSha256.
40sed --in-place \
41 "s|cargoSha256 = \".*\"|cargoSha256 = \"sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"|" \
42 "$dirname/default.nix"
43
44# Update cargoSha256
45echo "Computing cargoSha256"
46cargoSha256=$(
47 nix-prefetch "{ sha256 }: (import $nixpkgs {}).rover.cargoDeps.overrideAttrs (_: { outputHash = sha256; })"
48)
49sed --in-place \
50 "s|cargoSha256 = \".*\"|cargoSha256 = \"$cargoSha256\"|" \
51 "$dirname/default.nix"
52
53# Update apollo api schema info
54response="$(mktemp)"
55schemaUrl=https://graphql.api.apollographql.com/api/schema
56
57mkdir -p "$dirname"/schema
58
59# Fetch schema info
60echo "Fetching Apollo GraphQL schema"
61# include response headers, and append terminating newline to response body
62curl --include --write-out "\n" "$schemaUrl" > "$response"
63
64# Parse response headers and write the etag to schema/etag.id
65grep \
66 --max-count=1 \
67 --only-matching \
68 --perl-regexp \
69 '^etag: \K\S*' \
70 "$response" \
71 > "$dirname"/schema/etag.id
72
73# Discard headers and blank line (terminated by carriage return), and write the
74# response body to schema/schema.graphql
75sed '1,/^\r/d' "$response" > "$dirname"/schema/schema.graphql