diff --git a/data/helpers.d/utils b/data/helpers.d/utils index 576fa5a56..c491b1aa0 100644 --- a/data/helpers.d/utils +++ b/data/helpers.d/utils @@ -486,48 +486,58 @@ ynh_check_app_version_changed () { echo $return_value } -# Compare the old package version and a other version passer as argument. -# This is really useful we we need to do some action only for some old package version. +# Compare the current package version against another version given as an argument. +# This is really useful when we need to do some actions only for some old package versions. # -# example: ynh_compare_package_version --comparaison gt --version 2.3.2~ynh1 -# In word this example will check if the installed version is grater than (gt) the version 2.3.2~ynh1 +# example: ynh_compare_package_version --comparison gt --version 2.3.2~ynh1 +# This example will check if the installed version is greater than (gt) the version 2.3.2~ynh1 # -# usage: ynh_compare_package_version --comparaision lt|gt|le|ge -# | arg: --comparaison - comparaison type. Could be : le (lower than), gt (grater than), le (lower or equals), ge (grater or equals) +# usage: ynh_compare_package_version --comparison lt|gt|le|ge +# | arg: --comparison - Comparison type. Could be : le (lower than), gt (greater than), le (lower or equal), ge (greater or equal) # | 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. +# # Requires YunoHost version 3.8.0 or higher. ynh_compare_package_version() { local legacy_args=cv - declare -Ar args_array=( [c]=comparaison= [v]=version= ) - + declare -Ar args_array=( [c]=comparison= [v]=version= ) local version - local comparaison - local old_version=$YNH_APP_OLD_VERSION + local comparison # Manage arguments with getopts ynh_handle_getopts_args "$@" - if [[ ! $version =~ '~ynh' ]] || [[ ! $old_version =~ '~ynh' ]]; then + local current_version=$YNH_APP_OLD_VERSION + + # Check the syntax of the versions + if [[ ! $version =~ '~ynh' ]] || [[ ! $current_version =~ '~ynh' ]] + then ynh_print_warn "Invalid agument for version." return 1 fi - if [ $version == $old_version ]; then - if [ $comparaison == ge ] || [ $comparaison == le ]; then + # If the version are identical, and the evaluation allows equal versions. + if [ $version == $current_version ] + then + if [ $comparison == ge ] || [ $comparison == le ]; then return 0 else return 1 fi fi - if [ $comparaison == ge ] || [ $comparaison == gt ]; then - if [ $(printf "$version\n$old_version" | sort -V | tail -n 1) == $old_version ]; then + # Check if the current version is greater than the one given as argument + if [ $comparison == ge ] || [ $comparison == gt ] + 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$old_version" | sort -V | tail -n 1) == $version ]; then + if [ $(printf "$version\n$current_version" | sort --version-sort | tail -n 1) == $version ]; then return 0 else return 1