pkgs/den-http-get-updater: support list to prefetch

This commit is contained in:
Wroclaw 2025-04-07 18:37:34 +02:00
parent b188a5239c
commit 5cedeec18c
2 changed files with 90 additions and 43 deletions

View file

@ -11,10 +11,20 @@
{
# location of file to modify
fileLocation,
previousHash,
previousVersion,
versionUrl,
prefetchUrlLocation ? null,
# {
# fileLocation: string?;
# previousHash: string;
# prefetchUrlLocation: {
# file: string;
# attrpath: string[]'
# };
# }[]
#
prefetchList ? [],
# change newVersion variable in it, if the contents of the page
# is not plaintext version
# (json for example)
@ -24,23 +34,30 @@
name ? if unpack then "source" else null,
}:
assert builtins.isNull prefetchUrlLocation || lib.isAttrs prefetchUrlLocation;
assert lib.isAttrs prefetchUrlLocation && (
lib.isString prefetchUrlLocation.file or null ||
lib.isPath prefetchUrlLocation.file or null
);
assert lib.isAttrs prefetchUrlLocation && lib.isString prefetchUrlLocation.attrpath or null;
let
realFileLocation = builtins.toString fileLocation;
mark = builtins.hashString "sha256" previousHash;
mark' = lib.escapeShellArg mark;
prefetchUrlLocation' = lib.mapAttrs (_: lib.escapeShellArg) prefetchUrlLocation;
prefetchList' = lib.map (x:
assert builtins.isNull x.prefetchUrlLocation || lib.isAttrs x.prefetchUrlLocation;
assert lib.isAttrs x.prefetchUrlLocation && (
lib.isString x.prefetchUrlLocation.file or null ||
lib.isPath x.prefetchUrlLocation.file or null
);
assert lib.isAttrs x.prefetchUrlLocation && lib.isString x.prefetchUrlLocation.attrpath or null;
rec {
inherit fileLocation;
mark = builtins.hashString "sha256" x.previousHash;
markShellEscape = lib.escapeShellArg mark;
markShellRegexEscape = lib.escapeShellArg (lib.escapeRegex mark);
realFileLocation = builtins.toString x.fileLocation or fileLocation;
realFileLocationShellEscape = lib.escapeShellArg realFileLocation;
prefetchUrlLocationShellEscape = lib.mapAttrs (_: lib.escapeShellArg) x.prefetchUrlLocation;
previousHashShellRegexEscape = lib.escapeShellArg (lib.escapeRegex x.previousHash);
} // x) prefetchList;
realFileLocation' = lib.escapeShellArg realFileLocation;
versionUrl' = lib.escapeShellArg versionUrl;
mark'' = lib.escapeShellArg (lib.escapeRegex mark);
previousVersion'' = lib.escapeShellArg (lib.escapeRegex previousVersion);
nixUnpack = lib.optionalString unpack "--unpack";
@ -56,6 +73,7 @@ in
writeScript "den-http-get-updater" (''
PATH="${lib.escapeShellArg path}"
prefetchFailed=
newVersion=$(curl -L "${versionUrl'}")
if [[ "$?" != 0 ]]; then
@ -63,15 +81,30 @@ writeScript "den-http-get-updater" (''
exit 1
fi
newVersion=$(${contentParser})
awk -i inplace "{
sub(/${previousVersion''}/, \"$newVersion\")
# invalidate hash
sub(/${previousHash}/, \"${mark'}\")
}1" "${realFileLocation'}"
'' + lib.optionalString (!builtins.isNull prefetchUrlLocation) ''
awk -i inplace "{ sub(/${previousVersion''}/, \"$newVersion\") }1" "${realFileLocation'}"
''
# invalidate hashes
+ lib.concatStringsSep "\n" (lib.map ({
markShellEscape,
previousHash,
previousHashShellRegexEscape,
realFileLocationShellEscape,
...
}: ''
awk -i inplace "{ sub(/${previousHashShellRegexEscape}/, \"${markShellEscape}\") }1" "${realFileLocationShellEscape}"
'') prefetchList')
+ lib.concatStringsSep "\n" (lib.map ({
fileLocation,
markShellRegexEscape,
prefetchUrlLocationShellEscape,
realFileLocationShellEscape,
...
}: ''
nixUrlsResult=$(nix-instantiate --eval --json \
"${prefetchUrlLocation'.file}" \
-A "${prefetchUrlLocation'.attrpath}"
"${prefetchUrlLocationShellEscape.file}" \
-A "${prefetchUrlLocationShellEscape.attrpath}"
)
urlsType=$(jq -rc 'type' <<< "$nixUrlsResult")
@ -93,14 +126,20 @@ writeScript "den-http-get-updater" (''
echo "prefetch succeeded!"
echo "hash: $expectedHash"
awk -i inplace "{
sub(/${mark''}/, \"$expectedHash\")
}1" "${realFileLocation'}"
sub(/${markShellRegexEscape}/, \"$expectedHash\")
}1" "${realFileLocationShellEscape}"
prefetchSucceeded=
break
fi
done
if [[ -n "$prefetchSucceeded" ]]; then
echo "warning: prefetch failed" 1>&2
prefetchFailed=1
fi
'') (lib.filter (x: !builtins.isNull x.prefetchUrlLocation) prefetchList'))
+ ''
if [[ -n "$prefetchFailed" ]]; then
exit 1
fi
'')