Skip to content

Instantly share code, notes, and snippets.

@nrichards
Last active August 29, 2015 14:20
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 nrichards/9a8398eca1e3da4119f7 to your computer and use it in GitHub Desktop.
Save nrichards/9a8398eca1e3da4119f7 to your computer and use it in GitHub Desktop.
Auto-build OSX shell script watches directories, builds unless already building, using fswatch, stream_prettifier, and bash for Apportable 'dt' command line build
#!/bin/bash
# DESCRIPTION: watch_and_dt.sh - watches a set of hardcoded directories and builds
# with Apportable dt unless already building.
# REAQUIREMENTS: Apportable dt, optionally stream_prettifier, fswatch, osx
# OPTIONAL: Run piping to stream_prettifier with ~/.stream_prettifier file
# if_seen /Error|error/ do |line|
# line.white.on_red
# end
# if_seen /Building.*apk/ do |line|
# line.white.on_green
# end
# OUTPUT (optionally colorized with stream_prettifier):
# Mon Apr 27 17:31:20 PDT 2015 ( /Users/nick/proj/AdNetworkTests/adnetworktests/ViewController.m )
# [7/7] Building Build/android-armeabi-debug/adnetworktests/adnetworktests-debug.apk
# ...
# CONFIG
DT_FULL_PATH=`which dt`
DT_BASE_PATH=${DT_FULL_PATH%/bin/dt}
WATCH_ME_0=.
WATCH_ME_1=$DT_BASE_PATH
IGNORE_ME_0=$DT_BASE_PATH/Build
IGNORE_ME_1=.git
IGNORE_ME_2=$DT_BASE_PATH/.git
export EXEC_CMD="dt 2>&1 | grep -i -e error -e Building.*apk"
export CMD_AS_SEEN_BY_PS_EF=$DT_FULL_PATH
export MAX_LINE_LENGTH=4096
# CODE
exec_unless() {
num_running=$(ps -ef | grep -c $CMD_AS_SEEN_BY_PS_EF)
if [ $num_running -gt 1 ]; then
# Already running
true
else
# Not running
echo `date` \( $1 \)
bash -c "$EXEC_CMD" | cut -c 1-$MAX_LINE_LENGTH &
fi
}
export -f exec_unless
fswatch -0 $WATCH_ME_0 $WATCH_ME_1 -e $IGNORE_ME_0 -e $IGNORE_ME_1 -e $IGNORE_ME_2 \
| xargs -0 -n 1 -I {} bash -c 'exec_unless "$@"' _ {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment