Skip to content

Commit

Permalink
feat(build.sh): Add a watch mode for automatic recompilation (#160)
Browse files Browse the repository at this point in the history
This features uses and requires https://github.com/AgentCosmic/xnotify
and should work on any platform

Use it with

* Watch & compile: build.sh --watch
* Watch, compile, test: build.sh --watch --test
* Watch, compile, test & verbose output: build.sh --watch --test --verbose

(shortcuts -w && -t can be used, too)
  • Loading branch information
rhuss authored and knative-prow-robot committed Jun 7, 2019
1 parent 01da225 commit 8a1adf0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ You can link that script into a directory within your search `$PATH`. This allow
* `build.sh -f` - Compile only
* `build.sh -f -t` - Compile & test
* `build.sh -u` - Update dependencies before compiling
* `build.sh -w` - Enter watch mode for automatic recompilation
* `build.sh -w -t` - Enter watch mode for automatic recompilation & running tests

See `build.sh --help` for a full list of options and usage examples.

Expand Down
39 changes: 36 additions & 3 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ run() {
exit 0
fi

if $(has_flag --watch -w); then
watch
# No exit, needs to be stopped with CTRL-C anyways
fi

if $(has_flag -u --update); then
# Update dependencies
update_deps
Expand Down Expand Up @@ -105,6 +110,31 @@ generate_docs() {
go run "./hack/generate-docs.go" "."
}

watch() {
local command="./hack/build.sh --fast"
local notify_opts="--include cmd --include pkg --batch 500"
if $(has_flag --test -t); then
command="$command --test"
fi
if $(has_flag --verbose); then
notify_opts="$notify_opts --verbose"
fi
set +e
which xnotify >/dev/null 2>&1
if [ $? -ne 0 ]; then
local green=""
local reset=""

echo "🤷 Watch: Cannot find ${green}xnotify${reset}"
echo "🌏 Please download from ${green}https://github.com/AgentCosmic/xnotify/releases${reset} and install in \$PATH"
exit 1
fi
set -e

echo "🔁 Watch"
xnotify $notify_opts -- $command
}

# Dir where this script is located
basedir() {
# Default is current directory
Expand Down Expand Up @@ -154,10 +184,12 @@ Usage: $(basename $BASH_SOURCE) [... options ...]
with the following options:
-f --fast Only compile (without formatting, testing, doc generation)
-t --test Run tests when used with --fast
-t --test Run tests when used with --fast or --watch
-u --update Update dependencies before compiling
-w --watch Watch for source changes and recompile in fast mode
-h --help Display this help message
--verbose Verbose script output (set -x)
--verbose More output
--debug Debug information for this script (set -x)
You can add a symbolic link to this build script into your PATH so that it can be
called from everywhere. E.g.:
Expand All @@ -169,10 +201,11 @@ Examples:
* Compile, format, tests, docs: build.sh
* Compile only: build.sh --fast
* Compile with tests: build.sh -f -t
* Automatice recompilation: build.sh --watch
EOT
}

if $(has_flag --verbose); then
if $(has_flag --debug); then
export PS4='+($(basename ${BASH_SOURCE[0]}):${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -x
fi
Expand Down

0 comments on commit 8a1adf0

Please sign in to comment.