firefox-extensions.addon-version-lister: port from sh to oil

This commit is contained in:
2025-03-22 09:07:14 +00:00
parent 3d3bd5e5b2
commit dca343daed
2 changed files with 74 additions and 38 deletions

View File

@@ -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<n>` or `rc<n>` 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"

View File

@@ -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;