Skip to content

Commit 509c83b

Browse files
author
Jonas Trampe
committed
Update version checker to cope with Major.Minor.Feature-RCVariant
1 parent a4ac78b commit 509c83b

File tree

3 files changed

+162
-32
lines changed

3 files changed

+162
-32
lines changed

deploy.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ TMP_FILENAME=hucon-${1}.tar.gz
1313
FILENAME=hucon-${1}.run
1414
RELEASE_DIR=release
1515
TEMP_DIR=$RELEASE_DIR/temp
16+
17+
FILELIST="LICENSE README.md code/ init.d/ python_lib/ webserver/ i2c_led.sh install.sh img_install.sh start_server.sh uninstall.sh update.sh version_compare.sh"
18+
1619
cd "$BASEDIR"
1720

1821
# create release dir, create version file and copy to the release folder
1922
mkdir -p $TEMP_DIR
20-
cp -r LICENSE README.md code/ init.d/ python_lib/ webserver/ i2c_led.sh install.sh img_install.sh start_server.sh uninstall.sh update.sh $TEMP_DIR
23+
cp -r ${FILELIST} $TEMP_DIR
2124

2225
cd $TEMP_DIR
2326
echo $1 > __version__
@@ -38,7 +41,7 @@ find . -type f \
3841
-exec mv {}.min {} \;
3942

4043
# compress the needed files into one tar image
41-
tar -czvf "$TMP_FILENAME" __version__ LICENSE README.md code/ init.d/ python_lib/ webserver/ i2c_led.sh install.sh img_install.sh start_server.sh uninstall.sh update.sh
44+
tar -czvf "$TMP_FILENAME" __version__ ${FILELIST}
4245

4346
mv "$TMP_FILENAME" ..
4447
cd ..

update.sh

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh -e
1+
#!/bin/bash -e
22
# update.sh - Update the system or return the latest released version from github.
33
#
44
# Copyright (C) 2019 Basler AG
@@ -66,35 +66,20 @@ if [ $do_check ]; then
6666
echo "Your version is: $currentVersion"
6767
echo "The latest version is: $latestVersion"
6868

69-
currentMajor=$(echo $currentVersion| cut -d'.' -f1)
70-
currentMinor=$(echo $currentVersion| cut -d'.' -f2)
71-
currentBugfix=$(echo $currentVersion| cut -d'.' -f3)
72-
73-
latestMajor=$(echo $latestVersion| cut -d'.' -f1)
74-
latestMinor=$(echo $latestVersion| cut -d'.' -f2)
75-
latestBugfix=$(echo $latestVersion| cut -d'.' -f3)
76-
77-
if [ ${latestMajor} -gt ${currentMajor} ]; then
78-
echo "There is a major update available."
79-
exit 1
80-
elif [ ${latestMajor} -eq ${currentMajor} ]; then
81-
if [ ${latestMinor} -gt ${currentMinor} ]; then
82-
echo "There is a minor update available."
83-
exit 1
84-
elif [ ${latestMinor} -eq ${currentMinor} ]; then
85-
86-
if [ "$latestBugfix" > "$currentBugfix" ]; then
87-
echo "There is a bugfix update available."
88-
exit 1
89-
else
90-
echo "You are using the up to date version."
91-
fi
92-
93-
else
94-
echo "You are using the up to date version."
95-
fi
96-
else
97-
echo "You are using the up to date version."
69+
result=$(/bin/bash version_compare.sh "${currentVersion}" "${latestVersion}")
70+
71+
if [ "${result}" -eq 0 ]; then
72+
echo "There is no new version!"
73+
elif [ "${result}" -eq 1 ]; then
74+
echo "Your version is newer than the available version!"
75+
elif [ "${result}" -eq 2 ]; then
76+
echo "There is a new stable version!"
77+
elif [ "${result}" -eq 3 ]; then
78+
echo "You have an unstable version, but hey it's the newest one"
79+
elif [ "${result}" -eq 4 ]; then
80+
echo "Your unstable version is newer than the available version!"
81+
elif [ "${result}" -eq 5 ]; then
82+
echo "There is a new unstable (alpha/beta/rc) version! It is highly experimental and discouraged to be installed!"
9883
fi
9984
else
10085
echo "Could not read the version from github."

version_compare.sh

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/bin/bash
2+
# version_compare.sh - compares numerical versions and versions with ascii remainders
3+
#
4+
# Copyright (C) 2019 Basler AG
5+
# All rights reserved.
6+
#
7+
# This software may be modified and distributed under the terms
8+
# of the CreativeCommons BY-SA 4.0. license.
9+
# See the https://creativecommons.org/licenses/by-sa/4.0/ file for details.
10+
11+
#-----------------
12+
# this base snippet is by https://stackoverflow.com/users/60075/craig-mcqueen
13+
# at fetched from https://stackoverflow.com/a/31258615
14+
# for compression purposes the debug stuff was removed
15+
16+
ascii_frag() {
17+
expr match "$1" "\([^[:digit:]]*\)"
18+
}
19+
20+
ascii_remainder() {
21+
expr match "$1" "[^[:digit:]]*\(.*\)"
22+
}
23+
24+
numeric_frag() {
25+
expr match "$1" "\([[:digit:]]*\)"
26+
}
27+
28+
numeric_remainder() {
29+
expr match "$1" "[[:digit:]]*\(.*\)"
30+
}
31+
32+
vercomp_debug() {
33+
OUT="$1"
34+
#echo "${OUT}"
35+
}
36+
37+
# return 1 for $1 > $2
38+
# return 2 for $1 < $2
39+
# return 0 for equal
40+
vercomp() {
41+
local WORK1="$1"
42+
local WORK2="$2"
43+
local NUM1="", NUM2="", ASCII1="", ASCII2=""
44+
45+
while true; do
46+
vercomp_debug "ASCII compare"
47+
ASCII1=`ascii_frag "${WORK1}"`
48+
ASCII2=`ascii_frag "${WORK2}"`
49+
WORK1=`ascii_remainder "${WORK1}"`
50+
WORK2=`ascii_remainder "${WORK2}"`
51+
52+
if [ "${ASCII1}" \> "${ASCII2}" ]; then
53+
return 1
54+
elif [ "${ASCII1}" \< "${ASCII2}" ]; then
55+
return 2
56+
fi
57+
58+
NUM1=`numeric_frag "${WORK1}"`
59+
NUM2=`numeric_frag "${WORK2}"`
60+
WORK1=`numeric_remainder "${WORK1}"`
61+
WORK2=`numeric_remainder "${WORK2}"`
62+
63+
if [ -z "${NUM1}" -a -z "${NUM2}" ]; then
64+
return 0
65+
elif [ -z "${NUM1}" -a -n "${NUM2}" ]; then
66+
return 2
67+
elif [ -n "${NUM1}" -a -z "${NUM2}" ]; then
68+
return 1
69+
fi
70+
71+
if [ "${NUM1}" -gt "${NUM2}" ]; then
72+
return 1
73+
elif [ "${NUM1}" -lt "${NUM2}" ]; then
74+
return 2
75+
fi
76+
done
77+
}
78+
# base snippet end
79+
#------------------
80+
compare_versions()
81+
{
82+
local INPUT_ONE="", INPUT_ONE_VERSION="", INPUT_ONE_SUFFIX=""
83+
local INPUT_TWO="", INPUT_TWO_VERSION="", INPUT_TWO_SUFFIX=""
84+
local VERSION_COMPARE=0
85+
86+
local STABLE_EQ=0
87+
local STABLE_ONE=1
88+
local STABLE_TWO=2
89+
90+
local UNSTABLE_EQ=3
91+
local UNSTABLE_ONE=4
92+
local UNSTABLE_TWO=5
93+
94+
INPUT_ONE="$1"
95+
INPUT_TWO="$2"
96+
97+
98+
INPUT_ONE_VERSION=$(echo "${INPUT_ONE}" | cut -d'-' -f1)
99+
if [[ "${INPUT_ONE}" =~ "-" ]]; then
100+
INPUT_ONE_SUFFIX=$(echo "${INPUT_ONE}" | cut -d'-' -f2)
101+
fi
102+
103+
INPUT_TWO_VERSION=$(echo "${INPUT_TWO}" | cut -d'-' -f1)
104+
if [[ "${INPUT_TWO}" =~ "-" ]]; then
105+
INPUT_TWO_SUFFIX=$(echo "${INPUT_TWO}" | cut -d'-' -f2)
106+
fi
107+
108+
vercomp "${INPUT_ONE_VERSION}" "${INPUT_TWO_VERSION}"
109+
110+
VERSION_COMPARE=$?
111+
112+
if [ "${VERSION_COMPARE}" = "0" ]; then
113+
if [ -z "${INPUT_ONE_SUFFIX}" ] && [ -z "${INPUT_TWO_SUFFIX}" ]; then
114+
return ${STABLE_EQ};
115+
elif [ -z "${INPUT_ONE_SUFFIX}" ] && [ -n "${INPUT_TWO_SUFFIX}" ]; then
116+
return ${STABLE_ONE};
117+
elif [ -n "${INPUT_ONE_SUFFIX}" ] && [ -z "${INPUT_TWO_SUFFIX}" ]; then
118+
return ${STABLE_TWO};
119+
elif [ "${INPUT_ONE_SUFFIX}" = "${INPUT_TWO_SUFFIX}" ]; then
120+
return ${UNSTABLE_EQ};
121+
else
122+
vercomp "${INPUT_ONE_SUFFIX}" "${INPUT_TWO_SUFFIX}"
123+
return $((UNSTABLE_EQ+$?))
124+
fi
125+
elif [ "${VERSION_COMPARE}" = "1" ]; then
126+
if [ -z "${INPUT_ONE_SUFFIX}" ]; then
127+
return ${STABLE_ONE};
128+
else
129+
return ${UNSTABLE_ONE}
130+
fi
131+
elif [ "${VERSION_COMPARE}" = "2" ]; then
132+
if [ -z "${INPUT_TWO_SUFFIX}" ]; then
133+
return ${STABLE_TWO};
134+
else
135+
return ${UNSTABLE_TWO}
136+
fi
137+
fi
138+
}
139+
140+
compare_versions $1 $2
141+
142+
echo $?

0 commit comments

Comments
 (0)