forked from j-hc/revanced-magisk-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver
executable file
·150 lines (139 loc) · 2.61 KB
/
semver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
#github.com/fsaintjacques/semver-tool
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_"
}