From 8c4c69072210fce322c6d9c825531fa4735b4a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20Hu=C3=9F?= Date: Thu, 30 May 2019 10:40:22 +0200 Subject: [PATCH 1/5] feat(build.sh): Adding options and running tests * Added running unit tests to the script * Added options to support different development flows (see help message below) * Allow symlinking to /usr/local/bin so that the script can be called from everywhere Usage message: Knative Client Build Script Usage: hack/build.sh [... 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 .../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 --- hack/build.sh | 162 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 141 insertions(+), 21 deletions(-) diff --git a/hack/build.sh b/hack/build.sh index ad6d351092..88e9ada815 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -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 < Date: Thu, 30 May 2019 11:38:00 +0200 Subject: [PATCH 2/5] doc(build.sh): Added documentation for new build.sh options --- DEVELOPMENT.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 2c916b24d7..1fa1302fbd 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -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 +* `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:** From 447e3c839472017965f36daa0415ffbb47203ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20Hu=C3=9F?= Date: Thu, 30 May 2019 12:04:33 +0200 Subject: [PATCH 3/5] docs(build.sh): Cosmetic fixes --- DEVELOPMENT.md | 2 +- hack/build.sh | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 1fa1302fbd..d4fbd042d7 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -71,7 +71,7 @@ $ hack/build.sh 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 +* `build.sh` Compile, test, generate docs and format source code * `build.sh -f` - Only compile * `build.sh -f -t` - Compile & test * `build.sh -u` - Update dependencies diff --git a/hack/build.sh b/hack/build.sh index 88e9ada815..6fa10f08ed 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -27,20 +27,18 @@ set -eu # Run build run() { + # Switch on modules unconditionally export GO111MODULE=on - # Jump in/out project directory + # Jump into project directory pushd $(basedir) >/dev/null 2>&1 - trap "popd >/dev/null 2>&1" EXIT + # Print help if requested 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 @@ -99,6 +97,7 @@ update_deps() { echo "πŸ•ΈοΈ Update" go mod vendor } + generate_docs() { echo "πŸ“– Docs" rm -rf "./docs/cmd" @@ -148,15 +147,15 @@ has_flag() { display_help() { local command="${1:-}" cat < Date: Thu, 30 May 2019 12:08:57 +0200 Subject: [PATCH 4/5] docs(build.sh): Tiny documentation fix --- DEVELOPMENT.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index d4fbd042d7..3ee05d2c8c 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -71,10 +71,10 @@ $ hack/build.sh 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, generate docs and format source code -* `build.sh -f` - Only compile +* `build.sh` - Compile, test, generate docs and format source code +* `build.sh -f` - Compile only * `build.sh -f -t` - Compile & test -* `build.sh -u` - Update dependencies +* `build.sh -u` - Update dependencies before compiling See `build.sh --help` for a full list of options and usage examples. From 01ab0506320d11b89bcc5e9cf1f9a519a3c4f483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20Hu=C3=9F?= Date: Thu, 30 May 2019 12:27:13 +0200 Subject: [PATCH 5/5] chore(build.sh): Another typo fix --- hack/build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hack/build.sh b/hack/build.sh index 6fa10f08ed..6f5bbee348 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -72,7 +72,7 @@ go_fmt() { go_build() { echo "🚧 Compile" source "./hack/build-flags.sh" - go build -mod=vendor -ldflags "$(build_flags .)" -o ./kn ./cmd/... + go build -mod=vendor -ldflags "$(build_flags .)" -o kn ./cmd/... } go_test() { @@ -153,7 +153,7 @@ Usage: $(basename $BASH_SOURCE) [... options ...] with the following options: --f --fast Only compile (without formatting, testing, code generation) +-f --fast Only compile (without formatting, testing, doc generation) -t --test Run tests when used with --fast -u --update Update dependencies before compiling -h --help Display this help message