98 lines
2.6 KiB
Bash
Executable File
98 lines
2.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Originally written by Anon, modified by Emil to better match Bake functionality
|
|
|
|
# Issues: sloooow, fails to handle multi-line statements
|
|
|
|
VERSION="20240408"
|
|
|
|
BLUE='\033[34m'
|
|
GREEN='\033[32m'
|
|
YELLOW='\033[93m'
|
|
DIM='\033[2m'
|
|
BOLD='\033[1m'
|
|
NORMAL='\033[0m'
|
|
|
|
MARKNAME="@BAKE"
|
|
MARK="${MARKNAME} "
|
|
MARKSTR="${GREEN}${MARKNAME}${NORMAL}"
|
|
|
|
enable -n echo
|
|
|
|
usage() {
|
|
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}"
|
|
echo -e "\t${YELLOW}@{${NORMAL}${BOLD}EXPUNGE_THIS_FILE${YELLOW}}${NORMAL} inline region to delete this or many files or directories,"
|
|
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"
|
|
}
|
|
|
|
if [[ $# -lt 1 ]]; then
|
|
usage
|
|
exit 1
|
|
fi
|
|
|
|
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
|
|
usage
|
|
exit 0
|
|
fi
|
|
|
|
if [[ $1 == "-v" ]] || [[ $1 == "--version" ]]; then
|
|
echo -e "$0: $VERSION"
|
|
exit 0
|
|
fi
|
|
|
|
run=1
|
|
if [[ $1 == "-n" ]] || [[ $1 == "--dry-run" ]]; then
|
|
if [[ $# -lt 2 ]]; then
|
|
usage
|
|
exit 1
|
|
fi
|
|
run=0
|
|
shift 1
|
|
fi
|
|
|
|
input_file=$1
|
|
shift 1
|
|
|
|
if [[ ! -f $input_file ]]; then
|
|
echo -e "Input file '$input_file' does not exist." >&2
|
|
exit 1
|
|
fi
|
|
|
|
cd $(dirname "$(readlink -f "$input_file")")
|
|
input_file=${input_file##*/}
|
|
|
|
line=$(grep "$MARK" "$input_file" | head -1)
|
|
|
|
if [[ -n $line ]]; then
|
|
|
|
line=${line//\$@/$input_file}
|
|
line=${line//\$\*/${input_file%.*}}
|
|
line=${line//\$+/$@}
|
|
line=${line//@FILENAME/$input_file}
|
|
line=${line//@SHORT/${input_file%.*}}
|
|
line=${line//@ARGS/$@}
|
|
line=${line//@NAME/$input_file}
|
|
line=${line//@FILE/$input_file}
|
|
line=$(echo "$line" | sed 's/@STOP.*//')
|
|
|
|
echo -e "${BOLD}${GREEN}$0${NORMAL}: ${line#*${MARK}}"
|
|
|
|
line=$(echo "$line" | sed -E 's/@\{([^ \}]+?)\}/\1/')
|
|
command="${line#*${MARK}}"
|
|
if [[ $run -eq 1 ]]; then
|
|
echo -e "${BOLD}${GREEN}output${NORMAL}:"
|
|
sh -c "$command"
|
|
fi
|
|
else
|
|
echo -e "${MARKSTR} is not defined." >&2
|
|
exit 1
|
|
fi
|