PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/cgj

https://code.google.com/p/xmeta/
Korn Shell | 133 lines | 82 code | 17 blank | 34 comment | 13 complexity | 811bc5f0c7beaf39ae858c2a461a1e0b MD5 | raw file
  1. #!/bin/env ksh
  2. #
  3. # Copyright (C) 2010 Young, Fey <fey.young@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # SCRIPT: cgj
  19. # AUTHOR: hiarcs <fey.young@gmail.com>
  20. # DATE: 2010-09-08
  21. #
  22. # PURPOSE: create a java class and related junit test
  23. #
  24. # set -n # Uncomment to check syntax without execution
  25. #
  26. # set -x # Uncomment to debug
  27. #
  28. ######################### FILES AND VARIABLES #############################
  29. . ~/.xmeta/xmeta.conf
  30. working_directory=`pwd`
  31. project_directory=
  32. ############################## FUNCTIONS ##################################
  33. function usage {
  34. echo "Usage: cgj classname template"
  35. echo "Exampls:"
  36. echo " cgj com.youngfey.util.Timer class.groovy use full class name"
  37. echo " cgj util.Timer class.java if working directory is src/main/groovy/com.youngfey"
  38. exit 1
  39. }
  40. ################################# MAIN ####################################
  41. # make sure you are in a project folder
  42. while [ -z $project_directory ]
  43. do
  44. if [ -s .xmeta ]
  45. then
  46. project_directory=`pwd`
  47. . $project_directory/.xmeta
  48. export author email project_name license code_name
  49. else
  50. cd ..
  51. fi
  52. if [ `pwd` = / ]
  53. then
  54. echo "\"$working_directory\" does not appear to be part of a Xmeta project"
  55. exit 2
  56. fi
  57. done
  58. # check args
  59. if (( $# != 2 ))
  60. then
  61. usage
  62. fi
  63. # file name and package name
  64. class_name=${ basename `pkg2path $1` }
  65. if [ `echo "$working_directory" | grep ^$project_directory/src/main/groovy` ]
  66. then
  67. src_pkg_pf=`path2pkg ${working_directory#$project_directory/src/main/groovy/}`
  68. if [ `echo $1 | grep '\.'` ]
  69. then
  70. if [ `echo $1 | grep ^$package_name` ]
  71. then
  72. src_package=${1%%.$class_name}
  73. else
  74. src_package=$src_pkg_pf.${1%%.$class_name}
  75. fi
  76. else
  77. src_package=$src_pkg_pf
  78. fi
  79. else
  80. if [ `echo $1 | grep '\.'` ]
  81. then
  82. src_package=${1%%.$class_name}
  83. else
  84. src_package=$package_name
  85. fi
  86. fi
  87. export class_name
  88. if [ `echo $2 | grep groovy` ]
  89. then
  90. filetype=groovy
  91. else
  92. filetype=java
  93. fi
  94. src_path=$project_directory/src/main/groovy/`pkg2path $src_package`
  95. src_file=$src_path/$class_name.$filetype
  96. if [ -s $src_file ]
  97. then
  98. echo "file $class_name already existed"
  99. exit 3
  100. fi
  101. mkdir -p $src_path
  102. export package_name=$src_package
  103. export import_text=
  104. cast $XMETA_HOME/templates/java/$2 > $src_file
  105. if [ `echo $2 | grep ^class` ]
  106. then
  107. test_path=$project_directory/src/test/groovy/`pkg2path $src_package`
  108. test_file=$test_path/${class_name}Test.groovy
  109. if [ -s $test_file ]
  110. then
  111. echo "Warning: created class $class_name, but test file already exists."
  112. fi
  113. mkdir -p $test_path
  114. cast $XMETA_HOME/templates/java/junit.groovy > $test_file
  115. fi
  116. # End of script