1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p curl jq
3
4set -euo pipefail
5
6# so if the script fails, debug logs are on stderr
7log() {
8 >&2 echo "DART_UPDATER: $@"
9}
10
11# fetch the latest version number from upstream
12NEW_VER_DETAILS=$(curl -sL https://storage.googleapis.com/dart-archive/channels/stable/release/latest/VERSION)
13NEW_VER=$(jq -r '.version' <<< "$NEW_VER_DETAILS")
14
15MY_PATH=$(dirname $(realpath "$0"))
16SRC_FILE=$(mktemp)
17
18log "file to write is $SRC_FILE"
19
20PRELUDE="let
21 version = \"$NEW_VER\";
22in
23{ fetchurl }:
24{
25 versionUsed = version;"
26echo "$PRELUDE" > "$SRC_FILE"
27log "wrote prelude"
28
29# Fetches the source, then writes the fetcher and hash into the sources file.
30# Arguments:
31# - $1: VARIABLE NAME of (table of nix platform -> dart platform mappings) ("DARWIN_PLATFORMS"|"LIN_PLATFORMS")
32# - $2: Dart-OS ("macos"|"linux")
33write_for_platform() {
34 BASE_OF_ALL_URLS='https://storage.googleapis.com/dart-archive/channels/stable/release'
35 BASE_URL_WRITTEN="$BASE_OF_ALL_URLS/\${version}/sdk"
36 BASE_URL_FETCHED="$BASE_OF_ALL_URLS/$NEW_VER/sdk"
37
38 TABLE_NAME=$1
39 declare -n TABLE=$TABLE_NAME
40
41 for platform in "${!TABLE[@]}"; do
42 DART_PLATFORM="${TABLE[$platform]}"
43 log "trying for dartplatform $DART_PLATFORM (platform $platform) (OS $2)"
44
45 URL_POSTFIX="dartsdk-$2-$DART_PLATFORM-release.zip"
46 URL="$BASE_URL_FETCHED/$URL_POSTFIX"
47 log "URL for $DART_PLATFORM: $URL"
48
49 HASH=$(nix-prefetch-url "$URL" --type sha256)
50 log "hash for platform $platform: $HASH"
51
52 FETCHER=" \"\${version}-$platform\" = fetchurl {
53 url = \"$BASE_URL_WRITTEN/$URL_POSTFIX\";
54 sha256 = \"$HASH\";
55 };"
56
57 echo "$FETCHER" >> $SRC_FILE
58 done
59 log "finished for $1"
60
61}
62
63# Map nix platforms -> Dart platforms
64X8664="x64"
65AARCH64="arm64"
66I686="ia32"
67declare -A DARWIN_PLATFORMS=(["aarch64-darwin"]="$AARCH64"
68 ["x86_64-darwin"]="$X8664")
69
70declare -A LIN_PLATFORMS=( ["x86_64-linux"]="$X8664"
71 ["i686-linux"]="$I686"
72 ["aarch64-linux"]="$AARCH64")
73
74write_for_platform "DARWIN_PLATFORMS" "macos"
75write_for_platform "LIN_PLATFORMS" "linux"
76
77echo '}' >> $SRC_FILE
78
79log "moving tempfile to target directory"
80mv "$SRC_FILE" "$MY_PATH/sources.nix"