Skip to content

Instantly share code, notes, and snippets.

@nilium
Last active August 29, 2015 14:24
Show Gist options
  • Save nilium/9a66b997e551152da6a1 to your computer and use it in GitHub Desktop.
Save nilium/9a66b997e551152da6a1 to your computer and use it in GitHub Desktop.
General purpose make.bash script for building wgo-based Go projects
#!/usr/bin/env bash
# General-purpose build script for wgo workspaces -- by default, builds all
# main root packages and their sub-packages. This sort of thing isn't
# applicable to all projects, especially those with more than just binary
# products.
if ! which wgo >/dev/null ; then
cat <<-EOF 1>&2
wgo not found. Please install wgo and vendor before running make.bash:
$ go get github.com/skelterjohn/wgo
$ go get github.com/skelterjohn/vendor
EOF
fi
# Root ourselves in the workspace
cd "$(dirname "${BASH_SOURCE[0]}")"
headline () {
echo "$*"
echo "$*" | sed "s/./=/g"
}
root_packages () {
# Don't use -depth 2 because GNU find is dumb and doesn't support this.
packages=($(find src -mindepth 2 -maxdepth 2 -iname '*.go' -exec grep -Rle '^package main\b' '{}' '+'))
for i in ${!packages[@]} ; do
packages[$i]="$(dirname "${packages[$i]#src/}")"
done
printf "%s\n" "${packages[@]}" | sort -u
}
testable_packages () {
# Don't use -depth 2 because GNU find is dumb and doesn't support this.
packages=($(find src -mindepth 2 -iname '*_test.go'))
for i in ${!packages[@]} ; do
packages[$i]="$(dirname "${packages[$i]#src/}")"
done
printf "%s\n" "${packages[@]}" | sort -u
}
if [[ ${#testopt} != 0 ]] ; then
testopt=($testopt)
fi
if [[ ${#installopt} != 0 ]] ; then
installopt=($installopt)
fi
if [[ "$verbose" == 0 || "$verbose" == no ]] ; then
vflag=''
else
vflag='-v'
fi
if [[ -z "$project" ]] ; then
test_proj_dirs=($(testable_packages))
first=yes
for proj_dir in ${test_proj_dirs[@]} ; do
if [[ $first == no ]] ; then
echo ''
fi
first=no
headline "Testing $proj_dir"
pushd "src/$proj_dir" >/dev/null
(set -x ; wgo test $vflag -i "${testopt[@]}" .)
(set -x ; wgo test $vflag "${testopt[@]}" .)
popd >/dev/null
done
proj_dirs=($(root_packages))
for proj_dir in ${proj_dirs[@]} ; do
if [[ $first == no ]] ; then
echo ""
fi
first=no
if [[ "$(find "src/$proj_dir" -iname '*.go' -depth 1 | wc -l)" -lt 1 ]] ; then
continue
fi
notest=1 project="${proj_dir#src/}" ./make.bash
if [ ! $? ] ; then
exit 1
fi
done
exit 0
fi
arch="$(go env GOARCH)"
os="$(go env GOOS)"
# Only really useful if there's a package step (removed from this script, but
# leaving this here because it's still used to log non-host target directories
# to stdout)
loc="${os}_${arch}"
if [[ "$loc" == "$(go env GOHOSTOS)_$(go env GOHOSTARCH)" ]] ; then
loc="bin"
else
loc="bin/$loc"
fi
# Try to generate some sort of version string -- this will get passed to as an ldflag to set a version string variable at compile-time.
proj_build_time="$(date -u '+%Y-%m-%dT%H:%M:%S')Z"
proj_version="$(git describe --first-parent --match 'v*.*.*' --all --long 2>/dev/null)"
proj_version="${proj_version:-v0.0.0}"
proj_version="$project $proj_version ($os/$arch) $proj_build_time"
headline "Building $proj_version"
if [[ $loc != bin ]] ; then
echo "products under $loc"
fi
# Only restore if the vendor directory doesn't already exist -- if it does,
# assume that we probably don't want to accidentally nuke changes we've made
# that aren't yet saved to .gocfg (i.e., updated refs).
if [[ ! -d vendor ]] ; then
(set -x ; wgo restore)
fi
pushd "src/$project" >/dev/null
# If there are tests to run in or under the project package, then run them now
# (unless notest is set to anything).
if [[ -z "$notest" && "$(find . -iname '*_test.go' | wc -l)" -gt 0 ]] ; then
(set -x ; wgo test -i $vflag './...')
(set -x ; wgo test $vflag "${testopt[@]}" './...')
fi
if [[ -z "$noinstall" ]] ; then
(set -x ; wgo install $vflag -ldflags "-X main.version '$proj_version'" "${installopt[@]}" './...')
fi
popd >/dev/null
# vim: set noet ts=8 sw=8 sts=8 tw=79 :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment