Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 122 lines (106 sloc) 2.447 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
# Observe
#
# Utility for observing paths and executing a given command when any files are
# modified, i.e. run unit tests.
#
# Implemented by Ole Petter Bang <olepbang@gmail.com>

observe()
{
  PATHS=""
  while [ $# -gt 0 ]; do
case $1 in

      "-e")
        if [ $# -lt 2 ]; then
echo "Missing command after -e parameter."
          return
fi
shift
CMD=$1
        echo "Executing \"$CMD\" when any files are modified"
      ;;

      "-b")
        BG="1"
      ;;

      "-f")
        FILE="1"
      ;;

      "-i")
        if [ $# -lt 2 ]; then
echo "Missing filter after -i parameter."
          return
fi
shift
IGNORE=$1
        echo "Ignoring files matching filter \"$IGNORE\""
      ;;

      "-h")
        echo "Usage: observe [OPTION]... [PATH]..."
        echo
echo "Observe specified paths, or current directory."
        echo
echo " -e <command> Execute command when any files are modified"
        echo " -b Execute the command in background (needs -e)"
        echo " -f Execute the command on each changed file (needs -e)"
        echo " -i <filter> Ignore files matching filter"
        echo " -h Show this help and exit"
        echo
return
      ;;
      *)
        PATHS="$PATHS $1"
      ;;
    esac

shift
done

if [ -z "$PATHS" ]; then
echo "Observing current directory"
  else
echo "Observing paths:"
    for path in $PATHS; do
echo " $path"
    done
fi

HIDEERROR=" 2>&1 /dev/null"
  FIND="find -L $PATHS -type f -newermt"
  NOW=`date`

  while true; do

while true; do
if [ -n "$IGNORE" ]; then
MODIFIED=`$FIND "$NOW" -not -name "$IGNORE"`
      else
MODIFIED=`$FIND "$NOW"`
      fi

if [ -n "$MODIFIED" ]; then
break
fi
sleep 1
    done

if [ -n "$!" ]; then
kill -0 $! $HIDEERROR && kill $! $HIDEERROR && sleep 1
    fi

if [ -n "$CMD" ]; then
if [ -n "$BG" ]; then
if [ -n "$FILE" ]; then
for file in $MODIFIED; do
            $CMD $file &
          done
else
          $CMD &
        fi
fi
if [ -n "$FILE" ]; then
for file in $MODIFIED; do
          $CMD $file
        done
else
        $CMD
      fi
else
for file in $MODIFIED; do
echo $file
      done
fi

sleep 1
    NOW=`date`
  done
}
Something went wrong with that request. Please try again.