Skip to content

Instantly share code, notes, and snippets.

@odashi
Last active February 24, 2019 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odashi/b2e6ee3349ad72697ea58f0fc9485069 to your computer and use it in GitHub Desktop.
Save odashi/b2e6ee3349ad72697ea58f0fc9485069 to your computer and use it in GitHub Desktop.
Performs a command when the content of a directory changed.
#!/bin/zsh
autoload colors; colors
if [ $# != 2 ]; then
echo "usage: $0 <root-dir> <command>"
exit 1
fi
ROOTDIR=$1
COMMAND=$2
INTERVAL=3
function separator() {
echo -n "$fg[blue]"
for i in $(seq 1 $COLUMNS); do
echo -n '─'
done
echo "$reset_color"
}
function message() {
echo "$bg_bold[blue] imake $reset_color $1"
}
function list_all_files() {
find $(realpath $1) \
-type d -path $(realpath .) -prune \
-or -name '.*' -prune \
-or -type f -print \
| sort
}
function get_hash() {
files=$(list_all_files $1)
# Encodes both list of file names and their contents.
(echo ${files}; echo ${files} | xargs cat) | openssl sha256
}
old_hash=$(get_hash ${ROOTDIR})
separator
message "waiting for changes."
while true; do
sleep ${INTERVAL}
new_hash=$(get_hash ${ROOTDIR})
if [ "${new_hash}" != "${old_hash}" ]; then
message "change detected."
eval ${COMMAND}
if [ $? = 0 ]; then
message "$fg[green]make succeeded.$reset_color"
else
message "$fg[red]make failed.$reset_color"
fi
old_hash=${new_hash}
separator
message "waiting for changes."
fi
done
#!/bin/zsh
autoload colors; colors
if [ $# != 2 ]; then
echo "usage: $0 <root-dir> <command>"
exit 1
fi
ROOTDIR=$1
COMMAND=$2
INTERVAL=3
function separator() {
echo -n "$fg[blue]"
for i in $(seq 1 $COLUMNS); do
echo -n '─'
done
echo "$reset_color"
}
function message() {
echo "$bg_bold[blue] imake $reset_color $1"
}
while true; do
separator
message "waiting for changes."
inotifywait -r ${ROOTDIR} \
-e CLOSE_WRITE -e MOVED_TO -e MOVED_FROM \
-e CREATE -e DELETE \
>/dev/null 2>/dev/null
eval ${COMMAND}
if [ $? = 0 ]; then
message "$fg[green]make succeeded.$reset_color"
else
message "$fg[red]make failed.$reset_color"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment