Skip to content

Commit

Permalink
feat(build.sh): Adding options and running tests (#149)
Browse files Browse the repository at this point in the history
* 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

* doc(build.sh): Added documentation for new build.sh options

* docs(build.sh): Cosmetic fixes

* docs(build.sh): Tiny documentation fix

* chore(build.sh): Another typo fix
  • Loading branch information
rhuss authored and knative-prow-robot committed May 30, 2019
1 parent cf41ab2 commit 9221429
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 22 deletions.
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, generate docs and format source code
* `build.sh -f` - Compile only
* `build.sh -f -t` - Compile & test
* `build.sh -u` - Update dependencies before compiling

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
161 changes: 140 additions & 21 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,166 @@
# limitations under the License.

set -o pipefail

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

set -eu

# Run build
run() {
# Switch on modules unconditionally
export GO111MODULE=on

go_fmt
# Jump into project directory
pushd $(basedir) >/dev/null 2>&1

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

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

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

# Generate docs
generate_docs
fi

$(basedir)/kn version
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=""

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
}

go run "${base}/hack/generate-docs.go" "${base}"
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'
}

run $*
# 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 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
--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
}

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

run $*

0 comments on commit 9221429

Please sign in to comment.