at 24.11-pre 110 lines 4.0 kB view raw
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 62sed -i -E "s|npm:||g" yarn_v1.lock 63printf "Substituion done\n" 64 65printf "Will now add missing github packages back to the v1 yarn.lock file\n" 66# remove header 67tail +8 yarn.lock > yarn_mod.lock 68LENGTH=$(yq '. | with_entries(select(.value.resolution | contains("github"))) | keys | length' yarn_mod.lock) 69for i in $(seq 0 $(($LENGTH-1))); 70do 71 ENTRY=$(yq ". | with_entries(select(.value.resolution | contains(\"github\"))) | keys | .[$i]" yarn_mod.lock) 72 URL=$(echo $ENTRY | cut -d "@" -f 2) 73 VERSION=$(yq ".$ENTRY.version" yarn_mod.lock) 74 LENGTH_DEP=$(yq ".$ENTRY.dependencies | keys | length" yarn_mod.lock) 75 echo "$ENTRY:" >> adendum.lock 76 echo " version $VERSION" >> adendum.lock 77 echo " resolved \"$URL" >> adendum.lock 78 echo " dependencies:" >> adendum.lock 79 80 for j in $(seq 0 $(($LENGTH_DEP-1))); 81 do 82 DEPENDENCY_KEY=$(yq ".$ENTRY.dependencies | keys | .[$j]" yarn_mod.lock) 83 DEPENDENCY_VALUE=$(yq ".$ENTRY.dependencies.$DEPENDENCY_KEY" yarn_mod.lock) 84 # remove '"' 85 DEPENDENCY_KEY=${DEPENDENCY_KEY//\"} 86 echo " \"$DEPENDENCY_KEY\" $DEPENDENCY_VALUE" >> adendum.lock 87 done 88done 89 90echo "" >> yarn_v1.lock 91cat adendum.lock >> yarn_v1.lock 92printf "Done\n" 93 94rm yarn.lock 95mv yarn_v1.lock yarn.lock 96 97printf "Will now generate the hash. This will download the packages to the nix store and also take some time\n" 98YARN_HASH=$(prefetch-yarn-deps yarn.lock) 99YARN_HASH=$(nix hash to-sri --type sha256 "$YARN_HASH") 100printf "Done\n" 101 102printf "Copy files to nixpkgs\n" 103cp yarn.lock "$nixpkgs/pkgs/tools/admin/pgadmin/" 104printf "Done\n" 105popd 106 107sed -i -E -e "s#yarnHash = \".*\"#yarnHash = \"$YARN_HASH\"#" ${scriptDir}/default.nix 108 109update-source-version pgadmin4 "$newest_version" --print-changes 110touch $TMPDIR/.done