163 lines
4.2 KiB
Bash
Executable File
163 lines
4.2 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=compileAndGo
|
|
TEMPLATES=(javago)
|
|
NAMES=()
|
|
EXTS=(jgo)
|
|
|
|
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_javago() {
|
|
local file="$1"
|
|
local dir="$(dirname "$file")"
|
|
local name="$(basename "$file")"
|
|
local basename="${name%.*}"
|
|
local mode=java
|
|
|
|
check_overwrite "$1" || return
|
|
estep "$(ppath "$file")"
|
|
echo >"$file" "#!/usr/bin/env compileAndGo
|
|
# -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding
|
|
compiler=javac
|
|
mainClass=$basename
|
|
compileAndGo
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.util.logging.ConsoleHandler;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
public class $basename {
|
|
public void run(String[] args) {
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Il n'est pas nécessaire de modifier à partir d'ici
|
|
static final String CLASS_NAME = $basename.class.getName();
|
|
static final Logger log = Logger.getLogger(CLASS_NAME);
|
|
static {
|
|
ConsoleHandler handler = new ConsoleHandler();
|
|
handler.setLevel(Level.ALL);
|
|
log.addHandler(handler);
|
|
log.setUseParentHandlers(false);
|
|
}
|
|
|
|
static final void p(String s) {
|
|
System.out.println(s);
|
|
System.out.flush();
|
|
}
|
|
static final void die(String msg, Throwable t) {
|
|
if (msg != null) System.err.println(msg);
|
|
if (t != null) t.printStackTrace(System.err);
|
|
System.err.flush();
|
|
System.exit(1);
|
|
}
|
|
|
|
public static final void main(String[] args) {
|
|
try {
|
|
new $basename().run(args);
|
|
System.exit(0);
|
|
} catch (Throwable t) {
|
|
if (t instanceof InvocationTargetException) t = t.getCause();
|
|
die(null, t);
|
|
}
|
|
}
|
|
}"
|
|
[ -n "$executable" ] && chmod +x "$file"
|
|
[ -n "$2" ] && array_add "$2" "$file"
|
|
return 0
|
|
}
|
|
|
|
template=
|
|
edit=1
|
|
overwrite=
|
|
encoding=
|
|
executable=1
|
|
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= \
|
|
-x,--executable executable=1 \
|
|
-n,--no-executable executable= \
|
|
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
|
|
|
[ -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
|
|
javago|jgo) generate_javago "$file" files2edit || r=$?;;
|
|
*) die "$NAME: template invalide: $t";;
|
|
esac
|
|
done
|
|
|
|
if [ -n "$edit" -a "${#files2edit[*]}" -gt 0 ]; then
|
|
"${EDITOR:-vi}" "${files2edit[@]}"
|
|
fi
|
|
|
|
exit $r
|