1#! /usr/bin/env nix-shell
2#! nix-shell -i bash -p git mercurial common-updater-scripts
3set -eux -o pipefail
4
5cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
6root=../../../..
7tmp=$(mktemp -d)
8trap 'rm -rf "$tmp"' EXIT
9
10default() {
11 (cd "$root" && nix-instantiate --eval --strict -A "sourcehut.python.pkgs.$1.meta.position" | sed -re 's/^"(.*):[0-9]+"$/\1/')
12}
13
14version() {
15 (cd "$root" && nix-instantiate --eval --strict -A "sourcehut.python.pkgs.$1.version" | tr -d '"')
16}
17
18src_url() {
19 nix-instantiate --eval --strict --expr " with import $root {}; let src = sourcehut.python.pkgs.$1.drvAttrs.src; in src.meta.homepage" | tr -d '"'
20}
21
22get_latest_version() {
23 src="$(src_url "$1")"
24 rm -rf "$tmp"
25 if [ "$1" = "hgsrht" ]; then
26 hg clone "$src" "$tmp" >/dev/null
27 printf "%s" "$(cd "$tmp" && hg log --limit 1 --template '{latesttag}')"
28 else
29 git clone "$src" "$tmp" >/dev/null
30 printf "%s" "$(cd "$tmp" && git describe "$(git rev-list --tags --max-count=1)")"
31 fi
32}
33
34update_version() {
35 default_nix="$(default "$1")"
36 oldVersion="$(version "$1")"
37 version="$(get_latest_version "$1")"
38
39 (cd "$root" && update-source-version "sourcehut.python.pkgs.$1" "$version")
40
41 # Update vendorSha256 of Go modules
42 retry=true
43 while "$retry"; do
44 retry=false;
45 exec < <(exec nix -L build -f "$root" sourcehut.python.pkgs."$1" 2>&1)
46 while IFS=' :' read -r origin hash; do
47 case "$origin" in
48 (expected|specified) oldHash="$hash";;
49 (got) sed -i "s|$oldHash|$hash|" "$default_nix"; retry=true; break;;
50 (*) printf >&2 "%s\n" "$origin${hash:+:$hash}"
51 esac
52 done
53 done
54
55 if [ "$oldVersion" != "$version" ]; then
56 git add "$default_nix"
57 git commit -m "sourcehut.$1: $oldVersion -> $version"
58 fi
59}
60
61if [ $# -gt 0 ]; then
62 services=("$@")
63else
64 # Beware that some packages must be updated before others,
65 # eg. buildsrht must be updated before gitsrht,
66 # otherwise this script would enter an infinite loop
67 # because the reported $oldHash to be changed
68 # may not actually be in $default_nix
69 # but in the file of one of its dependencies.
70 services=( "srht" "scmsrht" "buildsrht" "gitsrht" "hgsrht" "hubsrht" "listssrht" "mansrht"
71 "metasrht" "pagessrht" "pastesrht" "todosrht" )
72fi
73
74for service in "${services[@]}"; do
75 update_version "$service"
76done