/bin/cgs
Korn Shell | 127 lines | 76 code | 17 blank | 34 comment | 12 complexity | 597403b0e785b73f9cbd0536c5f8d145 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: cgs
- # AUTHOR: hiarcs <fey.young@gmail.com>
- # DATE: 2011-3-13
- #
- # PURPOSE: create a scala class (TBD: 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: cgs classname template"
- echo "Exampls:"
- echo " cgs com.youngfey.util.Timer class use full class name"
- echo " cgs util.Timer class if working directory is src/main/scala/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/scala` ]
- then
- src_pkg_pf=`path2pkg ${working_directory#$project_directory/src/main/scala/}`
- 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
- src_path=$project_directory/src/main/scala/`pkg2path $src_package`
- src_file=$src_path/$class_name.scala
- 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/scala/$2 > $src_file
- if [ `echo $2 | grep ^class` ]
- then
- test_path=$project_directory/src/test/scala/`pkg2path $src_package`
- test_file=$test_path/${class_name}Test.scala
- 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/scala/junit > $test_file
- fi
- # End of script