Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build.sh): Adding options and running tests #149

Merged
merged 5 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ Once you've [setup your development environment](#prerequisites), let's build
$ hack/build.sh
```

It builds `kn` binary in your current directory. You can start playing with it.
You can link that script into a directory within your search `$PATH`. This allows you to build `kn` from any working directory. There are several options to support various development flows:

* `build.sh` Compile, test, generated docs and format source code
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*generate

* `build.sh -f` - Only compile
* `build.sh -f -t` - Compile & test
* `build.sh -u` - Update dependencies

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

At the end, the build results in `kn` binary in your current directory, which can be directly executed.

**Notes:**

Expand Down
162 changes: 141 additions & 21 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,167 @@
# limitations under the License.

set -o pipefail

# Store for later
if [ -z "$1" ]; then
ARGS=("")
else
ARGS=("$@")
fi

set -eu

# Run build
run() {
export GO111MODULE=on

go_fmt
# Jump in/out project directory
pushd $(basedir) >/dev/null 2>&1
trap "popd >/dev/null 2>&1" EXIT

if $(has_flag --help -h); then
display_help
exit 0
fi

# Switch on modules unconditionally
export GO111MODULE=on

if $(has_flag -u --update); then
# Update dependencies
update_deps
fi

# Run build
go_build
generate_docs

echo "🌞 Success"
# Run tests
if $(has_flag --test -t) || ! $(has_flag --fast -f); then
go_test
fi

$(basedir)/kn version
if ! $(has_flag --fast -f); then
# Format source code
go_fmt

# Generate docs
generate_docs
fi

echo "────────────────────────────────────────────"
./kn version
}

go_fmt() {
local base=$(basedir)
echo "📋 Formatting"
go fmt "${base}/cmd/..." "${base}/pkg/..."
echo "🧹 Format"
go fmt ./cmd/... ./pkg/...
}

go_build() {
local base=$(basedir)
echo "🚧 Building"
source "${base}/hack/build-flags.sh"
go build -mod=vendor -ldflags "$(build_flags ${base})" -o ${base}/kn ${base}/cmd/...
echo "🚧 Compile"
source "./hack/build-flags.sh"
go build -mod=vendor -ldflags "$(build_flags .)" -o ./kn ./cmd/...
}

generate_docs() {
local base=$(basedir)
echo "📑 Generating docs"
rm -rf "${base}/docs/cmd"
mkdir -p "${base}/docs/cmd"
go_test() {
local test_output=$(mktemp /tmp/kn-client-test-output.XXXXXX)
local red=""
local reset=""

go run "${base}/hack/generate-docs.go" "${base}"
echo "🧪 Test"
set +e
go test -v ./pkg/... >$test_output 2>&1
local err=$?
if [ $err -ne 0 ]; then
echo "🔥 ${red}Failure${reset}"
cat $test_output | sed -e "s/^.*\(FAIL.*\)$/$red\1$reset/"
rm $test_output
exit $err
fi
rm $test_output
}

update_deps() {
echo "🕸️ Update"
go mod vendor
}
generate_docs() {
echo "📖 Docs"
rm -rf "./docs/cmd"
mkdir -p "./docs/cmd"
go run "./hack/generate-docs.go" "."
}

# Dir where this script is located
basedir() {
dir=$(dirname "${BASH_SOURCE[0]}")
base=$(cd "$dir/.." && pwd)
echo ${base}
# Default is current directory
local script=${BASH_SOURCE[0]}

# Resolve symbolic links
if [ -L $script ]; then
if readlink -f $script >/dev/null 2>&1; then
script=$(readlink -f $script)
elif readlink $script >/dev/null 2>&1; then
script=$(readlink $script)
elif realpath $script >/dev/null 2>&1; then
script=$(realpath $script)
else
echo "ERROR: Cannot resolve symbolic link $script"
exit 1
fi
fi

local dir=$(dirname "$script")
local full_dir=$(cd "${dir}/.." && pwd)
echo ${full_dir}
}

# Checks if a flag is present in the arguments.
has_flag() {
filters="$@"
for var in "${ARGS[@]}"; do
for filter in $filters; do
if [ "$var" = "$filter" ]; then
echo 'true'
return
fi
done
done
echo 'false'
}

# Display a help message.
display_help() {
local command="${1:-}"
cat <<EOT
Knative Client Build Script

Usage: $(basename $BASH_SOURCE) [... options ...]

with the following options:

-f --fast Only build (without formatting, testing, code generation)
-t --test Run tests even when used with --fast
-u --update Update dependencies
-h --help Display this help message
--verbose Verbose script output (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.:

ln -s $(basedir)/hack/build.sh /usr/local/bin/kn_build.sh

Examples:

* Compile, format, tests, docs: build.sh
* Compile only: build.sh --fast
* Compile with tests: build.sh -f -t
EOT
}

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

run $*