141 lines
3.6 KiB
Plaintext
141 lines
3.6 KiB
Plaintext
|
#!/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)
|
||
|
NAMES=()
|
||
|
EXTS=(txt conf sql)
|
||
|
|
||
|
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 &&
|
||
|
source "$(dirname "$0")/../../ulib/ulib" &&
|
||
|
urequire DEFAULTS ||
|
||
|
exit 1
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
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
|
||
|
|
||
|
if [ "$t" == text ]; then
|
||
|
generate_text "$file" files2edit || r=$?
|
||
|
elif [ "$t" == conf ]; then
|
||
|
generate_conf "$file" files2edit || r=$?
|
||
|
elif [ "$t" == sql ]; then
|
||
|
generate_sql "$file" files2edit || r=$?
|
||
|
else
|
||
|
die "text: template invalide: $t"
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
if [ -n "$edit" -a "${#files2edit[*]}" -gt 0 ]; then
|
||
|
"${EDITOR:-vi}" "${files2edit[@]}"
|
||
|
fi
|
||
|
|
||
|
exit $r
|