Created
August 20, 2016 08:53
-
-
Save chrishenzie/7a5284d57af7ca27702028b5271b1551 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/bash | |
set -e | |
usage() { | |
cat << EOF | |
Usage: ./dev.sh [options...] | |
Options: | |
-p, --pattern Pattern of files to watch | |
-s, --sleep Time to wait between scans | |
-c, --command Command to run when changes are detected | |
-h, --help This help text | |
EOF | |
} | |
bash_version_check() { | |
local bash_major_version=$(echo $BASH_VERSION | head -c 1) | |
if [ $bash_major_version -lt 4 ]; then | |
echo "This script requires associative arrays, which are supported in bash >= 4." >&2 | |
exit 1 | |
fi | |
} | |
assert_args() { | |
if [ -z ${PATTERN+x} ]; then echo "Please provide a value for -p|--pattern." >&2; exit 1; fi | |
if [ -z ${SLEEP_SECONDS+x} ]; then echo "Please provide a value for -s|--sleep." >&2; exit 1; fi | |
if [ -z ${BUILD_COMMAND+x} ]; then echo "Please provide a value for -c|--command." >&2; exit 1; fi | |
} | |
scan_files() { | |
for file in $(find . -name *.go); do | |
local md5=$(md5 -q $file) | |
local time_modified=$(GetFileInfo -d $file) | |
# hash key name can't have periods or slashes | |
file=${file//\./} | |
file=${file//\//} | |
if [ ! "${FILE_MD5S[$file]+_}" ]; then | |
echo "[$time_modified]: new file: $file" | |
FILE_MD5S[$file]=$md5 | |
build_project | |
return | |
fi | |
if [ "${FILE_MD5S[$file]}" != "$md5" ]; then | |
echo "[$time_modified]: file changed: $file" | |
FILE_MD5S[$file]=$md5 | |
build_project | |
return | |
fi | |
done | |
} | |
build_project() { | |
$($BUILD_COMMAND) | |
#godep go build | |
} | |
start_loop() { | |
while true; do | |
scan_files | |
sleep $SLEEP_SECONDS | |
done | |
} | |
bash_version_check | |
while [[ $# -gt 1 ]]; do | |
key="$1" | |
case $key in | |
-p|--pattern) | |
PATTERN="$2" | |
shift | |
;; | |
-s|--sleep) | |
SLEEP_SECONDS="$2" | |
shift | |
;; | |
-c|--command) | |
BUILD_COMMAND="$2" | |
shift | |
;; | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
assert_args | |
cd "$(dirname "$0")" | |
declare -A FILE_MD5S | |
start_loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment