Files
artloglaby/generate_new.sh
philippe lhardy 3c4b44e837 add missing script coming from unlinked artlog toolbox project
not mandatory but consistent to have all code

Signed-off-by: philippe lhardy <philippe.lhardy@astrolabe.coop>
2025-11-02 08:41:53 +01:00

210 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
# generate empty templates or add functions given a defined language
usage()
{
head -n 2
}
generate_java_class()
{
cat <<EOF >$JAVA_FILE
package $PACKAGE;
/**
$CLASS was autogenerated by $TOOLP
**/
public $ctype $CLASS
{
Object param;
$CLASS(Object param)
{
this.param=param;
}
void method()
{
System.out.println("Method of $PACKAGE.$CLASS");
}
public static void main(String args[]) {
if ( args.length > 1 )
{
$CLASS instance=new $CLASS(args[1]);
instance.method();
}
else
{
System.err.println("Missing argument for $CLASS");
}
}
}
EOF
}
generate_java_interface()
{
cat <<EOF >$JAVA_FILE
package $PACKAGE;
/**
$CLASS was autogenerated by $TOOLP
**/
public $ctype $CLASS
{
/**
@return X
*/
int getX();
/**
@return Y
*/
int getY();
}
EOF
}
add_new_c_main()
{
cat <<EOF >>$C_FILE
# autogenerated by $TOOLP
int main(int argc, char **argv)
{
todo(\"Code this\");
return 0;
}
EOF
}
add_new_c_function()
{
echo "[INFO] add new c function $FUNCTION in h_file=$H_FILE and c_file=$C_FILE"
if [[ $FUNCTION == main ]]
then
add_new_c_main
else
cat <<EOF >>$H_FILE
# autogenerated by $TOOLP
int $FUNCTION();
EOF
cat <<EOF >>$C_FILE
# autogenerated by $TOOLP
int $FUNCTION(){
todo(\"Code this $FUNCTION\");
return -2;
}
EOF
fi
}
#TODO : support "like=" to copy/renaming an existing class
TOOLP="$0 $*"
ctype=class
genlang=java
while [[ $# > 0 ]]
do
case $1 in
class|interface)
ctype=$1
;;
package=*)
PACKAGE=${1/package=/}
;;
package_dir=*)
PACKAGE_DIR=${1/package=/}
PACKAGE=${PACKAGE//\//\.}
;;
genlang=*)
genlang=${1/genlang=/}
;;
class=*)
CLASS=${1/class=/}
;;
function=*)
FUNCTION=${1/function=/}
;;
c_file=*)
C_FILE=${1/c_file=}
;;
h_file=*)
H_FILE=${1/h_file=}
;;
*)
if [[ -z $CLASS ]]
then
CLASS=${1}
echo "[INFO] setting class=$CLASS" >&2
else
echo "[ERROR] unrecognized parameter $1" >&2
fi
;;
esac
shift 1
done
if [[ $genlang == java ]]
then
if [[ -z $PACKAGE ]]
then
echo "[INFO] obtain package name from project settings"
PACKAGE=$(./debianize.sh getproject_mainpackage)
if [[ -z $PACKAGE ]]
then
echo "[ERROR] can't find project_mainpackage" >&2
exit 1
fi
fi
if [[ -z $CLASS ]]
then
echo "[ERROR] Missing class" >&2
exit 1
fi
PACKAGE_DIR=${PACKAGE//\./\/}
if [[ ! -d $PACKAGE_DIR ]]
then
echo "Missing $PACKAGE_DIR" >&2
exit 1
fi
JAVA_FILE=$PACKAGE_DIR/$CLASS.java
if [[ -e $JAVA_FILE ]]
then
echo "[ERROR] $JAVA_FILE already exists" >&2
exit 1
else
if [[ $ctype == class ]]
then
generate_java_class
else
generate_java_interface
fi
fi
echo "$JAVA_FILE generated"
elif [[ $genlang == c ]]
then
if [[ -z $H_FILE ]]
then
H_FILE=c/genauto.h
fi
if [[ -z $C_FILE ]]
then
C_FILE=c/genauto.c
fi
add_new_c_function
else
echo "[ERROR] language genlang=$genlang NOT supported" >&2
usage
fi