#!/usr/bin/env bash set -eu set -o pipefail set -o errtrace shopt -s expand_aliases [ "$(uname)" == "Darwin" ] && alias readlink="greadlink" || : here="$(pwd)" SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" TAO_PATH="${TAO_PATH:-$SCRIPT_DIR/../..}" BASEIMAGE_REPO="${BASEIMAGE_REPO:-baseimage-tutorial-framework}" TEST_REPO="${TEST_REPO:-"$(basename "$(realpath "${SCRIPT_DIR}/..")")"}" FRONTEND_REPO="${FRONTEND_REPO:-frontend-tutorial-framework}" BASEIMAGE_PATH="${TAO_PATH}/${BASEIMAGE_REPO}" TEST_PATH="${TAO_PATH}/${TEST_REPO}" FRONTEND_PATH="${TAO_PATH}/${FRONTEND_REPO}" IMAGE_NAME="${IMAGE_NAME:-test-tutorial-framework}" TEST_PORT="${TEST_PORT:-8888}" AVATAO_SECRET="${AVATAO_SECRET:-secret}" BUILD_CONTEXT="${BUILD_CONTEXT:-solvable}" source "${SCRIPT_DIR}/libhack/baseimage.sh" build_test_internal() { [[ -d "$BASEIMAGE_PATH" ]] && baseimage::build_as_latest cd $TEST_PATH docker build -t $IMAGE_NAME \ -f ${BUILD_CONTEXT}/Dockerfile \ --build-arg BUILD_CONTEXT=$BUILD_CONTEXT \ ${ARGS} . } build_test() { ARGS="--build-arg NOFRONTEND=1" build_test_internal } build_test_withfrontend() { ARGS="--no-cache" build_test_internal } run_test() { cd "$TAO_PATH" if [ "${HOTRELOAD:-0}" == "1" ]; then [ -d "$BASEIMAGE_PATH" ] && mount_baseimage="-v ${BASEIMAGE_PATH}/lib/tfw:/usr/local/lib/tfw" mount_test="-v ${TEST_PATH}/solvable/src:/srv/.tfw" mount_volumes="${mount_baseimage:-} ${mount_test}" fi docker run --rm \ -p $TEST_PORT:$TEST_PORT \ ${mount_volumes:-} \ ${@:-} \ -e AVATAO_SECRET=$AVATAO_SECRET $IMAGE_NAME } run_frontend() { FRONTEND_IN_TEST="${TEST_PATH}/solvable/frontend" if [ "$(find "$FRONTEND_IN_TEST" | wc -l)" -gt 2 ] then cd $FRONTEND_IN_TEST else cd $FRONTEND_PATH fi yarn start } start_test() { trap 'exit' INT TERM trap 'kill 0' EXIT [[ "${RUN_FRONTEND:-1}" == "1" ]] && run_frontend & [[ "${BUILD:-1}" == "1" ]] && build_test run_test $@ wait } build_docs() { mount_point="/mnt/docs" docker run --rm \ -ti \ -v "$BASEIMAGE_PATH"/docs:"$mount_point" \ "$BASEIMAGE_NAME" \ /bin/bash -c \ "cd $mount_point pip3 install sphinx make html exit" \ &> /dev/null if [ "$?" == "0" ]; then echo "Built baseimage docs at ${BASEIMAGE_PATH}/docs/build/html!" else echo "Building baseimage docs failed!" fi } case ${1:-} in start) RUN_FRONTEND=1 start_test ${@:2} ;; run) BUILD=0 RUN_FRONTEND=1 start_test ${@:2} ;; startcontainer) RUN_FRONTEND=0 start_test ${@:2} ;; runcontainer) BUILD=0 RUN_FRONTEND=0 start_test ${@:2} ;; buildtfw) [[ -d "$BASEIMAGE_PATH" ]] && baseimage::build_as_latest ;; build) build_test ;; buildwithfrontend) build_test_withfrontend ;; releasetfw) [[ -d "$BASEIMAGE_PATH" ]] && baseimage::release ;; builddocs) build_docs ;; *) echo "Usage: tfw.sh [start|buildtfw|build|buildwithfrontend|releasetfw]" echo " |--- start: build & run TFW test and Angular frontend" echo " |--- run: run TFW test and Angular frontend" echo " |--- startcontainer: build & run TFW test, container only" echo " |--- runcontainer: run TFW test, container only" echo " |--- buildtfw: build TFW baseimage" echo " |--- build: build TFW baseimage and test" echo " |--- buildwithfrontend: build TFW baseimage and test, include frontend in image" echo " |--- releasetfw: tag TFW baseimage and push to upstream" echo " |--- builddocs: build baseimage documentation (in docs/build/html)" ;; esac