/bin/cgj
Korn Shell | 133 lines | 82 code | 17 blank | 34 comment | 13 complexity | 811bc5f0c7beaf39ae858c2a461a1e0b MD5 | raw file
- #!/bin/env ksh
- #
- # Copyright (C) 2010 Young, Fey <fey.young@gmail.com>
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- #
- # SCRIPT: cgj
- # AUTHOR: hiarcs <fey.young@gmail.com>
- # DATE: 2010-09-08
- #
- # PURPOSE: create a java class and related junit test
- #
- # set -n # Uncomment to check syntax without execution
- #
- # set -x # Uncomment to debug
- #
- ######################### FILES AND VARIABLES #############################
- . ~/.xmeta/xmeta.conf
- working_directory=`pwd`
- project_directory=
- ############################## FUNCTIONS ##################################
- function usage {
- echo "Usage: cgj classname template"
- echo "Exampls:"
- echo " cgj com.youngfey.util.Timer class.groovy use full class name"
- echo " cgj util.Timer class.java if working directory is src/main/groovy/com.youngfey"
- exit 1
- }
- ################################# MAIN ####################################
- # make sure you are in a project folder
- while [ -z $project_directory ]
- do
- if [ -s .xmeta ]
- then
- project_directory=`pwd`
- . $project_directory/.xmeta
- export author email project_name license code_name
- else
- cd ..
- fi
- if [ `pwd` = / ]
- then
- echo "\"$working_directory\" does not appear to be part of a Xmeta project"
- exit 2
- fi
- done
- # check args
- if (( $# != 2 ))
- then
- usage
- fi
- # file name and package name
- class_name=${ basename `pkg2path $1` }
- if [ `echo "$working_directory" | grep ^$project_directory/src/main/groovy` ]
- then
- src_pkg_pf=`path2pkg ${working_directory#$project_directory/src/main/groovy/}`
- if [ `echo $1 | grep '\.'` ]
- then
- if [ `echo $1 | grep ^$package_name` ]
- then
- src_package=${1%%.$class_name}
- else
- src_package=$src_pkg_pf.${1%%.$class_name}
- fi
- else
- src_package=$src_pkg_pf
- fi
- else
- if [ `echo $1 | grep '\.'` ]
- then
- src_package=${1%%.$class_name}
- else
- src_package=$package_name
- fi
- fi
- export class_name
- if [ `echo $2 | grep groovy` ]
- then
- filetype=groovy
- else
- filetype=java
- fi
- src_path=$project_directory/src/main/groovy/`pkg2path $src_package`
- src_file=$src_path/$class_name.$filetype
- if [ -s $src_file ]
- then
- echo "file $class_name already existed"
- exit 3
- fi
- mkdir -p $src_path
- export package_name=$src_package
- export import_text=
- cast $XMETA_HOME/templates/java/$2 > $src_file
- if [ `echo $2 | grep ^class` ]
- then
- test_path=$project_directory/src/test/groovy/`pkg2path $src_package`
- test_file=$test_path/${class_name}Test.groovy
- if [ -s $test_file ]
- then
- echo "Warning: created class $class_name, but test file already exists."
- fi
- mkdir -p $test_path
- cast $XMETA_HOME/templates/java/junit.groovy > $test_file
- fi
- # End of script