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.

92 lines
2.2KB

  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="20240302"
  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. }
  26. if [[ $# -lt 1 ]]; then
  27. usage
  28. exit 1
  29. fi
  30. if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
  31. usage
  32. exit 0
  33. fi
  34. if [[ $1 == "-v" ]] || [[ $1 == "--version" ]]; then
  35. echo -e "$0: $VERSION"
  36. exit 0
  37. fi
  38. run=1
  39. if [[ $1 == "-n" ]] || [[ $1 == "--dry-run" ]]; then
  40. if [[ $# -lt 2 ]]; then
  41. usage
  42. exit 1
  43. fi
  44. run=0
  45. shift 1
  46. fi
  47. input_file=$1
  48. shift 1
  49. if [[ ! -f $input_file ]]; then
  50. echo -e "Input file '$input_file' does not exist." >&2
  51. exit 1
  52. fi
  53. cd $(dirname "$(readlink -f "$input_file")")
  54. input_file=${input_file##*/}
  55. line=$(grep "$MARK" "$input_file" | head -1)
  56. if [[ -n $line ]]; then
  57. line=${line//\$@/$input_file}
  58. line=${line//\$\*/${input_file%.*}}
  59. line=${line//\$+/$@}
  60. line=${line//@FILENAME/$input_file}
  61. line=${line//@SHORT/${input_file%.*}}
  62. line=${line//@ARGS/$@}
  63. line=$(echo "$line" | sed 's/\W*@STOP.*//')
  64. command="${line#*${MARK}}"
  65. echo -e "${BOLD}${GREEN}$0${NORMAL}: $command"
  66. if [[ $run -eq 1 ]]; then
  67. echo -e "${BOLD}${GREEN}output${NORMAL}:"
  68. sh -c "$command"
  69. fi
  70. else
  71. echo -e "${MARKSTR} is not defined." >&2
  72. exit 1
  73. fi