2 # shellcheck disable=SC2034
4 # for mod in $(nix eval --raw '(
5 # with import <nixpkgs> { };
7 # let mods = attrNames (removeAttrs openraPackages.mods [ "recurseForDerivations" ]);
8 # in concatStringsSep " " mods
10 # ./mod-update.sh "$mod"
13 # Uses: https://github.com/msteen/nix-upfetch
27 command curl
--silent --show-error "$@"
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}"
50 if [[ -n $commit_count ]]; then
70 # shellcheck disable=SC2089
72 repository(owner: "'$mod_owner'", name: "'$mod_repo'") {
75 ... on Commit '$query_on_commit'
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
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),
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}"
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
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
150 for type in mod engine
; do
152 for name
in owner repo
rev; do
153 var
="${type}_${name}"
154 fetcher_args
+=( "--$name" "${!var}" )
156 var
="${type}_version"
158 nix-upfetch
--yes --version "$version" "$(nix-preupfetch --file "$nixpkgs" "openraPackages.mods.
$mod" --index $i -- "${fetcher_args[@]}")"