nutools/ulib/semver

163 lines
5.0 KiB
Bash

##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## Versionnage sémantique
##@cooked nocomments
##@require base
uprovide semver
urequire base
function __semver_check_version() { [ -z "${1//[a-zA-Z0-9.+-]/}" ]; }
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" ""
# vérifier les caractères valides
__semver_check_version "$_ver" || return 1
# extraire major
_tmp=
while [ "${_ver/#[0-9]/}" != "$_ver" ]; do
_tmp="$_tmp${_ver:0:1}"
_ver="${_ver:1}"
done
[ "${_ver:0:1}" == . ] || return 1
_ver="${_ver:1}"
set_var "$_ma" "$_tmp"
# extraire minor
_tmp=
while [ "${_ver/#[0-9]/}" != "$_ver" ]; do
_tmp="$_tmp${_ver:0:1}"
_ver="${_ver:1}"
done
[ "${_ver:0:1}" == . ] || return 1
_ver="${_ver:1}"
set_var "$_mi" "$_tmp"
# extraire patchlevel
_tmp=
while [ "${_ver/#[0-9]/}" != "$_ver" ]; do
_tmp="$_tmp${_ver:0:1}"
_ver="${_ver:1}"
done
[ -z "$_ver" -o "${_ver:0:1}" == - -o "${_ver:0:1}" == + ] || return 1
set_var "$_pl" "$_tmp"
# extraire prelease
if [ "${_ver:0:1}" == - ]; then
_ver="${_ver:1}"
if [[ "$_ver" == *+* ]]; then
_tmp="${_ver%%+*}"
_ver="+${_ver##*+}"
else
_tmp="$_ver"
_ver=
fi
__semver_check_prelease "$_tmp" || return 1
array_split "$_pr" "$_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" .
fi
# on doit avoir tout analysé
[ -z "$_ver" ] || return 1
set_var "$_va" 1
return 0
}
function semver_incmajor() {
set_var "$1" $((${!1} + 1))
set_var "$2" 0
set_var "$3" 0
array_new "$4"
}
function semver_incminor() {
set_var "$2" $((${!2} + 1))
set_var "$3" 0
array_new "$4"
}
function semver_incpatchlevel() {
set_var "$3" $((${!3} + 1))
array_new "$4"
}
function semver_setprelease() {
# XXX analyser $1 et spliter avant de copier dans le tableau $5
if [ "$1" ]; then
set_array "$5" @ "$1"
else
array_new "$5"
fi
}
function semver_compare_prelease() {
echo err
}
function semver_setmetadata() {
# XXX analyser $1 et spliter avant de copier dans le tableau $6
if [ "$1" ]; then
set_array "$6" @ "$1"
else
array_new "$5"
fi
}
function semver_compare_metadata() {
echo err
}
function semver_copy() {
set_var "$1" "${!6}"
set_var "$2" "${!7}"
set_var "$3" "${!8}"
array_copy "$4" "$9"
array_copy "$5" "${10}"
}
function semver_build() {
echo_ "${!1}.${!2}.${!3}"
array_isempty "$4" || rawecho_ "-$(array_join "$4" .)"
array_isempty "$5" || rawecho_ "+$(array_join "$5" .)"
echo ""
}
function semver_setvar() {
set_var "$1" "$(semver_build "$2" "$3" "$4" "$5" "$6")"
}
################################################################################
# Les fonctions psemver_* sont comme semver_* mais les noms de variables sont
# standardisés (major, minor, pathchlevel, prelease, metadata) et l'on spécifie
# uniquement un préfixe pour les noms de variable
function psemver_parse() { semver_parse "$1" "${2}major" "${2}minor" "${2}patchlevel" "${2}prelease" "${2}metadata" "${2}valid"; }
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_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"; }
function psemver_compare_metadata() { semver_compare_metadata "${1}metadata" "${2}metadata"; }
function psemver_copy() { semver_copy "${1}major" "${1}minor" "${1}patchlevel" "${1}prelease" "${1}metadata" "${2}major" "${2}minor" "${2}patchlevel" "${2}prelease" "${2}metadata"; }
function psemver_build() { semver_build "${1}major" "${1}minor" "${1}patchlevel" "${1}prelease" "${1}metadata"; }
function psemver_setvar() { semver_setvar "$1" "${2}major" "${2}minor" "${2}patchlevel" "${2}prelease" "${2}metadata"; }