Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / games / openra_2019 / mod-update.sh
blob8489d5a97a7cc46cad2df614b15850ec23328a38
1 #!/usr/bin/env bash
2 # shellcheck disable=SC2034
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
13 # Uses: https://github.com/msteen/nix-upfetch
15 mod=$1
16 commit_count=$2
17 token=
18 nixpkgs='<nixpkgs>'
20 die() {
21 local ret=$?
22 echo "$*" >&2
23 exit $ret
26 curl() {
27 command curl --silent --show-error "$@"
30 get_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"
36 [[ -n $mod ]] || die "The first argument of this script has to be a mod identifier."
38 [[ -n $token ]] || die "Please edit this script to include a GitHub API access token, which is required for API v4:
39 https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/"
41 # Get current mod_owner and mod_repo.
42 vars=$(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
45 mod_owner=; mod_repo=; mod_rev=
46 while IFS='=' read -r key val; do
47 declare "mod_${key}=${val}"
48 done <<< "$vars"
50 if [[ -n $commit_count ]]; then
51 query_on_commit='{
52 history(first: 10) {
53 nodes {
54 abbreviatedOid
55 oid
57 totalCount
60 else
61 query_on_commit='{
62 history(first: 0) {
63 totalCount
65 abbreviatedOid
66 oid
70 # shellcheck disable=SC2089
71 query='{
72 repository(owner: "'$mod_owner'", name: "'$mod_repo'") {
73 defaultBranchRef {
74 target {
75 ... on Commit '$query_on_commit'
78 licenseInfo {
79 key
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
87 query=$(echo $query)
88 query=${query//\"/\\\"}
90 # https://developer.github.com/v4/guides/using-the-explorer/#configuring-graphiql
91 json=$(curl -H "Authorization: bearer $token" -X POST -d '{ "query": "'"$query"'" }' https://api.github.com/graphql) || exit
93 if [[ -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
100 vars=$(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
107 mod_license_key=; mod_version=; mod_rev=
108 while IFS='=' read -r key val; do
109 declare "mod_${key}=${val}"
110 done <<< "$vars"
112 mod_config=$(curl "https://raw.githubusercontent.com/$mod_owner/$mod_repo/$mod_rev/mod.config") || exit
114 mod_id=; engine_version=; automatic_engine_management=; automatic_engine_source=
115 while IFS='=' read -r key val; do
116 declare "${key,,}=$(jq --raw-output . <<< "$val")"
117 done < <(grep '^\(MOD_ID\|ENGINE_VERSION\|AUTOMATIC_ENGINE_MANAGEMENT\|AUTOMATIC_ENGINE_SOURCE\)=' <<< "$mod_config")
119 for var in mod_id engine_version automatic_engine_management automatic_engine_source; do
120 echo "$var=${!var}" >&2
121 done
122 echo >&2
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."
135 engine_owner=${BASH_REMATCH[1]}
136 engine_repo=${BASH_REMATCH[2]}
137 # shellcheck disable=SC2016
138 [[ ${BASH_REMATCH[3]} == '${ENGINE_VERSION}' ]] || engine_version=${BASH_REMATCH[3]}
139 engine_rev=$(get_sha1 "$engine_owner" "$engine_repo" "$engine_version")
141 for 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
147 done
150 for 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++ ))
160 done