2023-10-15 20:56:16 -04:00
|
|
|
#!/bin/bash
|
2023-10-16 15:17:21 -04:00
|
|
|
|
2024-03-02 02:48:19 -05:00
|
|
|
# Originally written by Anon, modified by Emil to better match Bake functionality
|
|
|
|
|
|
|
|
# Issues: sloooow, fails to handle multi-line statements
|
2023-10-16 15:17:21 -04:00
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
BLUE='\033[34m'
|
|
|
|
GREEN='\033[32m'
|
2024-03-02 02:48:19 -05:00
|
|
|
YELLOW='\033[93m'
|
|
|
|
DIM='\033[2m'
|
2023-10-15 20:56:16 -04:00
|
|
|
BOLD='\033[1m'
|
|
|
|
NORMAL='\033[0m'
|
2023-10-16 03:53:44 -04:00
|
|
|
|
2023-11-12 23:35:46 -05:00
|
|
|
MARKNAME="@BAKE"
|
2023-10-16 03:53:44 -04:00
|
|
|
MARK="${MARKNAME} "
|
2024-03-02 02:48:19 -05:00
|
|
|
MARKSTR="${GREEN}${MARKNAME}${NORMAL}"
|
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
enable -n echo
|
2024-03-02 02:48:19 -05:00
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
usage() {
|
2024-03-02 02:48:19 -05:00
|
|
|
echo -e "$0: [option] ${BOLD}target-file${NORMAL} [${GREEN}arguments${NORMAL} ...]\n"
|
|
|
|
echo -e "Use the format \`${BOLD}@BAKE${NORMAL} cmd ...' within the ${BOLD}target-file${NORMAL}."
|
|
|
|
echo -e "This will execute until the end of line, or if existing, until the ${BOLD}@STOP${NORMAL} marker.\n"
|
|
|
|
echo -e "Options [Must be first]"
|
|
|
|
echo -e "\t${DIM}-h --help${NORMAL}, ${BOLD}-n --dry-run${NORMAL}\n"
|
|
|
|
echo -e "Expansions\n"
|
|
|
|
echo -e "\t${YELLOW}@FILENAME${NORMAL} returns target-file (abc.x.txt)"
|
|
|
|
echo -e "\t${YELLOW}@SHORT${NORMAL} returns target-file without suffix (^-> abc.x)"
|
|
|
|
echo -e "\t${YELLOW}@ARGS${NORMAL} returns ${GREEN}arguments${NORMAL}"
|
2023-10-15 20:56:16 -04:00
|
|
|
}
|
2023-10-16 03:53:44 -04:00
|
|
|
|
|
|
|
if [[ $# -lt 1 ]]; then
|
2023-10-15 20:56:16 -04:00
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
2024-03-02 02:48:19 -05:00
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
fi
|
2023-10-16 03:53:44 -04:00
|
|
|
|
2023-10-16 18:00:21 -04:00
|
|
|
run=1
|
|
|
|
if [[ $1 == "-n" ]] || [[ $1 == "--dry-run" ]]; then
|
|
|
|
if [[ $# -lt 2 ]]; then
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
run=0
|
|
|
|
shift 1
|
|
|
|
fi
|
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
input_file=$1
|
2023-10-16 03:53:44 -04:00
|
|
|
shift 1
|
2024-03-02 02:48:19 -05:00
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
if [[ ! -f $input_file ]]; then
|
|
|
|
echo -e "Input file '$input_file' does not exist." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-10-16 03:53:44 -04:00
|
|
|
|
2024-03-02 02:48:19 -05:00
|
|
|
cd $(dirname "$(readlink -f "$input_file")")
|
|
|
|
input_file=${input_file##*/}
|
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
line=$(grep "$MARK" "$input_file" | head -1)
|
2023-10-16 03:53:44 -04:00
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
if [[ -n $line ]]; then
|
2024-03-02 02:48:19 -05:00
|
|
|
|
|
|
|
line=${line//\$@/$input_file}
|
|
|
|
line=${line//\$\*/${input_file%.*}}
|
|
|
|
line=${line//\$+/$@}
|
|
|
|
line=${line//@FILENAME/$input_file}
|
|
|
|
line=${line//@SHORT/${input_file%.*}}
|
|
|
|
line=${line//@ARGS/$@}
|
|
|
|
line=$(echo "$line" | sed 's/\W*@STOP.*//')
|
|
|
|
|
2023-10-16 03:53:44 -04:00
|
|
|
command="${line#*${MARK}}"
|
2024-03-02 02:48:19 -05:00
|
|
|
echo -e "${BOLD}${GREEN}$0${NORMAL}: $command"
|
2023-10-16 18:00:21 -04:00
|
|
|
if [[ $run -eq 1 ]]; then
|
2024-03-02 02:48:19 -05:00
|
|
|
echo -e "${BOLD}${GREEN}output${NORMAL}:"
|
2024-01-24 09:00:46 -05:00
|
|
|
sh -c "$command"
|
2023-10-16 18:00:21 -04:00
|
|
|
fi
|
2023-10-15 20:56:16 -04:00
|
|
|
else
|
|
|
|
echo -e "${MARKSTR} is not defined." >&2
|
2024-02-14 13:17:35 -05:00
|
|
|
exit 1
|
2023-10-15 20:56:16 -04:00
|
|
|
fi
|