nutools/lib/templates/text

161 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
function display_help() {
uecho "$scriptname: générer un fichier $NAME
USAGE
$scriptname [options] <file>
OPTIONS
-t TEMPLATE
Indiquer le modèle de fichier à utiliser pour la génération. Les valeurs
valides sont:
${TEMPLATES[*]}
-e, --edit
-g, --no-edit
Editer (resp. ne pas éditer) le fichier après l'avoir généré.
Par défaut, l'éditeur est lancé après la génération.
-f, --overwrite
Ecraser le fichier s'il existe déjà
-E, --encoding ENCODING
Spécifier l'encoding à utiliser pour la génération du fichier"
}
NAME=text
TEMPLATES=(text conf sql sieve yaml)
NAMES=()
EXTS=(txt conf sql sieve yml yaml)
if [ $# -eq 2 ]; then
if [ "$1" == "--matches-template" ]; then
for template in "${TEMPLATES[@]}"; do
[ "$template" == "$2" ] && exit 0
done
exit 1
elif [ "$1" == "--matches-name" ]; then
for name in "${NAMES[@]}"; do
[ "$name" == "$2" ] && exit 0
done
exit 1
elif [ "$1" == "--matches-ext" ]; then
for ext in "${EXTS[@]}"; do
[ "$ext" == "$2" ] && exit 0
done
exit 1
fi
fi
#source /etc/ulib || exit 1
source "$(dirname "$0")/../ulib/ulib" || exit 1
urequire DEFAULTS
function check_overwrite() {
if [ -e "$1" -a -z "$overwrite" ]; then
eerror "$1: refus d'écraser un fichier déjà existant (utiliser -f)"
return 1
fi
return 0
}
function generate_text() {
local file="$1"
local mode=text
check_overwrite "$1" || return
estep "$(ppath "$file")"
echo >"$file" "# -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding"
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_conf() {
local file="$1"
local mode=conf
check_overwrite "$1" || return
estep "$(ppath "$file")"
echo >"$file" "# -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding"
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_sql() {
local file="$1"
local mode=sql
check_overwrite "$1" || return
estep "$(ppath "$file")"
echo >"$file" "-- -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding"
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_sieve() {
local file="$1"
local mode=sieve
check_overwrite "$1" || return
estep "$(ppath "$file")"
echo >"$file" "# -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding"
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_yaml() {
local file="$1"
local mode=yaml
check_overwrite "$1" || return
estep "$(ppath "$file")"
echo >"$file" "# -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding"
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
template=
edit=1
overwrite=
encoding=
#executable=
parse_opts "${PRETTYOPTS[@]}" \
--help '$exit_with display_help' \
-t:,--template: template= \
-e,--edit edit=1 \
-g,--no-edit edit= \
-f,--overwrite overwrite=1 \
-E:,--encoding: encoding= \
@ args -- "$@" && set -- "${args[@]}" || die "$args"
# à rajouter ci-dessus si les fichiers peuvent être exécutables:
#-x,--executable executable=1 \
#-n,--no-executable executable= \
[ -n "$encoding" ] || encoding=utf-8
files2edit=()
r=0
for file in "$@"; do
t="$template"
if [ -z "$t" ]; then
dir="$(dirname "$file")"
name="$(basename "$file")"
# Si template n'est pas spécifié, le déterminer le cas échéant à partir
# du nom de fichier. Rajouter ici le code approprié.
fi
case "$t" in
text|txt|t) generate_text "$file" files2edit || r=$?;;
conf|c) generate_conf "$file" files2edit || r=$?;;
sql) generate_sql "$file" files2edit || r=$?;;
sieve) generate_sieve "$file" files2edit || r=$?;;
yaml) generate_yaml "$file" files2edit || r=$?;;
*) die "$NAME: template invalide: $t";;
esac
done
if [ -n "$edit" -a "${#files2edit[*]}" -gt 0 ]; then
"${EDITOR:-vi}" "${files2edit[@]}"
fi
exit $r