123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #!/bin/bash
-
-
- usage() {
- local self
- self="$(basename "$0")"
- warn "usage: $self [-f|--force] FILE [[+]TEMPLATE]"
- warn "usage: $self -l|--list"
- exit 2
- }
-
- warn() {
- #
- # Print warning
- #
- echo "$@" >&2
- }
-
- die() {
- #
- # Warn and exit prematurely
- #
- warn "$@"
- exit 3
- }
-
- lstemplates() {
- #
- # Print list of valid templates
- #
- echo +pya
- echo +shellfu
- echo Bash
- echo Lua
- echo Perl
- echo py3
- echo Python3
- echo Python
- }
-
- #shellcheck disable=SC2016
- mktemplate() {
- #
- # Print template $1 for $FilePath
- #
- # Template may be a name of built-in template (starting with
- # plus sign) or language name (actually *interpreter* name).
- #
- local tmplname="${1:-$Template}"
- case "$tmplname" in
- sh)
- echo "#!/bin/sh"
- echo ''
- echo ''
- ;;
- bash|Bash)
- echo "#!/bin/bash"
- echo ''
- echo ''
- ;;
- lua|Lua)
- echo "#!$(which lua)"
- echo ''
- ;;
- pl|Perl)
- echo "#!$(which perl)"
- echo ''
- echo 'use strict;'
- echo 'use warnings;'
- echo ''
- echo ''
- ;;
- py|Python)
- echo "#!$(which python)"
- echo ''
- echo "if __name__ == '__main__':"
- echo ' '
- ;;
- py3|Python3)
- echo "#!/usr/bin/python3"
- echo ''
- echo "if __name__ == '__main__':"
- echo ' '
- ;;
- +pya)
- echo '#!/usr/bin/python'
- echo '"""'
- echo 'A simple application'
- echo '"""'
- echo ''
- echo 'import sys'
- echo ''
- echo ''
- echo 'class App(object):'
- echo ' """'
- echo ' Main application class'
- echo ' """'
- echo ''
- echo ' def __init__(self, argv):'
- echo ' self.argv = argv'
- echo ' # agument decoding and further initializations'
- echo ''
- echo ' @classmethod'
- echo ' def main(cls, argv):'
- echo ' """'
- echo ' Initialize and launch'
- echo ' """'
- echo ' app = cls(argv)'
- echo ' app.run()'
- echo ''
- echo ' def run(self):'
- echo ' """'
- echo ' Run the application'
- echo ' """'
- echo ' # actual action (calling other methods)'
- echo ' print self.argv'
- echo ' print __doc__'
- echo ''
- echo ''
- echo 'if __name__ == '__main__':'
- echo ' App.main(sys.argv)'
- ;;
- +shellfu)
- echo "#!/bin/bash"
- echo ""
- echo '#shellcheck disable=SC1090'
- echo '. "$(sfpath)" || exit 3'
- echo ''
- echo 'shellfu import pretty'
- echo ''
- echo 'PRETTY_DEBUG=${PRETTY_DEBUG:-true}'
- echo ''
- echo 'usage() {'
- echo ' mkusage "arg..."'
- echo '}'
- echo ''
- echo 'main() {'
- echo ' while true; do case $1 in'
- echo ' -*) usage ;;'
- echo ' *) break ;;'
- echo ' esac done'
- echo '}'
- echo ''
- echo 'main "$@"'
- ;;
- "")
- mktemplate "$(guess_language)"
- ;;
- *)
- die "unknown template or language: $Template"
- ;;
- esac
- }
-
- guess_language() {
- #
- # Guess intended language from filepath $FilePath
- #
- local fname # file name
- fname="$(basename "$FilePath")"
- case "$fname" in
- *.*) echo "${fname##*.}" ;;
- *) echo "$MKX_DEFAULT_LANG" ;;
- esac
- }
-
-
- #
- # Default language
- #
- MKX_DEFAULT_LANG=${MKX_DEFAULT_LANG:-sh}
-
- main() {
- local FilePath # destination file path
- local Template # built-in template name
- local careful # ignore existing file
- careful=true
- while true; do case $1 in
- -f|--force) careful=false; shift ;;
- -l|--list) lstemplates; exit 0 ;;
- -*) usage ;;
- *) break ;;
- esac done
- FilePath="$1"
- Template="$2"
- test -n "$FilePath" || usage
- test -f "$FilePath" && $careful && die "file already exists: $FilePath"
- mktemplate > "$FilePath"
- chmod +x "$FilePath"
- }
-
- main "$@"
|