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

shellfu.sh.skel 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/bin/sh
  2. #shellcheck disable=SC2039,SC1090
  3. #
  4. # Shell compatibility
  5. #
  6. # This should be the binary name of the shell we're running and is used to
  7. # choose where to look for modules
  8. #
  9. SHELLFU_COMPAT=${SHELLFU_COMPAT:-$(ps -p$$ -ocmd= | cut -d' ' -f1 | rev | cut -d/ -f1 | rev)}
  10. #
  11. # Our installation directory
  12. #
  13. SHELLFU_DIR=${SHELLFU_DIR:-__SHELLFU_DIR__}
  14. #
  15. # Shellfu internals debug mode
  16. #
  17. SHELLFU_DEBUGINIT=${SHELLFU_DEBUGINIT:-false}
  18. #
  19. # Target to list imported functions in embedding mode
  20. #
  21. SHELLFU_EMBEDTMP=${SHELLFU_EMBEDTMP:-}
  22. #
  23. # True if running in embedded mode (imports do nothing)
  24. #
  25. SHELLFU_EMBEDDED=${SHELLFU_EMBEDDED:-false}
  26. #
  27. # Last git commit hash before build
  28. #
  29. SHELLFU_GIT_HASH=__MKIT_PROJ_GIT_LASTHASH__
  30. #
  31. # Includes folder prefix
  32. #
  33. # Modules are searched for in path formed by this prefix, plus
  34. # 'sh', plus run-time value of SHELLFU_COMPAT, and if set, in
  35. # directories listed in SHELLFU_PATH
  36. #
  37. SHELLFU_INCLUDE=${SHELLFU_INCLUDE:-$SHELLFU_DIR/include}
  38. #
  39. # Colon separated list of additional paths to search modules
  40. #
  41. SHELLFU_PATH=${SHELLFU_PATH:-}
  42. #
  43. # Shellfu version (SemVer 2.0)
  44. #
  45. SHELLFU_VERSION=__MKIT_PROJ_VERSION__
  46. #
  47. # Colon-separated list of already imported modules
  48. #
  49. __SHELLFU_IMPORTED=${__SHELLFU_IMPORTED:-}
  50. shellfu() {
  51. local cmd=$1; shift
  52. local msg
  53. # debug on import
  54. case $SHELLFU_DEBUGINIT:$1 in
  55. true:import)
  56. shellfu __debug "\$*='$*'"
  57. shellfu __debug "SHELLFU_COMPAT='$SHELLFU_COMPAT'"
  58. shellfu __debug "SHELLFU_DIR='$SHELLFU_DIR'"
  59. shellfu __debug "SHELLFU_DEBUGINIT='$SHELLFU_DEBUGINIT'"
  60. shellfu __debug "SHELLFU_EMBEDDED='$SHELLFU_EMBEDDED'"
  61. shellfu __debug "SHELLFU_EMBEDTMP='$SHELLFU_EMBEDTMP'"
  62. shellfu __debug "SHELLFU_GIT_HASH='$SHELLFU_GIT_HASH'"
  63. shellfu __debug "__SHELLFU_IMPORTED='$__SHELLFU_IMPORTED'"
  64. shellfu __debug "SHELLFU_INCLUDE='$SHELLFU_INCLUDE'"
  65. shellfu __debug "SHELLFU_PATH='$SHELLFU_PATH'"
  66. shellfu __debug "SHELLFU_VERSION='$SHELLFU_VERSION'"
  67. ;;
  68. esac
  69. case $cmd in
  70. __debug)
  71. $SHELLFU_DEBUGINIT || return 0
  72. for msg in "$@";
  73. do
  74. echo "shellfu:debug: $msg" >&2
  75. done
  76. ;;
  77. __die)
  78. for msg in "$@";
  79. do
  80. echo "shellfu:fatal: $msg" >&2
  81. done
  82. test -z "$PS1" && exit 3
  83. return 3
  84. ;;
  85. __warn)
  86. for msg in "$@";
  87. do
  88. echo "shellfu: $msg" >&2
  89. done
  90. ;;
  91. import)
  92. #
  93. # Import module named $1
  94. #
  95. local mname=$1
  96. shellfu _is_imported "$mname" && return 0
  97. if shellfu _do_import "$mname";
  98. then
  99. __SHELLFU_IMPORTED="$__SHELLFU_IMPORTED${__SHELLFU_IMPORTED:+:}$mname"
  100. else
  101. shellfu __die "cannot import module: $mname"
  102. fi
  103. ;;
  104. try_import)
  105. #
  106. # Try if module $1 could be imported
  107. #
  108. local mname=$1
  109. ( shellfu _do_import "$mname" )
  110. ;;
  111. version)
  112. #
  113. # Show version of module named $1
  114. #
  115. local mname=$1
  116. mpath=$(shellfu _select_mfile "$mname") || {
  117. shellfu __warn "cannot find module: $mname"
  118. return 3
  119. }
  120. shellfu __debug "found module file: $mpath"
  121. tail -3 "$mpath" \
  122. | grep '^#shellfu *module-version:' \
  123. | sed 's/.*: *//; s/ *$//' \
  124. | grep . \
  125. || {
  126. shellfu __warn "missing version declaration: $mpath"
  127. return 2
  128. }
  129. ;;
  130. _do_import)
  131. #
  132. # Really import module named $1
  133. #
  134. $SHELLFU_EMBEDDED && return 0
  135. local mname=$1
  136. local es=4
  137. local mpath
  138. shellfu __debug "importing module $mname"
  139. mpath=$(shellfu _select_mfile "$mname") || {
  140. shellfu __warn "cannot find module: $mname"
  141. return 3
  142. }
  143. shellfu __debug "found module file: $mpath"
  144. bash -n "$mpath" || return 3
  145. . "$mpath"
  146. es=$?
  147. test -n "$SHELLFU_EMBEDTMP" && echo "$mpath" >> "$SHELLFU_EMBEDTMP"
  148. local init=__shellfu_${mname}__init
  149. if test "$(command -v "$init")" = "$init";
  150. then
  151. shellfu __debug "launching init function: $init"
  152. $init
  153. es=$?
  154. fi
  155. return $es
  156. ;;
  157. _is_imported)
  158. #
  159. # True if module $1 is already imported
  160. #
  161. local mname=$1
  162. echo "$__SHELLFU_IMPORTED" | tr : \\n | grep -qx "$mname"
  163. ;;
  164. _list_mfiles)
  165. #
  166. # Find module files of module matching $1 (wildcard expression)
  167. #
  168. local ex="${1:-*}"
  169. local incdir
  170. echo "$SHELLFU_INCLUDE-sh:$SHELLFU_INCLUDE-$SHELLFU_COMPAT:$SHELLFU_PATH" \
  171. | tr ":" '\n' \
  172. | awk '!seen[$0]++' \
  173. | while read -r incdir;
  174. do
  175. shellfu __debug "checking: $incdir"
  176. test -d "$incdir" || continue
  177. shellfu __debug "looking into: $incdir"
  178. find "$incdir" -name "$ex.sh"
  179. done
  180. ;;
  181. _select_mfile)
  182. #
  183. # Select file that contains module named $1
  184. #
  185. shellfu _list_mfiles "$1" | grep -m1 .
  186. ;;
  187. *)
  188. shellfu __die "unknown shellfu command: $cmd"
  189. ;;
  190. esac
  191. }