#!/bin/bash
# dev/gen/bash - MCSH bash script source generation
set -e
######
# MCSH Bash Script CLI
gen_bash_dispatch() { lib_cmd_dispatch gen_bash "$@"; }
gen_bash_usage() {
cat <<USAGE
<cmd> ...
Source Generation Commands:
tool <name> <desc> [<lib>+] Generates a new tool
lib <name> <desc> [<lib>+] Generates a new library
license <blurb> <wrnty> <lic> Generates the 'license' library
Low-Level Source Generation Commands:
header <name> <desc> Prints standard library header code
func <name> Generates a function
here doc <tag> Generates here document
here func <tag> Generates here document function
USAGE
}
gen_bash_source() {
min_args 2 "$@"
local kind=$1
local name=$2
local desc=$3
local gen_edit=true
local gen_error_fail=true
local conf dir
case "$kind" in
(tool)
gen_bash_source "conf" "$name.conf" "$desc configuration"
conf="conf/$name.conf.in"
dir=src
;;
(conf)
gen_edit=false
gen_error_fail=false
dir=conf
;;
(library)
dir=libs
;;
esac
local file="$dir/$name.in"
[ ! -f "$file" ] || error "$file: exists"
gen_bash_source_file "$@" >"$file"
local -a files=( "$file" )
[ -z "$conf" ] || files+=( "$conf" )
! $gen_edit || run_editor "${files[@]}"
if [ ! -s "$file" ]; then
warn "$file: $kind empty... aborting"
run rm -f "${files[@]}"
fi
}
gen_bash_source_file() {
min_args 3 "$@"
local kind=$1
local name=$2
local desc=$3
shift 3
gen_bash_header "$name" "$desc"
if [ "$kind" = tool ]; then
echo
echo "source '__""runtime""__'"
fi
if [ "$*" ]; then
echo
for_each gen_bash_lib_load "$@"
fi
gen_bash_source_$kind "$name" "$desc"
}
gen_bash_lib_load() { echo "lib_load '$1'"; }
gen_bash_tool() { gen_bash_source tool "$@"; }
gen_bash_source_tool() {
local name=$1
local sym=$(lib_symbol "$name")
cat <<TOOL
######
# Script Dependencies
${sym}_client_packages=( )
${sym}_server_packages=( )
######
# Script Initialization
${sym}_init() {
: debug "${sym}_init called"
}
######
# Script Configuration
${sym}_config_init() {
: debug "${sym}_config_init called"
}
${sym}_config_check() {
: debug "${sym}_config_check called"
}
######
# CLI
${sym}_func() {
warn "$name has not been implemented"
}
######
# Check
${sym}_check() {
${sym}_func
}
######
# Main
${sym}_desc() { echo "$desc"; }
${sym}_usage() {
cat <<USAGE
...
$sym Commands:
func Tests the $name tool
USAGE
}
${sym}_help() {
cat <<HELP
$name is a $desc tool. It is part of the $package_name package.
HELP
}
app_run "\$@"
TOOL
}
gen_bash_source_conf() {
:
}
gen_bash_lib() { gen_bash_source library "$@"; }
gen_bash_source_library() {
local name=$1
local desc=$2
local sym=$(lib_symbol "$name")
cat <<LIB
######
# Native Dependencies
${sym}_client_packages=( )
${sym}_server_packages=( )
######
# Library Initialization
${sym}_lib_init() {
: debug "${sym}_lib_init called"
}
######
# Library Configuration
${sym}_config_init() {
: debug "${sym}_config_init called"
}
${sym}_config_check() {
: debug "${sym}_config_check called"
}
######
# $sym CLI
${sym}_dispatch() { lib_cmd_dispatch ${sym} "\$@"; }
${sym}_usage() {
cat <<USAGE
...
Commands:
func Tests $name command dispatch
USAGE
}
######
# Public Interface
${sym}_func() { warn "$name library is not implemented"; }
LIB
}
gen_bash_license() {
has_args 4 "$@"
gen_bash_header "license-parts" "$1 licence details"
gen_bash_here_func license_blurb_print <"$2"
gen_bash_here_func license_warranty_print <"$3"
gen_bash_here_func license_full_print <"$4"
}
gen_bash_header() {
has_args 2 "$@"
local name=$1
local desc=$2
cat <<EOF
#!/bin/bash
# $name - $desc
EOF
$gen_error_fail || return 0
echo
echo "set -e"
}
gen_bash_here_func() {
has_args 1 "$@"
local name=$1
local -u tag="${name}_data"
local pipe_args=( '|' base64 -d '|' gzip -dc )
gzip -c | base64 \
| gen_bash_here_doc "$tag" "${pipe_args[@]}" \
| gen_bash_func $name
}
gen_bash_func() {
local name=$1
echo "$name() {"
cat -
echo "}"
}
gen_bash_here_doc() {
local tag=$1
shift
echo "cat <<$tag" "$@"
cat -
echo "$tag"
}
Generated on Tue Apr 25 21:20:29 PDT 2017 by mcsh i7 v0.18.0.