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

shellfu.sh.skel 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. _do_import)
  112. #
  113. # Really import module named $1
  114. #
  115. $SHELLFU_EMBEDDED && return 0
  116. local mname=$1
  117. local es=4
  118. local mpath
  119. shellfu __debug "importing module $mname"
  120. mpath=$(shellfu _select_mfile "$mname") || {
  121. shellfu __warn "cannot find module: $mname"
  122. return 3
  123. }
  124. shellfu __debug "found module file: $mpath"
  125. bash -n "$mpath" || return 3
  126. . "$mpath"
  127. es=$?
  128. test -n "$SHELLFU_EMBEDTMP" && echo "$mpath" >> "$SHELLFU_EMBEDTMP"
  129. local init=__shellfu_${mname}__init
  130. if test "$(command -v "$init")" = "$init";
  131. then
  132. shellfu __debug "launching init function: $init"
  133. $init
  134. es=$?
  135. fi
  136. return $es
  137. ;;
  138. _is_imported)
  139. #
  140. # True if module $1 is already imported
  141. #
  142. local mname=$1
  143. echo "$__SHELLFU_IMPORTED" | tr : \\n | grep -qx "$mname"
  144. ;;
  145. _list_mfiles)
  146. #
  147. # Find module files of module matching $1 (wildcard expression)
  148. #
  149. local ex="${1:-*}"
  150. local incdir
  151. echo "$SHELLFU_INCLUDE-sh:$SHELLFU_INCLUDE-$SHELLFU_COMPAT:$SHELLFU_PATH" \
  152. | tr ":" '\n' \
  153. | awk '!seen[$0]++' \
  154. | while read -r incdir;
  155. do
  156. shellfu __debug "checking: $incdir"
  157. test -d "$incdir" || continue
  158. shellfu __debug "looking into: $incdir"
  159. find "$incdir" -name "$ex.sh"
  160. done
  161. ;;
  162. _select_mfile)
  163. #
  164. # Select file that contains module named $1
  165. #
  166. shellfu _list_mfiles "$1" | grep -m1 .
  167. ;;
  168. *)
  169. shellfu __die "unknown shellfu command: $cmd"
  170. ;;
  171. esac
  172. }