properly support semver

This commit is contained in:
j-hc
2022-09-07 15:59:28 +03:00
parent 8cb74763b4
commit 2e4fd207c0
2 changed files with 164 additions and 3 deletions

159
semver Executable file
View File

@ -0,0 +1,159 @@
#!/usr/bin/env bash
#github.com/fsaintjacques/semver-tool
set -o errexit -o nounset -o pipefail
NAT='0|[1-9][0-9]*'
ALPHANUM='[0-9]*[A-Za-z-][0-9A-Za-z-]*'
IDENT="$NAT|$ALPHANUM"
FIELD='[0-9A-Za-z-]+'
SEMVER_REGEX="\
^[vV]?\
($NAT)\\.($NAT)\\.($NAT)\
(\\-(${IDENT})(\\.(${IDENT}))*)?\
(\\+${FIELD}(\\.${FIELD})*)?$"
function validate_version {
local version=$1
if [[ "$version" =~ $SEMVER_REGEX ]]; then
if [ "$#" -eq "2" ]; then
local major=${BASH_REMATCH[1]}
local minor=${BASH_REMATCH[2]}
local patch=${BASH_REMATCH[3]}
local prere=${BASH_REMATCH[4]}
local build=${BASH_REMATCH[8]}
eval "$2=(\"$major\" \"$minor\" \"$patch\" \"$prere\" \"$build\")"
else
echo "$version"
fi
fi
}
function is_nat {
[[ "$1" =~ ^($NAT)$ ]]
}
function is_null {
[ -z "$1" ]
}
function order_nat {
[ "$1" -lt "$2" ] && {
echo -1
return
}
[ "$1" -gt "$2" ] && {
echo 1
return
}
echo 0
}
function order_string {
[[ $1 < $2 ]] && {
echo -1
return
}
[[ $1 > $2 ]] && {
echo 1
return
}
echo 0
}
function compare_fields {
local l="$1[@]"
local r="$2[@]"
local leftfield=("${!l}")
local rightfield=("${!r}")
local left
local right
local i=$((-1))
local order=$((0))
while true; do
[ $order -ne 0 ] && {
echo $order
return
}
: $((i++))
left="${leftfield[$i]}"
right="${rightfield[$i]}"
is_null "$left" && is_null "$right" && {
echo 0
return
}
is_null "$left" && {
echo -1
return
}
is_null "$right" && {
echo 1
return
}
is_nat "$left" && is_nat "$right" && {
order=$(order_nat "$left" "$right")
continue
}
is_nat "$left" && {
echo -1
return
}
is_nat "$right" && {
echo 1
return
}
{
order=$(order_string "$left" "$right")
continue
}
done
}
# shellcheck disable=SC2206
function compare_version {
local order
validate_version "$1" V
validate_version "$2" V_
local left=("${V[0]}" "${V[1]}" "${V[2]}")
local right=("${V_[0]}" "${V_[1]}" "${V_[2]}")
order=$(compare_fields left right)
[ "$order" -ne 0 ] && {
echo "$order"
return
}
local prerel="${V[3]:1}"
local prerel_="${V_[3]:1}"
local left=(${prerel//./ })
local right=(${prerel_//./ })
[ -z "$prerel" ] && [ -z "$prerel_" ] && {
echo 0
return
}
[ -z "$prerel" ] && {
echo 1
return
}
[ -z "$prerel_" ] && {
echo -1
return
}
compare_fields left right
}
function command_compare {
local v
local v_
v=$(validate_version "$1")
v_=$(validate_version "$2")
set +u
compare_version "$v" "$v_"
exit 0
}
command_compare "$@"

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
MODULE_TEMPLATE_DIR="revanced-magisk"
MODULE_SCRIPTS_DIR="scripts"
@ -92,10 +92,12 @@ reset_template() {
req() { wget -nv -O "$2" --header="$WGET_HEADER" "$1"; }
log() { echo -e "$1 " >>build.log; }
get_apk_vers() { req "$1" - | sed -n 's;.*Version:</span><span class="infoSlide-value">\(.*\) </span>.*;\1;p'; }
get_largest_ver() { # fix this later to properly support semver
get_largest_ver() {
local max=0
while read -r v || [ -n "$v" ]; do
if [[ ${v//[!0-9]/} -gt ${max//[!0-9]/} ]]; then max=$v; fi
if [ "$(./semver "$v" "$max")" = 1 ]; then
max=$v
fi
done
if [[ $max = 0 ]]; then echo ""; else echo "$max"; fi
}