Skip to content

Instantly share code, notes, and snippets.

@olls
Last active October 6, 2018 15:18
Show Gist options
  • Save olls/0a0344b71537b84d005d to your computer and use it in GitHub Desktop.
Save olls/0a0344b71537b84d005d to your computer and use it in GitHub Desktop.
A script to run my build in the terminal from anywhere!
#!/bin/bash
function build-and-run {
build "$1" && $2
}
function build {
clear
# run $1, pipe output to stdout then strip colour and put in .build_results
$1 2>&1 | tee >(perl -np -e 's/\x1b\[[0-9;]*m//g' > .build_results)
RESULT=${PIPESTATUS[0]}
return $RESULT
}
function make-listener {
trap "build \"$1\" \"$2\"" SIGUSR1
trap "build-and-run \"$1\" \"$2\"" SIGUSR2
echo "Build command: " $1
echo "Run command: " $2
while true
do
read
done
}
function wait-for-results {
if [ -x "$(command -v inotifywait)" ]; then
while read i; do if [ "$i" = .build_results ]; then break; fi; done \
< <(inotifywait -e close_write --format '%f' --quiet ./ --monitor)
else
fswatch --event=Updated --include=.build_results --one-event ./ > /dev/null
fi
cat .build_results
}
if [ "$1" == "listen" ]
then
echo $$ > .listener_PID
trap "rm .listener_PID; rm .build_results; exit" SIGHUP SIGINT SIGTERM
make-listener "$2" "$3"
elif [ "$1" == "run" ]
then
rm .build_results
kill -SIGUSR2 $(cat .listener_PID)
wait-for-results
else
rm .build_results
kill -SIGUSR1 $(cat .listener_PID)
wait-for-results
fi
{
"shell_cmd": "make-and-run",
"working_dir": "${project_path}",
"file_regex": "^\\s*(?:\\.\\.\\/\\.\\.\\/)([^:\\s]*):(\\d+):(\\d+): ([^\\n]+)",
"variants": [
{
"name": "Run",
"shell_cmd": "make-and-run run"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment