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

sfembed 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. #
  6. usage() {
  7. mkusage "script"
  8. }
  9. chk_applicable() {
  10. #
  11. # Check if script file is properly formed
  12. #
  13. # Not all scripts are applicable for embedding. Check
  14. # if the file is usable.
  15. #
  16. :
  17. #TODO: for starters, file must only contain shellfu header, imports, main() and nothing else
  18. }
  19. mkbody() {
  20. debug -v ScriptFullPath
  21. mkhead || return $?
  22. mkmodules || return $?
  23. mkpayload || return $?
  24. mkfoot || return $?
  25. }
  26. mkmodules() {
  27. #
  28. # Print each module body w/ header
  29. #
  30. local modlist=__shellfu_embed__modlist
  31. local Module
  32. local ModuleHash
  33. (
  34. SHELLFU_EMBEDTMP="$modlist" "$ScriptFullPath" >&2
  35. )
  36. #shellcheck disable=SC2030
  37. awk '!x[$0]++' "$modlist" \
  38. | while read -r Module;
  39. do
  40. ModuleHash=$(md5sum "$Module")
  41. think "appending: $Module"
  42. mkmodule "$Module"
  43. done
  44. }
  45. mkpayload() {
  46. cat "$ScriptFullPath"
  47. }
  48. mkfoot() {
  49. echo "#"
  50. echo "# end of shellfu-embedded shell script"
  51. echo "#"
  52. }
  53. #shellcheck disable=SC2031
  54. mkmodule() {
  55. #
  56. # Print single module body
  57. #
  58. echo "#"
  59. echo "# begin module: $ModuleHash"
  60. echo "#"
  61. echo
  62. cat "$Module"
  63. echo
  64. echo "#"
  65. echo "# end module: $ModuleHash"
  66. echo "#"
  67. echo
  68. }
  69. mkhead() {
  70. #
  71. # Print header of the final script
  72. #
  73. echo "#!/bin/bash"
  74. echo "#"
  75. echo "# shellfu embedded: $ScriptFullPath"
  76. echo "# shellfu version: $SHELLFU_VERSION"
  77. echo "#"
  78. echo
  79. echo "# neuter any shellfu and sfpath calls"
  80. echo "shellfu() { :; }"
  81. echo "sfpath() { echo /dev/null; }"
  82. #FIXME: assumes that shellfu and sfpath are not installed
  83. # I.e. beats the use case when embedding is done to
  84. # avoid conflict.
  85. echo
  86. }
  87. do_embed() {
  88. local tmp # temporary dir to run in
  89. local es=0 # exit status of this function
  90. local ScriptFullPath # full path to script to embed in
  91. tmp=$(mktemp -d -t shellfu_embed.XXXXXXXX)
  92. ScriptFullPath=$(readlink -f "$Script")
  93. pushd "$tmp" >/dev/null
  94. mkbody; es=$?
  95. popd >/dev/null
  96. rm -r "$tmp"
  97. return $es
  98. }
  99. main() {
  100. local Script # script to embed libraries into
  101. #shellcheck disable=SC2034
  102. while true; do case "$1" in
  103. -d) PRETTY_DEBUG=true; shift ;;
  104. -v) PRETTY_VERBOSE=true; shift ;;
  105. -*) usage ;;
  106. *) break ;;
  107. esac done
  108. Script="$1"
  109. test -n "$Script" || usage
  110. test -f "$Script" || die "no such file: $Script"
  111. test -r "$Script" || die "cannot read: $Script"
  112. chk_applicable "$Script" || die "not applicable for embedding: $Script"
  113. debug -v Script
  114. do_embed
  115. }
  116. main "$@"