shell dot on steroids https://pagure.io/shellfu

ffoo.sh.skel 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. FFOO_DIR=${FFOO_DIR:-__FFOO_DIR__}
  3. FFOO_DEBUG=${FFOO_DEBUG:-false}
  4. FFOO_DEBUGINIT=${FFOO_DEBUGINIT:-false}
  5. FFOO_INCLUDE=${FFOO_INCLUDE:-$FFOO_DIR/include}
  6. FFOO_MKPRETTY=${FFOO_MKPRETTY:-plain}
  7. FFOO_VERBOSE=${FFOO_VERBOSE:-false}
  8. FFOO_VERSION=__VERSION__
  9. ffoo() {
  10. case $1 in
  11. __debug)
  12. $FFOO_DEBUGINIT || return 0
  13. shift
  14. echo "debug: $@" >&2
  15. ;;
  16. __die)
  17. shift
  18. echo "$@" >&2
  19. test -z "$PS1" && exit 1
  20. ;;
  21. doc)
  22. ffoo _cat_function "$2" | ffoo _filter_doc
  23. ;;
  24. import)
  25. ffoo __debug "importing module $2"
  26. local mfile=$(ffoo _select_mfile $2)
  27. test -n "$mfile" || ffoo __die "cannot find module: $2"
  28. ffoo __debug "found module file: $mfile"
  29. bash -n $mfile && . $mfile
  30. return $?
  31. ;;
  32. _parse)
  33. # convert stdin from $1 to $2 (can be 1:N conversion)
  34. local src=$2
  35. local tgt=$3
  36. local line
  37. while read line;
  38. do
  39. ffoo _parse_${src}_to_${tgt} "$line"
  40. done
  41. ;;
  42. _parse_path_to_mod)
  43. local fname="${2##*/}"; echo "${fname%%.sh}"
  44. ;;
  45. _parse_fspec_to_mod)
  46. echo "${2%%.*}"
  47. ;;
  48. _parse_fspec_to_fun)
  49. local mod=$(ffoo _parse_fspec_to_mod "$2")
  50. echo "${2##$mod.}"
  51. ;;
  52. _parse_path_to_fspeclist)
  53. grep -HE '^[[:alnum:]_]+\(\) \{' "$2" \
  54. | sed -e 's/^.*\///; s/\.sh:/./; s/(.*//'
  55. ;;
  56. _parse_path_to_fnlist)
  57. grep -E '^[[:alnum:]_]+\(\) \{' "$2" \
  58. | sed -e 's/^.*\///; s/(.*//'
  59. ;;
  60. _cat_function)
  61. local mod=$(ffoo _parse_fspec_to_mod "$2")
  62. local fun=$(ffoo _parse_fspec_to_fun "$2")
  63. local mfname=$(ffoo _select_mfile "$mod")
  64. test -n "$mfname" || ffoo __die "unknown module: $mod"
  65. name=$fun perl -we '
  66. undef $/;
  67. my $name = $ENV{name};
  68. my ($fbody) = <> =~ m/^($name\(\) \{.*?^\}$)/ms;
  69. print "$fbody\n" if defined $fbody;
  70. ' < "$mfname"
  71. ;;
  72. _filter_doc)
  73. # 1. line "^ #$" opens doc
  74. # 2. first line that is not "^ #" closes doc
  75. # 3. first and last line will be stripped
  76. perl -we '
  77. my $isdoc;
  78. while (<>) {
  79. if (m/^ #$/) {
  80. $isdoc = 1;
  81. } elsif (m/^ [^#]/) {
  82. exit;
  83. }
  84. next unless $isdoc;
  85. s/ # ?//;
  86. print;
  87. }' \
  88. | head -n -1 \
  89. | tail -n +2
  90. ;;
  91. _list_modules)
  92. ffoo _list_mfiles "$2" | ffoo _parse path mod
  93. ;;
  94. _list_mfiles)
  95. local ex="${2:-*}"
  96. find $FFOO_INCLUDE ${FFOO_PATH/:/ } -name "$ex.sh"
  97. ;;
  98. _select_mfile)
  99. ffoo _list_mfiles $2 | head -1
  100. ;;
  101. _list_functions)
  102. ffoo _list_mfiles "$2" | ffoo _parse path fspeclist
  103. ;;
  104. _list_functions_in)
  105. ffoo _list_mfiles "$2" | ffoo _parse path fnlist
  106. ;;
  107. *)
  108. ffoo __die "unknown ffoo command: $1"
  109. ;;
  110. esac
  111. }
  112. ffoo __debug "FFOO_DIR='$FFOO_DIR'"
  113. ffoo __debug "FFOO_DEBUG='$FFOO_DEBUG'"
  114. ffoo __debug "FFOO_DEBUGINIT='$FFOO_DEBUGINIT'"
  115. ffoo __debug "FFOO_INCLUDE='$FFOO_INCLUDE'"
  116. ffoo __debug "FFOO_INI_PATH='$FFOO_INI_PATH'"
  117. ffoo __debug "FFOO_MKPRETTY='$FFOO_MKPRETTY'"
  118. ffoo __debug "FFOO_VERBOSE='$FFOO_VERBOSE'"
  119. ffoo __debug "FFOO_VERSION='$FFOO_VERSION'"