Use 'dpkg --compare-versions'

This commit is contained in:
Josué Tille 2020-04-15 12:07:45 +02:00
parent d947724b70
commit ceeb34f68e
No known key found for this signature in database
GPG key ID: 716A6C99B04194EF

View file

@ -499,8 +499,9 @@ ynh_check_app_version_changed () {
# # Do something that is needed for the package version older than 2.3.2~ynh1 # # Do something that is needed for the package version older than 2.3.2~ynh1
# fi # fi
# #
# usage: ynh_compare_current_package_version --comparison lt|gt|le|ge # usage: ynh_compare_current_package_version --comparison lt|le|eq|ne|ge|gt
# | arg: --comparison - Comparison type. Could be : le (lower than), gt (greater than), le (lower or equal), ge (greater or equal) # | arg: --comparison - Comparison type. Could be : le (lower than), le (lower or equal),
# | eq (equal), ne (not equal), ge (greater or equal), gt (greater than)
# | arg: --version - The version to compare. Need to be a version in the yunohost package version type (like 2.3.1~ynh4) # | arg: --version - The version to compare. Need to be a version in the yunohost package version type (like 2.3.1~ynh4)
# #
# Return 0 if the evaluation is true. 1 if false. # Return 0 if the evaluation is true. 1 if false.
@ -519,35 +520,14 @@ ynh_compare_current_package_version() {
# Check the syntax of the versions # Check the syntax of the versions
if [[ ! $version =~ '~ynh' ]] || [[ ! $current_version =~ '~ynh' ]] if [[ ! $version =~ '~ynh' ]] || [[ ! $current_version =~ '~ynh' ]]
then then
ynh_print_warn "Invalid agument for version." ynh_die "Invalid argument for version."
return 1
fi fi
# If the version are identical, and the evaluation allows equal versions. # Check validity of the comparator
if [ $version == $current_version ] if [[ ! $comparison =~ (lt|le|eq|ne|ge|gt) ]]; then
then ynh_die "Invialid comparator must be : lt, le, eq, ne, ge, gt"
if [ $comparison == ge ] || [ $comparison == le ]; then
return 0
else
return 1
fi
fi fi
# Check if the current version is greater than the one given as argument # Return the return value of dpkg --compare-versions
if [ $comparison == ge ] || [ $comparison == gt ] dpkg --compare-versions $current_version $comparison $version
then
if [ $(printf "$version\n$current_version" | sort --version-sort | tail -n 1) == $current_version ]; then
return 0
else
return 1
fi
# Else if the current version is lower than the one given as argument
else
if [ $(printf "$version\n$current_version" | sort --version-sort | tail -n 1) == $version ]; then
return 0
else
return 1
fi
fi
} }