corriger l'implémentation de -v

n'afficher les message que si l'on a un tty
implémenter semver_setversion
This commit is contained in:
Jephte CLAIN 2014-01-18 20:51:36 +04:00
parent ac8cc8753d
commit e10b3f09f3
2 changed files with 78 additions and 57 deletions

View File

@ -10,74 +10,76 @@ function __semver_check_prelease() { [ -z "${1//[a-zA-Z0-9.-]/}" ]; }
function __semver_check_metadata() { [ -z "${1//[a-zA-Z0-9.-]/}" ]; }
function semver_parse() {
local _ver="$1" _ma="${2:-major}" _mi="${3:-minor}" _pl="${4:-patchlevel}" _pr="${5:-prelease}" _md="${6:-metadata}" _va="${7:-valid}"
local _tmp
set_var "$_ma" ""
set_var "$_mi" ""
set_var "$_pl" ""
array_new "$_pr"
array_new "$_md"
set_var "$_va" ""
local __p_ver="$1"
local __p_ma="${2:-major}" __p_mi="${3:-minor}" __p_pl="${4:-patchlevel}"
local __p_pr="${5:-prelease}" __p_md="${6:-metadata}" __p_va="${7:-valid}"
local __p_tmp
set_var "$__p_ma" ""
set_var "$__p_mi" ""
set_var "$__p_pl" ""
array_new "$__p_pr"
array_new "$__p_md"
set_var "$__p_va" ""
# vérifier les caractères valides
__semver_check_version "$_ver" || return 1
__semver_check_version "$__p_ver" || return 1
# extraire major
_tmp=
while [ "${_ver/#[0-9]/}" != "$_ver" ]; do
_tmp="$_tmp${_ver:0:1}"
_ver="${_ver:1}"
__p_tmp=
while [ "${__p_ver/#[0-9]/}" != "$__p_ver" ]; do
__p_tmp="$__p_tmp${__p_ver:0:1}"
__p_ver="${__p_ver:1}"
done
[ "${_ver:0:1}" == . ] || return 1
_ver="${_ver:1}"
set_var "$_ma" "$_tmp"
[ "${__p_ver:0:1}" == . ] || return 1
__p_ver="${__p_ver:1}"
set_var "$__p_ma" "$__p_tmp"
# extraire minor
_tmp=
while [ "${_ver/#[0-9]/}" != "$_ver" ]; do
_tmp="$_tmp${_ver:0:1}"
_ver="${_ver:1}"
__p_tmp=
while [ "${__p_ver/#[0-9]/}" != "$__p_ver" ]; do
__p_tmp="$__p_tmp${__p_ver:0:1}"
__p_ver="${__p_ver:1}"
done
[ "${_ver:0:1}" == . ] || return 1
_ver="${_ver:1}"
set_var "$_mi" "$_tmp"
[ "${__p_ver:0:1}" == . ] || return 1
__p_ver="${__p_ver:1}"
set_var "$__p_mi" "$__p_tmp"
# extraire patchlevel
_tmp=
while [ "${_ver/#[0-9]/}" != "$_ver" ]; do
_tmp="$_tmp${_ver:0:1}"
_ver="${_ver:1}"
__p_tmp=
while [ "${__p_ver/#[0-9]/}" != "$__p_ver" ]; do
__p_tmp="$__p_tmp${__p_ver:0:1}"
__p_ver="${__p_ver:1}"
done
[ -z "$_ver" -o "${_ver:0:1}" == - -o "${_ver:0:1}" == + ] || return 1
set_var "$_pl" "$_tmp"
[ -z "$__p_ver" -o "${__p_ver:0:1}" == - -o "${__p_ver:0:1}" == + ] || return 1
set_var "$__p_pl" "$__p_tmp"
# extraire prelease
if [ "${_ver:0:1}" == - ]; then
_ver="${_ver:1}"
if [[ "$_ver" == *+* ]]; then
_tmp="${_ver%%+*}"
_ver="+${_ver##*+}"
if [ "${__p_ver:0:1}" == - ]; then
__p_ver="${__p_ver:1}"
if [[ "$__p_ver" == *+* ]]; then
__p_tmp="${__p_ver%%+*}"
__p_ver="+${__p_ver##*+}"
else
_tmp="$_ver"
_ver=
__p_tmp="$__p_ver"
__p_ver=
fi
__semver_check_prelease "$_tmp" || return 1
array_split "$_pr" "$_tmp" .
__semver_check_prelease "$__p_tmp" || return 1
array_split "$__p_pr" "$__p_tmp" .
fi
# extraire metadata
if [ "${_ver:0:1}" == + ]; then
_ver="${_ver:1}"
_tmp="$_ver"
_ver=
__semver_check_metadata "$_tmp" || return 1
array_split "$_md" "$_tmp" .
if [ "${__p_ver:0:1}" == + ]; then
__p_ver="${__p_ver:1}"
__p_tmp="$__p_ver"
__p_ver=
__semver_check_metadata "$__p_tmp" || return 1
array_split "$__p_md" "$__p_tmp" .
fi
# on doit avoir tout analysé
[ -z "$_ver" ] || return 1
[ -z "$__p_ver" ] || return 1
set_var "$_va" 1
set_var "$__p_va" 1
return 0
}
@ -99,6 +101,16 @@ function semver_incpatchlevel() {
array_new "$4"
}
function semver_setversion() {
local __sv_ma __sv_mi __sv_svl __sv_svr __sv_md __sv_va
semver_parse "$1" __sv_ma __sv_mi __sv_pl __sv_pr __sv_md __sv_va
[ -n "$__sv_va" ] || return 1
set_var "$2" "$__sv_ma"
set_var "$3" "$__sv_mi"
set_var "$4" "$__sv_pl"
return 0
}
function semver_setprelease() {
# XXX analyser $1 et spliter avant de copier dans le tableau $5
if [ "$1" ]; then
@ -153,6 +165,7 @@ function psemver_parse() { semver_parse "$1" "${2}major" "${2}minor" "${2}patchl
function psemver_incmajor() { semver_incmajor "${1}major" "${1}minor" "${1}patchlevel" "${1}prelease" "${1}metadata"; }
function psemver_incminor() { semver_incminor "${1}major" "${1}minor" "${1}patchlevel" "${1}prelease" "${1}metadata"; }
function psemver_incpatchlevel() { semver_incpatchlevel "${1}major" "${1}minor" "${1}patchlevel" "${1}prelease" "${1}metadata"; }
function psemver_setversion() { semver_setversion "$1" "${2}major" "${2}minor" "${2}patchlevel" "${2}prelease" "${2}metadata"; }
function psemver_setprelease() { semver_setprelease "$1" "${2}major" "${2}minor" "${2}patchlevel" "${2}prelease" "${2}metadata"; }
function psemver_compare_prelease() { semver_compare_prelease "${1}prelease" "${2}prelease"; }
function psemver_setmetadata() { semver_setmetadata "$1" "${2}major" "${2}minor" "${2}patchlevel" "${2}prelease" "${2}metadata"; }

View File

@ -137,17 +137,14 @@ fi
# Actions
if [ "$action" == show ]; then
estepi "La version actuelle est $version"
if isatty; then
estepi "La version actuelle est $version"
else
echo "$version"
fi
exit 0
fi
# forcer le numéro de version
# XXX implémenter semver_setversion qui change juste la partie
# major.minor.patchlevel et ne touche pas au reste. une fois que cela sera fait,
# la ligne suivante doit être supprimée et le code de setversion doit être
# mis dans la partie "update"
[ -n "$setversion" ] && version="$setversion"
psemver_parse "$version"
if [ "$action" == check ]; then
[ -n "$valid" ] || die "Numéro de version invalide: $version"
@ -236,12 +233,19 @@ elif [ "$action" == update ]; then
if [ -n "$file" ]; then
if [ -f "$file" ]; then
estepi "La version actuelle est $version"
if isatty; then
estepi "La version actuelle est $version"
fi
else
ask_yesno "Le fichier $(ppath "$file") n'existe pas. Faut-il le créer?" O || die
if isatty; then
ask_yesno "Le fichier $(ppath "$file") n'existe pas. Faut-il le créer?" O || die
fi
fi
fi
# forcer le numéro de version
psemver_setversion "$setversion" "" || die "Numéro de version invalide: $setversion"
# incrémenter les numéros de version
if [ "$incversion" == auto ]; then
if [ -n "$setrelease" -o -n "$setprelease" -o -n "$setmetadata" ]; then
@ -299,7 +303,11 @@ elif [ "$action" == update ]; then
if [ -n "$file" ]; then
echo "$version" >"$file"
fi
estepn "La nouvelle version est $version"
if isatty; then
estepn "La nouvelle version est $version"
else
echo "$version"
fi
fi
exit 0