mkx 4.6KB

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