From dca343daed37d76bdcb7b19bc377b4aa4b6519c9 Mon Sep 17 00:00:00 2001 From: Colin Date: Sat, 22 Mar 2025 09:07:14 +0000 Subject: [PATCH] firefox-extensions.addon-version-lister: port from sh to oil --- .../firefox-extensions/addon-version-lister | 108 ++++++++++++------ pkgs/by-name/firefox-extensions/package.nix | 4 +- 2 files changed, 74 insertions(+), 38 deletions(-) diff --git a/pkgs/by-name/firefox-extensions/addon-version-lister b/pkgs/by-name/firefox-extensions/addon-version-lister index d7d91dac2..950b01a50 100755 --- a/pkgs/by-name/firefox-extensions/addon-version-lister +++ b/pkgs/by-name/firefox-extensions/addon-version-lister @@ -1,53 +1,89 @@ #!/usr/bin/env nix-shell -#!nix-shell -i bash -p bash -p common-updater-scripts -p coreutils -p curl +#!nix-shell -i ysh -p common-updater-scripts -p coreutils -p curl -p oils-for-unix -NEWLINE=$'\n' +set -e -numeric_version() { +source $LIB_YSH/args.ysh + +parser (&spec) { + flag "" --max-versions (Int, default=10, help=''' + only show up to this many of the most recent valid versions. + the lower this value, the faster this script will complete. + many use cases can set this to `1`. + ''') + flag -v --verbose (Bool, default=false, help="more logging") + flag -vv --really-verbose (Bool, default=false, help="more logging") + flag "" --old-version (Str, help=''' + version which yielded the provided url, used to recognize how the URL needs to be updated per version + ''') + arg url (help=''' + URL at which to expect a .xpi file. + combined with `--old-version`, this is treated as a template for discovering newer versions. + ''') +} + +func numeric_version (tag) { # TODO: consider `b` or `rc` suffixes? - echo "$1" | egrep --only-matching "[0-9.]+" | head -n1 + return ($(echo "$tag" | egrep --only-matching "[0-9.]+" | head -n1)) } -sort_versions() { - local lines= - for v in $1; do - local n=$(numeric_version "$v") - lines="$lines$n $v$NEWLINE" - done - echo "$lines" | sort --version-sort --reverse | cut -d" " -f 2 +func sort_versions (versions) { + var numerics = [] + for v in (versions) { + var row = [numeric_version(v), v] => join(" ") + call numerics->append (row) + } + var lines = numerics => join($'\n') + var sorted = $(echo "$lines" | sort --version-sort --reverse | cut -d" " -f 2) + return (sorted => split ($'\n')) } -set -ex -url="$1" +proc debug (;...stmt) { + if (VERBOSE) { + # TODO: pretty-print instead of assuming every stmt is a Str + var joined = stmt => join(" ") + echo "[DEBUG] $joined" >&2 + } +} -MAX_VERSIONS=${MAX_VERSIONS:-10} +var CURL_FLAGS = [] -# check that caller supplied all arguments -test -n "$url" -test -n "$UPDATE_NIX_OLD_VERSION" +var args = parseArgs(spec, ARGV) + +var MAX_VERSIONS = args['max-versions'] +var OLD_VERSION = args['old-version'] +var URL = args.url +var VERBOSE = args.verbose or args['really-verbose'] + +if (args['really-verbose']) { + call CURL_FLAGS->append("--verbose") +} # we need the bare git URL. # strip `https://github.com/OWNER/NAME/releases/download/...` -> `https://github.com/OWNER/NAME` -repo_url="${url/\/releases*/}" +var repo_url = URL.replace(/ '/releases/' .* $ /, "") +debug ("extracted", repo_url) -set +x -all_versions=$(list-git-tags --url="$repo_url") -all_versions=$(sort_versions "$all_versions") +var all_tags = $(list-git-tags --url="$repo_url") => split($'\n') +debug ("extracted tags", ...all_tags) +setvar all_tags = sort_versions (all_tags) +debug ("sorted tags", ...all_tags) -# filter to the versions for which we can actually download an artifact. -# some packages (uBlock) publish releases even before all artifacts are available. -versions=() -for v in $all_versions; do - test_url=''${url//$UPDATE_NIX_OLD_VERSION/$v} - if $(set -x; curl --fail "$test_url"); then - versions+=("$v") - fi +# # filter to the versions for which we can actually download an artifact. +# # some packages (uBlock) publish releases even before all artifacts are available. +var online_versions = [] - if [[ ${#versions[@]} -ge $MAX_VERSIONS ]]; then - # if we have a bunch of versions already, then exit now instead of crawling through hundreds more tags we're just going to throw away. - break - fi -done +for v in (all_tags) { + var url_to_test = URL => replace (OLD_VERSION, v); + debug ("testing url:", url_to_test) + if curl @CURL_FLAGS --fail "$url_to_test" { + echo "found online tag: $v" + call online_versions -> append (v) + if (len(online_versions) >= MAX_VERSIONS) { + break + } + } +} -set -x -echo "${versions[*]}" +var pretty_versions = online_versions => join(" ") +echo "$pretty_versions" diff --git a/pkgs/by-name/firefox-extensions/package.nix b/pkgs/by-name/firefox-extensions/package.nix index 6ebe44023..fc9c18bea 100644 --- a/pkgs/by-name/firefox-extensions/package.nix +++ b/pkgs/by-name/firefox-extensions/package.nix @@ -17,7 +17,7 @@ zip, }: let - addon-version-lister = static-nix-shell.mkBash { + addon-version-lister = static-nix-shell.mkYsh { pname = "addon-version-lister"; pkgs = [ "common-updater-scripts" "coreutils" "curl" ]; srcRoot = ./.; @@ -118,7 +118,7 @@ let passthru.updateScript = genericUpdater { versionLister = writeShellScript "${pname}-version-lister" '' - ${lib.getExe addon-version-lister} ${url} + ${lib.getExe addon-version-lister} --verbose --old-version "$UPDATE_NIX_OLD_VERSION" ${url} ''; }; passthru.extid = extid;