123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/bin/bash
  2. usage() {
  3. local self
  4. self="$(basename "$0")"
  5. warn "usage: $self [-f|--force] [+TEMPLATE] FILE"
  6. warn "usage: $self -l|--list"
  7. exit 2
  8. }
  9. warn() {
  10. #
  11. # Print warning
  12. #
  13. echo "$@" >&2
  14. }
  15. die() {
  16. #
  17. # Warn and exit prematurely
  18. #
  19. warn "$@"
  20. exit 3
  21. }
  22. lstemplates() {
  23. #
  24. # Print list of valid templates
  25. #
  26. echo pya
  27. echo shellfu
  28. echo Bash
  29. echo Lua
  30. echo Perl
  31. echo py3
  32. echo Python3
  33. echo Python
  34. }
  35. #shellcheck disable=SC2016
  36. mktemplate() {
  37. #
  38. # Print template $Template for $FilePath
  39. #
  40. case "$Template" in
  41. sh)
  42. echo "#!/bin/sh"
  43. echo ''
  44. echo ''
  45. ;;
  46. bash|Bash)
  47. echo "#!/bin/bash"
  48. echo ''
  49. echo ''
  50. ;;
  51. lua|Lua)
  52. echo "#!$(which lua)"
  53. echo ''
  54. ;;
  55. pl|Perl)
  56. echo "#!$(which perl)"
  57. echo ''
  58. echo 'use strict;'
  59. echo 'use warnings;'
  60. echo ''
  61. echo ''
  62. ;;
  63. py|Python)
  64. echo "#!$(which python)"
  65. echo ''
  66. echo "if __name__ == '__main__':"
  67. echo ' '
  68. ;;
  69. py3|Python3)
  70. echo "#!/usr/bin/python3"
  71. echo ''
  72. echo "if __name__ == '__main__':"
  73. echo ' '
  74. ;;
  75. pya)
  76. echo '#!/usr/bin/python'
  77. echo '"""'
  78. echo 'A simple application'
  79. echo '"""'
  80. echo ''
  81. echo 'import sys'
  82. echo ''
  83. echo ''
  84. echo 'class App(object):'
  85. echo ' """'
  86. echo ' Main application class'
  87. echo ' """'
  88. echo ''
  89. echo ' def __init__(self, argv):'
  90. echo ' self.argv = argv'
  91. echo ' # agument decoding and further initializations'
  92. echo ''
  93. echo ' @classmethod'
  94. echo ' def main(cls, argv):'
  95. echo ' """'
  96. echo ' Initialize and launch'
  97. echo ' """'
  98. echo ' app = cls(argv)'
  99. echo ' app.run()'
  100. echo ''
  101. echo ' def run(self):'
  102. echo ' """'
  103. echo ' Run the application'
  104. echo ' """'
  105. echo ' # actual action (calling other methods)'
  106. echo ' print self.argv'
  107. echo ' print __doc__'
  108. echo ''
  109. echo ''
  110. echo 'if __name__ == '__main__':'
  111. echo ' App.main(sys.argv)'
  112. ;;
  113. shellfu)
  114. echo "#!/bin/bash"
  115. echo ""
  116. echo '#shellcheck disable=SC1090'
  117. echo '. "$(sfpath)" || exit 3'
  118. echo ''
  119. echo 'shellfu import pretty'
  120. echo ''
  121. echo 'usage() {'
  122. echo ' mkusage "$@" "[-d] [-v] ARG..."'
  123. echo '}'
  124. echo ''
  125. echo 'main() {'
  126. echo ' while true; do case $1 in'
  127. echo ' -d) PRETTY_DEBUG=true; shift ;;'
  128. echo ' -v) PRETTY_VERBOSE=true; shift ;;'
  129. echo ' -*) usage -w "unknown argument: $1" ;;'
  130. echo ' *) break ;;'
  131. echo ' esac done'
  132. echo '}'
  133. echo ''
  134. echo 'main "$@"'
  135. ;;
  136. *)
  137. die "unknown template or language: $Template"
  138. ;;
  139. esac
  140. }
  141. guess_language() {
  142. #
  143. # Guess intended language from filepath $FilePath
  144. #
  145. local fname # file name
  146. fname="$(basename "$FilePath")"
  147. case "$fname" in
  148. *.*) echo "${fname##*.}" ;;
  149. *) echo "$MKX_DEFAULT_LANG" ;;
  150. esac
  151. }
  152. #
  153. # Default language
  154. #
  155. MKX_DEFAULT_LANG=${MKX_DEFAULT_LANG:-sh}
  156. main() {
  157. local FilePath # destination file path
  158. local Template # built-in template name
  159. local careful # ignore existing file
  160. careful=true
  161. while true; do case $1 in
  162. -f|--force) careful=false; shift ;;
  163. -l|--list) lstemplates; exit 0 ;;
  164. -*) usage ;;
  165. +*) Template="${1:1}"; shift ;;
  166. *) break ;;
  167. esac done
  168. FilePath="$1"
  169. test -n "$FilePath" || usage
  170. test -f "$FilePath" && $careful && die "file already exists: $FilePath"
  171. test -n "$Template" || Template=$(guess_language)
  172. mktemplate > "$FilePath"
  173. chmod +x "$FilePath"
  174. }
  175. main "$@"