mkx 4.6KB

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