Skip to content

Instantly share code, notes, and snippets.

@nilium
Last active August 31, 2018 23:04
Show Gist options
  • Save nilium/a3eea06ea01f830c1396f5b26b3d1cc0 to your computer and use it in GitHub Desktop.
Save nilium/a3eea06ea01f830c1396f5b26b3d1cc0 to your computer and use it in GitHub Desktop.
Lazy script to run ag and entr in conjunction with a series of commands.
#!/usr/bin/env bash
prog="$(basename "$0")"
want() {
for bin; do
if ! hash "$bin" 1>/dev/null 2>&1; then
echo "$bin: not installed" 1>&2
exit 1
fi
done
}
want ag entr
usage() {
exec 1>&2
cat 1>&2 <<EOF
${prog} [TYPE...] :: CMD[, CMD...]
TYPE
One of the type names understood by ag (e.g., go, c, md, erl). May
also be used to pass long arguments to ag (such as 'erl
skip-vcs-ignores') as these are passed directly to ag with '--'
prefixes.
If no type is given, then all files will be listed.
CMD
One or more comma-and-space separated commands to execute if a file is
changed. Commands are executed in a new shell.
Comma-separated commands such as 'CMD1, CMD2, CMD3' are executed as
'CMD1 && CMD2 && CMD3'.
Strings are passed verbatim (after expansion from the shell) to the
subshell for commands, so special characters will require
double-quoting to escape them (e..g, "'>'" to pass a > as text).
Commas can be escaped by writing a second comma (e.g., 'x,,').
EOF
exit 2
}
entrargs=()
agargs=()
case "$1" in
-h|-help|--help) usage;;
esac
while [ $# -gt 0 ]; do
if [[ "$1" =~ ^-[cdnprs]+$ ]]; then
entrargs[${#entrargs[@]}]="$1"
shift
continue
fi
case "$1" in
::)
break
;;
'')
shift
;;
--*)
agargs[${#agargs[@]}]="$1"
shift
;;
[a-z0-9]*)
agargs[${#agargs[@]}]="--$1"
shift
;;
esac
done
if [ $# -eq 1 ]; then
echo "$prog: no command(s) found after separator (::)" 1>&2
exit 1
elif [ $# -eq 0 ]; then
echo "$prog: no separator (::) found" 1>&2
exit 1
fi
shift
argv=()
for arg; do
case "$arg" in
*,,) argv=("${argv[@]}" "${arg%,}");;
,) argv=("${argv[@]}" '&&');;
*,) argv=("${argv[@]}" "${arg%,}" '&&');;
*) argv=("${argv[@]}" "${arg}");;
esac
done
# Default to bash is no shell set
: "${SHELL:=bash}"
while :; do
ag "${agargs[@]}" -l | entr "${entrargs[@]}" "$SHELL" -c "${argv[*]}" "$SHELL"
sleep 1 || break
done
@nilium
Copy link
Author

nilium commented Aug 15, 2018

Quick examples:

# Run go tests and build(s)
$ agentr go :: go test ./..., go build ./cmd/foo

# Run eunit and dialyzer when erlang sources change (uses comma escaping)
$ agentr erl :: rebar3 do eunit,, dialyzer

# Rebuild HTML for markdown
$ agentr md :: kramdown README.md \> README.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment