diff --git a/lib/ulib/ptools b/lib/ulib/ptools index c8b990f..f90af63 100644 --- a/lib/ulib/ptools +++ b/lib/ulib/ptools @@ -85,8 +85,48 @@ function list_feature_branches() { ################################################################################ # Outils de haut niveau +function __pom_get_version() { + # obtenir la version dans le pom $1(=pom.xml) + local pom="${1:-pom.xml}" + awk <"$pom" '/^[ \t]*/ { + sub(/^.*/, "") + sub(/<\/version>.*$/, "") + print + exit +}' +} +function __pom_set_version() { + # modifier la version du le fichier $1(=pom.xml) à la valeur + # $2(=1.0.0-SNAPSHOT) + local pom="${1:-pom.xml}" + local version="${2:-1.0.0-SNAPSHOT}" + local tmpfile; ac_set_tmpfile tmpfile + awk <"$pom" >"$tmpfile" ' +BEGIN { + version = '"$(qawk "$version")"' + found = 0 +} +!found && $0 ~ /^[ \t]*/ { + prefix = "" + if (match($0, /^.*/)) { + prefix = substr($0, RSTART, RLENGTH) + } + suffix = "" + if (match($0, /<\/version>.*$/)) { + suffix = substr($0, RSTART, RLENGTH) + } + print prefix version suffix + found = 1 + next +} +{ print }' + cat "$tmpfile" >"$pom" + ac_clean "$tmpfile" +} + function pver() { local DEFAULT_FILE=VERSION.txt + local DEFAULT_POM=pom.xml local -a args local action=auto @@ -109,6 +149,7 @@ function pver() { parse_opts "${PRETTYOPTS[@]}" \ --help '$exit_with pver_display_help' \ -f:,--file: '$set@ file; source=file' \ + -e:,--maven:,--pom: '$set@ file; source=pom' \ -F:,--file-string: '$set@ file; source=file-string' \ -g:,--git-string: '$set@ git; source=git-string' \ -s:,--string: '$set@ version; source=string' \ @@ -140,21 +181,38 @@ function pver() { # Calculer la source if [ "$source" == auto ]; then source=file - for i in "$DEFAULT_FILE" version.txt; do + for i in "$DEFAULT_FILE" version.txt "$DEFAULT_POM"; do if [ -f "$i" ]; then - file="$i" - break + case "$i" in + "$DEFAULT_POM") + source=pom + file="$i" + break + ;; + *) + source=file + file="$i" + break + ;; + esac fi done elif [ "$source" == file ]; then [ "$action" == auto ] && action=update fi [ "$source" == file -a -z "$file" ] && file="$DEFAULT_FILE" + [ "$source" == pom -a -z "$file" ] && file="$DEFAULT_POM" [ "$action" == auto ] && action=show # Lire la version if [ "$source" == file ]; then [ -f "$file" ] && version="$(<"$file")" + elif [ "$source" == pom ]; then + [ -f "$file" ] || { + eerror "$file: fichier introuvable" + return 1 + } + version="$(__pom_get_version "$file")" elif [ "$source" == file-string ]; then if [ -z "$file" ]; then file="$DEFAULT_FILE" @@ -319,6 +377,9 @@ BEGIN { if isatty; then estepi "La version actuelle est $version" fi + elif [ "$source" == pom ]; then + eerror "$file: fichier introuvable" + return 1 else if isatty; then ask_yesno "Le fichier $(ppath "$file") n'existe pas. Faut-il le créer?" O || return 1 @@ -399,7 +460,10 @@ BEGIN { # afficher le résultat final psemver_setvar version if [ -n "$file" ]; then - echo "$version" >"$file" + case "$source" in + file) echo "$version" >"$file";; + pom) __pom_set_version "$file" "$version";; + esac fi if isatty; then estepn "La nouvelle version est $version" diff --git a/pver b/pver index 3e43a3b..b5ca3a5 100755 --- a/pver +++ b/pver @@ -14,6 +14,10 @@ OPTIONS Gérer le numéro de version se trouvant dans le fichier spécifié. Le fichier est créé si nécessaire. C'est l'option par défaut si un fichier nommé VERSION.txt se trouve dans le répertoire courant. + -e, --maven POMFILE + Gérer le numéro de version se trouvant dans le fichier pom.xml spécifié. + Le fichier DOIT exister. C'est l'option par défaut si un fichier nommé + pom.xml se trouve dans le répertoire courant. -F, --file-string VERSIONFILE Prendre pour valeur de départ le contenu du fichier VERSIONFILE (qui vaut par défaut VERSION.txt)