Skip to content

Instantly share code, notes, and snippets.

@IanVaughan
Created January 3, 2012 18:57
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 IanVaughan/1556313 to your computer and use it in GitHub Desktop.
Save IanVaughan/1556313 to your computer and use it in GitHub Desktop.
Update to monitor.sh, this time its serious! This monitors a group of directories, and if a file in one of them changes it only actions that directory command.
#!/bin/bash
# Update to monitor.sh, this time its serious!
# This monitors a group of directories for source file changes, and if a file in one of them changes it only actions that directory command.
# It does this via creating a SHA of the ls tree of the folder in question, and if changed, triggers the action.
# The "action" is just the variable $command, normally make.
# Usage :
# ./monitor.sh
# Then save a file, and it gets built! Magic!
# Pass in anything to use on the target
commands=("make clean" "make" "./load")
mode=0
if [ $1 ]; then
echo "--> Monitor: " $*
mode=1
fi
# allow make commands to be passed to each make
# eg monitor.sh wc -> to go thr each wc
# as well as "install" to make clean && ...
# could get automaticly? but order might be important, and might have dirs not wanted to build
#DIRS=`ls`
DIRS=('driver' 'lib' 'test')
declare -a sha
declare -a previous_sha
has_changed=0
update_sha()
{
sha[$1]=`ls -lR . | egrep '\.c|\.cpp|\.h|\.spt' | sha1sum`
}
build () {
#echo "--> Monitor: Building... | ${DIRS[$1]} |"
echo "------| ${DIRS[$1]} |----------------------------------------------------------------------------------------------------------"
if [ $mode == 0 ]; then
make clean && make && make update
fi
if [ $mode == 1 ]; then
sudo sh -c "make clean && make && make install"
fi
#echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)"
}
changed () {
build $1
previous_sha[$1]=${sha[$1]}
has_changed=1
}
compare () {
update_sha $1
[[ ${sha[$1]} != ${previous_sha[$1]} ]] && changed $1
}
check () {
if [ -f .updated ]; then
if [ `cat .updated` != 0 ]; then
echo 0 > .updated
build
fi
fi
}
run () {
while true; do
for index in ${!DIRS[*]}; do
cd ${DIRS[$index]}
compare $index
cd ..
done
if [ $has_changed == 1 ]; then
echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)"
has_changed=0
fi
read -s -t 1 && (
echo -e "\n--> Monitor: Forced Update <-- \n"
for index in ${!DIRS[*]}; do
cd ${DIRS[$index]}
build $index
cd ..
done
echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)"
)
done
}
echo "--> Monitor: Init..."
echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)"
run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment