Ever burned a cake?
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.5KB

  1. #!/bin/bash
  2. # Originally written by Anon, modified by Emil to better match Bake functionality
  3. # Issues: sloooow, fails to handle multi-line statements
  4. VERSION="20240408"
  5. BLUE='\033[34m'
  6. GREEN='\033[32m'
  7. YELLOW='\033[93m'
  8. DIM='\033[2m'
  9. BOLD='\033[1m'
  10. NORMAL='\033[0m'
  11. MARKNAME="@BAKE"
  12. MARK="${MARKNAME} "
  13. MARKSTR="${GREEN}${MARKNAME}${NORMAL}"
  14. enable -n echo
  15. usage() {
  16. echo -e "$0: [option] ${BOLD}target-file${NORMAL} [${GREEN}arguments${NORMAL} ...]\n"
  17. echo -e "Use the format \`${BOLD}@BAKE${NORMAL} cmd ...' within the ${BOLD}target-file${NORMAL}."
  18. echo -e "This will execute until the end of line, or if existing, until the ${BOLD}@STOP${NORMAL} marker.\n"
  19. echo -e "Options [Must be first]"
  20. echo -e "\t${DIM}-h --help${NORMAL}, ${BOLD}-n --dry-run${NORMAL}\n"
  21. echo -e "Expansions\n"
  22. echo -e "\t${YELLOW}@FILENAME${NORMAL} returns target-file (abc.x.txt)"
  23. echo -e "\t${YELLOW}@SHORT${NORMAL} returns target-file without suffix (^-> abc.x)"
  24. echo -e "\t${YELLOW}@ARGS${NORMAL} returns ${GREEN}arguments${NORMAL}"
  25. echo -e "\t${YELLOW}@{${NORMAL}${BOLD}EXPUNGE_THIS_FILE${YELLOW}}${NORMAL} inline region to delete this or many files or directories,"
  26. echo -e "\tnon-recursive, only one file per block, removed from left to right. This has no\n\tinfluence on the normal command execution.\n"
  27. }
  28. if [[ $# -lt 1 ]]; then
  29. usage
  30. exit 1
  31. fi
  32. if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
  33. usage
  34. exit 0
  35. fi
  36. if [[ $1 == "-v" ]] || [[ $1 == "--version" ]]; then
  37. echo -e "$0: $VERSION"
  38. exit 0
  39. fi
  40. run=1
  41. if [[ $1 == "-n" ]] || [[ $1 == "--dry-run" ]]; then
  42. if [[ $# -lt 2 ]]; then
  43. usage
  44. exit 1
  45. fi
  46. run=0
  47. shift 1
  48. fi
  49. input_file=$1
  50. shift 1
  51. if [[ ! -f $input_file ]]; then
  52. echo -e "Input file '$input_file' does not exist." >&2
  53. exit 1
  54. fi
  55. cd $(dirname "$(readlink -f "$input_file")")
  56. input_file=${input_file##*/}
  57. line=$(grep "$MARK" "$input_file" | head -1)
  58. if [[ -n $line ]]; then
  59. line=${line//\$@/$input_file}
  60. line=${line//\$\*/${input_file%.*}}
  61. line=${line//\$+/$@}
  62. line=${line//@FILENAME/$input_file}
  63. line=${line//@SHORT/${input_file%.*}}
  64. line=${line//@ARGS/$@}
  65. line=$(echo "$line" | sed 's/@STOP.*//')
  66. echo -e "${BOLD}${GREEN}$0${NORMAL}: ${line#*${MARK}}"
  67. line=$(echo "$line" | sed -E 's/@\{([^ \}]+?)\}/\1/')
  68. command="${line#*${MARK}}"
  69. if [[ $run -eq 1 ]]; then
  70. echo -e "${BOLD}${GREEN}output${NORMAL}:"
  71. sh -c "$command"
  72. fi
  73. else
  74. echo -e "${MARKSTR} is not defined." >&2
  75. exit 1
  76. fi