#!/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] 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=www TEMPLATES=(html5 html4 javadoc-package javadoc-overview php cakephp-ctp css javascript) NAMES=(package.html overview.html) EXTS=(html htm php phps ctp css js) 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_html5() { local file="$1" local mode=html 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_html4() { local file="$1" local mode=html 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_javadoc_package() { local file="$1" local mode=html 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_javadoc_overview() { local file="$1" local mode=html 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_php() { local file="$1" local mode=php 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_cakephp_ctp() { local file="$1" local mode=php 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_css() { local file="$1" local mode=css 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_javascript() { local file="$1" local mode=javascript 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" == html5 ]; then generate_html5 "$file" files2edit || r=$? elif [ "$t" == html4 ]; then generate_html4 "$file" files2edit || r=$? elif [ "$t" == javadoc-package ]; then generate_javadoc_package "$file" files2edit || r=$? elif [ "$t" == javadoc-overview ]; then generate_javadoc_overview "$file" files2edit || r=$? elif [ "$t" == php ]; then generate_php "$file" files2edit || r=$? elif [ "$t" == cakephp-ctp ]; then generate_cakephp_ctp "$file" files2edit || r=$? elif [ "$t" == css ]; then generate_css "$file" files2edit || r=$? elif [ "$t" == javascript ]; then generate_javascript "$file" files2edit || r=$? else die "www: template invalide: $t" fi done if [ -n "$edit" -a "${#files2edit[*]}" -gt 0 ]; then "${EDITOR:-vi}" "${files2edit[@]}" fi exit $r