Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at devShellTools-shell 160 lines 5.0 kB view raw
1#!/usr/bin/env bash 2# shellcheck disable=SC2034 3 4# for mod in $(nix eval --raw '( 5# with import <nixpkgs> { }; 6# with lib; 7# let mods = attrNames (removeAttrs openraPackages.mods [ "recurseForDerivations" ]); 8# in concatStringsSep " " mods 9# )'); do 10# ./mod-update.sh "$mod" 11# done 12 13# Uses: https://github.com/msteen/nix-upfetch 14 15mod=$1 16commit_count=$2 17token= 18nixpkgs='<nixpkgs>' 19 20die() { 21 local ret=$? 22 echo "$*" >&2 23 exit $ret 24} 25 26curl() { 27 command curl --silent --show-error "$@" 28} 29 30get_sha1() { 31 local owner=$1 repo=$2 ref=$3 32 # https://developer.github.com/v3/#authentication 33 curl -H "Authorization: token $token" -H 'Accept: application/vnd.github.VERSION.sha' "https://api.github.com/repos/$owner/$repo/commits/$ref" 34} 35 36[[ -n $mod ]] || die "The first argument of this script has to be a mod identifier." 37 38[[ -n $token ]] || die "Please edit this script to include a GitHub API access token, which is required for API v4: 39https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/" 40 41# Get current mod_owner and mod_repo. 42vars=$(nix-prefetch --file "$nixpkgs" "openraPackages.mods.$mod" --index 0 --quiet --output json --no-compute-hash > >( 43 jq --raw-output 'with_entries(select(.value | contains("\n") | not)) | to_entries | .[] | .key + "=" + .value')) || exit 44 45mod_owner=; mod_repo=; mod_rev= 46while IFS='=' read -r key val; do 47 declare "mod_${key}=${val}" 48done <<< "$vars" 49 50if [[ -n $commit_count ]]; then 51 query_on_commit='{ 52 history(first: 10) { 53 nodes { 54 abbreviatedOid 55 oid 56 } 57 totalCount 58 } 59}' 60else 61 query_on_commit='{ 62 history(first: 0) { 63 totalCount 64 } 65 abbreviatedOid 66 oid 67}' 68fi 69 70# shellcheck disable=SC2089 71query='{ 72 repository(owner: "'$mod_owner'", name: "'$mod_repo'") { 73 defaultBranchRef { 74 target { 75 ... on Commit '$query_on_commit' 76 } 77 } 78 licenseInfo { 79 key 80 } 81 } 82}' 83 84# Newlines are not allowed in a query. 85# https://developer.github.com/v4/guides/forming-calls/#communicating-with-graphql 86# shellcheck disable=SC2086 disable=SC2090 disable=SC2116 87query=$(echo $query) 88query=${query//\"/\\\"} 89 90# https://developer.github.com/v4/guides/using-the-explorer/#configuring-graphiql 91json=$(curl -H "Authorization: bearer $token" -X POST -d '{ "query": "'"$query"'" }' https://api.github.com/graphql) || exit 92 93if [[ -n $commit_count ]]; then 94 json=$(jq "$commit_count"' as $commit_count 95 | .data.repository.defaultBranchRef.target 96 |= (.history |= (. | del(.nodes) | .totalCount = $commit_count)) 97 + (.history | .nodes[.totalCount - $commit_count])' <<< "$json") || exit 98fi 99 100vars=$(jq --raw-output '.data.repository | { 101 license_key: .licenseInfo.key, 102} + (.defaultBranchRef.target | { 103 version: ((.history.totalCount | tostring) + ".git." + .abbreviatedOid), 104 rev: .oid, 105}) | to_entries | .[] | .key + "=" + (.value | tostring)' <<< "$json") || exit 106 107mod_license_key=; mod_version=; mod_rev= 108while IFS='=' read -r key val; do 109 declare "mod_${key}=${val}" 110done <<< "$vars" 111 112mod_config=$(curl "https://raw.githubusercontent.com/$mod_owner/$mod_repo/$mod_rev/mod.config") || exit 113 114mod_id=; engine_version=; automatic_engine_management=; automatic_engine_source= 115while IFS='=' read -r key val; do 116 declare "${key,,}=$(jq --raw-output . <<< "$val")" 117done < <(grep '^\(MOD_ID\|ENGINE_VERSION\|AUTOMATIC_ENGINE_MANAGEMENT\|AUTOMATIC_ENGINE_SOURCE\)=' <<< "$mod_config") 118 119for var in mod_id engine_version automatic_engine_management automatic_engine_source; do 120 echo "$var=${!var}" >&2 121done 122echo >&2 123 124[[ $mod_id == "$mod" ]] || 125 die "The mod '$mod' reports being mod '$mod_id' instead." 126# shellcheck disable=SC2005 disable=SC2046 127[[ $mod_license_key == gpl-3.0 ]] || 128[[ $(echo $(head -2 <(curl "https://raw.githubusercontent.com/$mod_owner/$mod_repo/$mod_rev/COPYING"))) == 'GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007' ]] || 129 die "The mod '$mod' is licensed under '$mod_license_key' while expecting 'gpl-3.0'." 130[[ $automatic_engine_management == True ]] || 131 die "The mod '$mod' engine is not managed as a read-only dependency." 132[[ $automatic_engine_source =~ https://github.com/([a-zA-Z0-9_\-]+)/([a-zA-Z0-9_\-]+)/archive/([a-zA-Z0-9_\-\$\{\}]+).zip ]] || 133 die "The mod '$mod' engine is not hosted on GitHub as an archive." 134 135engine_owner=${BASH_REMATCH[1]} 136engine_repo=${BASH_REMATCH[2]} 137# shellcheck disable=SC2016 138[[ ${BASH_REMATCH[3]} == '${ENGINE_VERSION}' ]] || engine_version=${BASH_REMATCH[3]} 139engine_rev=$(get_sha1 "$engine_owner" "$engine_repo" "$engine_version") 140 141for type in mod engine; do 142 for name in version owner repo rev; do 143 var="${type}_${name}" 144 echo "$var=${!var}" >&2 145 done 146 echo >&2 147done 148 149i=0 150for type in mod engine; do 151 fetcher_args=() 152 for name in owner repo rev; do 153 var="${type}_${name}" 154 fetcher_args+=( "--$name" "${!var}" ) 155 done 156 var="${type}_version" 157 version=${!var} 158 nix-upfetch --yes --version "$version" "$(nix-preupfetch --file "$nixpkgs" "openraPackages.mods.$mod" --index $i -- "${fetcher_args[@]}")" 159 (( i++ )) 160done