-
Notifications
You must be signed in to change notification settings - Fork 4
/
version.functions
75 lines (62 loc) · 1.54 KB
/
version.functions
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
# This file is part of shellfire version. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/version/master/COPYRIGHT. No part of shellfire version, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire version. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/version/master/COPYRIGHT.
version_isLessThan()
{
_version_is eq 0 "$@"
}
version_isEqualTo()
{
_version_is eq 1 "$@"
}
version_isGreaterThan()
{
_version_is eq 2 "$@"
}
version_isLessThanOrEqualTo()
{
_version_is le 1 "$@"
}
version_isGreaterThanOrEqualTo()
{
_version_is ge 1 "$@"
}
_version_is()
{
local operator=$1
local expectedCode=$2
shift 2
set +e
version_compare "$@"
local exitCode=$?
set -e
if [ $exitCode -$operator $expectedCode ]; then
return 0
fi
return 1
}
# 0 is less than
# 1 is equal to
# 2 is greater than
version_compare()
{
local leftVersion="$1"
local rightVersion="$2"
if [ "$leftVersion" = "$rightVersion" ]; then
return 1
fi
local leastVersion
read -r leastVersion <<-EOF
$(_version_compare_sort)
EOF
if [ "$leastVersion" = "$leftVersion" ]; then
return 0
fi
return 2
}
_version_compare_sort()
{
sort -n -t '.' -k 1,1n -k 2,2n -k 3,4n -k 4,4n -k 5,5n -k 6,6n -k 7,7n <<-EOF
${leftVersion}
${rightVersion}
EOF
}