pytrainer: unpin python 3.10
[NixPkgs.git] / pkgs / applications / editors / android-studio / update.sh
blob04efbb36675406fb236562887f09a8caeb0b8abe
1 #! /usr/bin/env nix-shell
2 #! nix-shell -I nixpkgs=./. -i bash -p jq nix-prefetch-scripts
4 set -euo pipefail
6 DEFAULT_NIX="$(realpath "./pkgs/applications/editors/android-studio/default.nix")"
7 RELEASES_JSON="$(curl --silent -L https://jb.gg/android-studio-releases-list.json)"
9 # Available channels: Release/Patch (stable), Beta, Canary
10 getLatest() {
11 local attribute="$1"
12 local channel="$2"
13 case "$channel" in
14 "stable") local select='.channel == "Release" or .channel == "Patch"' ;;
15 "beta") local select='.channel == "Beta" or .channel == "RC"' ;;
16 *) local select=".channel == \"${channel^}\"" ;;
17 esac
18 local result="$(echo "$RELEASES_JSON" \
19 | jq -r ".content.item[] | select(${select}) | [.version, .${attribute}] | join(\" \")" \
20 | sort --version-sort \
21 | cut -d' ' -f 2- \
22 | tail -n 1)"
24 if [[ -n "$result" ]]; then
25 echo "$result"
26 else
27 echo "could not find the latest $attribute for $channel"
28 exit 1
32 updateChannel() {
33 local channel="$1"
34 local latestVersion="$(getLatest "version" "$channel")"
36 local localVersion="$(nix --extra-experimental-features nix-command eval --raw --file . androidStudioPackages."${channel}".version)"
37 if [[ "${latestVersion}" == "${localVersion}" ]]; then
38 echo "$channel is already up to date at $latestVersion"
39 return 0
41 echo "updating $channel from $localVersion to $latestVersion"
43 local latestHash="$(nix-prefetch-url "https://dl.google.com/dl/android/studio/ide-zips/${latestVersion}/android-studio-${latestVersion}-linux.tar.gz")"
44 local latestSri="$(nix --extra-experimental-features nix-command hash to-sri --type sha256 "$latestHash")"
45 local localHash="$(nix --extra-experimental-features nix-command eval --raw --file . androidStudioPackages."${channel}".unwrapped.src.drvAttrs.outputHash)"
46 sed -i "s~${localHash}~${latestSri}~g" "${DEFAULT_NIX}"
48 # Match the formatting of default.nix: `version = "2021.3.1.14"; # "Android Studio Dolphin (2021.3.1) Beta 5"`
49 local versionString="${latestVersion}\"; # \"$(getLatest "name" "${channel}")\""
50 sed -i "s~${localVersion}.*~${versionString}~g" "${DEFAULT_NIX}"
51 echo "updated ${channel} to ${latestVersion}"
54 if (( $# == 0 )); then
55 for channel in "beta" "canary" "stable"; do
56 updateChannel "$channel"
57 done
58 else
59 while (( "$#" )); do
60 case "$1" in
61 beta|canary|stable)
62 updateChannel "$1" ;;
64 echo "unknown channel: $1" && exit 1 ;;
65 esac
66 shift 1
67 done