firefox-extensions.addon-version-lister: port from sh to oil
This commit is contained in:
@@ -1,53 +1,89 @@
|
|||||||
#!/usr/bin/env nix-shell
|
#!/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?
|
# 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() {
|
func sort_versions (versions) {
|
||||||
local lines=
|
var numerics = []
|
||||||
for v in $1; do
|
for v in (versions) {
|
||||||
local n=$(numeric_version "$v")
|
var row = [numeric_version(v), v] => join(" ")
|
||||||
lines="$lines$n $v$NEWLINE"
|
call numerics->append (row)
|
||||||
done
|
}
|
||||||
echo "$lines" | sort --version-sort --reverse | cut -d" " -f 2
|
var lines = numerics => join($'\n')
|
||||||
|
var sorted = $(echo "$lines" | sort --version-sort --reverse | cut -d" " -f 2)
|
||||||
|
return (sorted => split ($'\n'))
|
||||||
}
|
}
|
||||||
|
|
||||||
set -ex
|
proc debug (;...stmt) {
|
||||||
url="$1"
|
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
|
var args = parseArgs(spec, ARGV)
|
||||||
test -n "$url"
|
|
||||||
test -n "$UPDATE_NIX_OLD_VERSION"
|
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.
|
# we need the bare git URL.
|
||||||
# strip `https://github.com/OWNER/NAME/releases/download/...` -> `https://github.com/OWNER/NAME`
|
# 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
|
var all_tags = $(list-git-tags --url="$repo_url") => split($'\n')
|
||||||
all_versions=$(list-git-tags --url="$repo_url")
|
debug ("extracted tags", ...all_tags)
|
||||||
all_versions=$(sort_versions "$all_versions")
|
setvar all_tags = sort_versions (all_tags)
|
||||||
|
debug ("sorted tags", ...all_tags)
|
||||||
|
|
||||||
# filter to the versions for which we can actually download an artifact.
|
# # filter to the versions for which we can actually download an artifact.
|
||||||
# some packages (uBlock) publish releases even before all artifacts are available.
|
# # some packages (uBlock) publish releases even before all artifacts are available.
|
||||||
versions=()
|
var online_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
|
|
||||||
|
|
||||||
if [[ ${#versions[@]} -ge $MAX_VERSIONS ]]; then
|
for v in (all_tags) {
|
||||||
# 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.
|
var url_to_test = URL => replace (OLD_VERSION, v);
|
||||||
break
|
debug ("testing url:", url_to_test)
|
||||||
fi
|
if curl @CURL_FLAGS --fail "$url_to_test" {
|
||||||
done
|
echo "found online tag: $v"
|
||||||
|
call online_versions -> append (v)
|
||||||
|
if (len(online_versions) >= MAX_VERSIONS) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
set -x
|
var pretty_versions = online_versions => join(" ")
|
||||||
echo "${versions[*]}"
|
echo "$pretty_versions"
|
||||||
|
@@ -17,7 +17,7 @@
|
|||||||
zip,
|
zip,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
addon-version-lister = static-nix-shell.mkBash {
|
addon-version-lister = static-nix-shell.mkYsh {
|
||||||
pname = "addon-version-lister";
|
pname = "addon-version-lister";
|
||||||
pkgs = [ "common-updater-scripts" "coreutils" "curl" ];
|
pkgs = [ "common-updater-scripts" "coreutils" "curl" ];
|
||||||
srcRoot = ./.;
|
srcRoot = ./.;
|
||||||
@@ -118,7 +118,7 @@ let
|
|||||||
|
|
||||||
passthru.updateScript = genericUpdater {
|
passthru.updateScript = genericUpdater {
|
||||||
versionLister = writeShellScript "${pname}-version-lister" ''
|
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;
|
passthru.extid = extid;
|
||||||
|
Reference in New Issue
Block a user