1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p curl wget jq yq common-updater-scripts prefetch-yarn-deps yarn-lock-converter
3
4set -eu -o pipefail
5
6TMPDIR=/tmp/pgadmin-update-script
7
8################################################################
9# This script will update pgadmin4 in nixpkgs #
10# Due to recent changes upstream, we will need to convert the #
11# `yarn.lock` file back to version 1. #
12# This isn't trivially done and relies on 3rd party tools #
13# and a hand-written converter (in this script). #
14# Also, the converter cannot check for `github` repos in the #
15# `yarn.lock` file, which this script will add automatically #
16################################################################
17
18cleanup() {
19 if [ -e $TMPDIR/.done ]
20 then
21 rm -rf "$TMPDIR"
22 else
23 echo
24 read -p "Script exited prematurely. Do you want to delete the temporary directory $TMPDIR ? " -n 1 -r
25 echo
26 if [[ $REPLY =~ ^[Yy]$ ]]
27 then
28 rm -rf "$TMPDIR"
29 fi
30 fi
31}
32
33trap cleanup EXIT
34
35scriptDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
36nixpkgs=$(realpath "$scriptDir"/../../../..)
37
38newest_version="$(curl -s https://www.pgadmin.org/versions.json | jq -r .pgadmin4.version)"
39old_version=$(nix-instantiate --eval -E "(import \"$nixpkgs\" { config = {}; overlays = []; }).pgadmin4.version" | tr -d '"')
40url="https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v${newest_version}/source/pgadmin4-${newest_version}.tar.gz"
41
42if [[ $newest_version == $old_version ]]; then
43 printf "Already at latest version $newest_version\n"
44 exit 0
45fi
46printf "New version: $newest_version \n"
47
48# don't use mktemp, so if a network error happens, we can resume from there
49mkdir -p $TMPDIR
50pushd $TMPDIR
51wget -c $url
52tar -xzf "pgadmin4-$newest_version.tar.gz"
53cd "pgadmin4-$newest_version/web"
54
55printf "Will now convert the v2 lockfile. This will download the npm packages to get the metadata.\n"
56printf "Please note: This will take some time! For details, see the logfile ${TMPDIR}/update.log\n"
57yarn-lock-converter -i yarn.lock -o yarn_v1.lock --cache .cache > $TMPDIR/update.log
58printf "Conversion done\n"
59
60printf "Will now do some regex substitution post-processing\n"
61sed -i -E "s|(.), |\1\", \"|g" yarn_v1.lock
62printf "Substituion done\n"
63
64printf "Will now add missing github packages back to the v1 yarn.lock file\n"
65# remove header
66tail +8 yarn.lock > yarn_mod.lock
67LENGTH=$(yq '. | with_entries(select(.value.resolution | contains("github"))) | keys | length' yarn_mod.lock)
68for i in $(seq 0 $(($LENGTH-1)));
69do
70 ENTRY=$(yq ". | with_entries(select(.value.resolution | contains(\"github\"))) | keys | .[$i]" yarn_mod.lock)
71 URL=$(echo $ENTRY | cut -d "@" -f 2)
72 VERSION=$(yq ".$ENTRY.version" yarn_mod.lock)
73 LENGTH_DEP=$(yq ".$ENTRY.dependencies | keys | length" yarn_mod.lock)
74 echo "$ENTRY:" >> adendum.lock
75 echo " version $VERSION" >> adendum.lock
76 echo " resolved \"$URL" >> adendum.lock
77 echo " dependencies:" >> adendum.lock
78
79 for j in $(seq 0 $(($LENGTH_DEP-1)));
80 do
81 DEPENDENCY_KEY=$(yq ".$ENTRY.dependencies | keys | .[$j]" yarn_mod.lock)
82 DEPENDENCY_VALUE=$(yq ".$ENTRY.dependencies.$DEPENDENCY_KEY" yarn_mod.lock)
83 # remove '"'
84 DEPENDENCY_KEY=${DEPENDENCY_KEY//\"}
85 echo " \"$DEPENDENCY_KEY\" $DEPENDENCY_VALUE" >> adendum.lock
86 done
87done
88
89echo "" >> yarn_v1.lock
90cat adendum.lock >> yarn_v1.lock
91printf "Done\n"
92
93rm yarn.lock
94mv yarn_v1.lock yarn.lock
95
96printf "Will now generate the hash. This will download the packages to the nix store and also take some time\n"
97YARN_HASH=$(prefetch-yarn-deps yarn.lock)
98YARN_HASH=$(nix hash to-sri --type sha256 "$YARN_HASH")
99printf "Done\n"
100
101printf "Copy files to nixpkgs\n"
102cp yarn.lock "$nixpkgs/pkgs/tools/admin/pgadmin/"
103printf "Done\n"
104popd
105
106sed -i -E -e "s#yarnSha256 = \".*\"#yarnSha256 = \"$YARN_HASH\"#" ${scriptDir}/default.nix
107
108update-source-version pgadmin4 "$newest_version" --print-changes
109touch $TMPDIR/.done