Skip to content

Instantly share code, notes, and snippets.

@danfarino
Last active August 7, 2024 13:41
Show Gist options
  • Save danfarino/bf2f0208c097138bc1eb9787f643ae94 to your computer and use it in GitHub Desktop.
Save danfarino/bf2f0208c097138bc1eb9787f643ae94 to your computer and use it in GitHub Desktop.
Conditionally build and run Go program if any files have changed
#!/usr/bin/env bash
# Note: be sure to add `.exe/` to your `.gitignore` file, otherwise this script will not work properly.
set -euo pipefail
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
input_sha=$(
(
cd "$script_dir"
git ls-tree -r HEAD -- .
# shellcheck disable=SC2016
git status --short -uall . | cut -c4- | xargs -n1 /bin/sh -c 'sha1sum "$0" 2>&1 || true'
) | sha1sum | cut -d' ' -f1
)
exe="$script_dir/.exe/$input_sha"
[[ -x $exe ]] || {
echo "Compiling..."
mkdir -p .exe
go build -o "$exe" .
}
exec "$exe" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment