2019-05-05 14:08:16 +03:00
#!/usr/bin/env bash
# This script defines common commands used during building / developing
# and makes it easy to run them.
2022-03-16 18:00:18 +03:00
SCRIPT_DIR=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
PROJECT_ROOT=$SCRIPT_DIR
2019-05-05 14:08:16 +03:00
THIS=$0
COMMAND=${1:-watch}
shift
2022-03-16 18:00:18 +03:00
ARGS=("$@") # NOTE: This is a bash array!
2019-05-05 14:08:16 +03:00
2020-09-22 14:52:16 +03:00
BOLD="\033[1m"
UNDERLINED="\033[4m"
RESET="\033[0m"
LIGHT_CYAN="\033[96m"
2021-11-16 22:42:49 +03:00
GREEN="\033[32m"
RED="\033[31m"
2020-09-22 14:52:16 +03:00
DEFAULT_COLOR="\033[39m"
2019-05-05 14:08:16 +03:00
2022-03-16 18:00:18 +03:00
BUILD_CMD="cabal build all"
2023-08-10 12:22:38 +03:00
INSTALL_CMD="${SCRIPT_DIR}/tools/install_packages_to_data_dir.sh && cabal install --overwrite-policy=always"
2022-03-16 18:00:18 +03:00
BUILD_ALL_CMD="cabal build all --enable-tests --enable-benchmarks"
TEST_UNIT_CMD="cabal test waspc-test"
2022-03-23 15:04:09 +03:00
TEST_CLI_CMD="cabal test cli-test"
2022-03-16 18:00:18 +03:00
TEST_E2E_CMD="cabal test e2e-test"
2022-11-07 19:07:47 +03:00
TEST_E2E_DELETE_GOLDENS="rm -rf $PROJECT_ROOT/e2e-test/test-outputs/*-golden"
2022-06-23 21:09:21 +03:00
TEST_E2E_ACCEPT_ALL_CMD="$TEST_E2E_DELETE_GOLDENS && $TEST_E2E_CMD"
2024-02-19 16:28:12 +03:00
TEST_HEADLESS_CMD="cd headless-test && npm install && npx playwright test"
TEST_CMD="cabal test && echo 'Running headless tests' && $TEST_HEADLESS_CMD && echo 'ALL TESTS PASSED' || { echo 'SOME TESTS FAILED'; exit 1; }"
2022-03-16 18:00:18 +03:00
RUN_CMD="cabal run wasp-cli ${ARGS[@]}"
GHCID_CMD="ghcid --command=cabal repl"
2023-11-29 01:36:10 +03:00
GHCUP_SET="ghcup set ghc 8.10.7 && ghcup set hls 2.2.0"
2022-03-16 18:00:18 +03:00
DEV_TOOLS_BIN="$PROJECT_ROOT/.bin"
function install_dev_tool () {
echo "cabal --project-file=$PROJECT_ROOT/dev-tool.project install $1 --installdir=$DEV_TOOLS_BIN --install-method=copy --overwrite-policy=always"
}
function dev_tool_path () {
echo "$DEV_TOOLS_BIN/$1"
}
STAN_CMD="$BUILD_ALL_CMD && $(install_dev_tool stan) && $(dev_tool_path stan) report ${ARGS[@]}"
HLINT_CMD="$(install_dev_tool hlint) && $(dev_tool_path hlint) . ${ARGS[@]}"
2022-09-08 14:56:37 +03:00
PRUNE_JUICE_CMD="$(install_dev_tool prune-juice) && $(dev_tool_path prune-juice) ${ARGS[@]}"
2022-03-16 18:00:18 +03:00
ORMOLU_BASE_CMD="$(install_dev_tool ormolu) && $(dev_tool_path ormolu) --color always --check-idempotence"
2021-11-16 22:42:49 +03:00
ORMOLU_CHECK_CMD="$ORMOLU_BASE_CMD --mode check "'$'"(git ls-files '*.hs' '*.hs-boot')"
ORMOLU_FORMAT_CMD="$ORMOLU_BASE_CMD --mode inplace "'$'"(git ls-files '*.hs' '*.hs-boot')"
2023-06-22 22:37:07 +03:00
WASP_PACKAGES_COMPILE="$SCRIPT_DIR/tools/install_packages_to_data_dir.sh"
2019-05-05 14:08:16 +03:00
echo_and_eval () {
echo -e $"${LIGHT_CYAN}Running:${DEFAULT_COLOR}" $1 "\n"
2021-11-16 22:43:55 +03:00
eval $1
2019-05-05 14:08:16 +03:00
}
echo_bold () { echo -e $"${BOLD}${1}${RESET}"; }
print_usage () {
print_usage_cmd () {
echo -e $" ${UNDERLINED}${1}${RESET}"
2022-03-16 18:00:18 +03:00
echo -e " $2";
2019-05-05 14:08:16 +03:00
}
2022-03-16 18:00:18 +03:00
echo_bold "Usage: run <command>"
2019-05-05 14:08:16 +03:00
echo "Commands:"
print_usage_cmd "build" \
"Builds the project."
2023-08-10 12:22:38 +03:00
print_usage_cmd "install" \
"Installs the project locally using cabal (runs '${SCRIPT_DIR}/tools/install_packages_to_data_dir.sh' and 'cabal install')."
2019-05-05 14:08:16 +03:00
print_usage_cmd "test" \
2024-02-19 16:28:12 +03:00
"Executes all tests (unit + e2e + headless). Builds the project first if needed."
2022-03-16 18:00:18 +03:00
print_usage_cmd "test:unit [pattern]" \
"Executes only unit tests. Builds the project first if needed. If pattern is provided, it will run only tests whose description/name matches the pattern. Check https://github.com/UnkindPartition/tasty#patterns to learn more about valid patterns."
2022-03-23 15:04:09 +03:00
print_usage_cmd "test:cli" \
"Executes only cli unit tests. Builds the project first if needed."
2022-03-16 18:00:18 +03:00
print_usage_cmd "test:e2e" \
"Executes only e2e tests. Builds the project first if needed."
2022-06-23 21:09:21 +03:00
print_usage_cmd "test:e2e:accept-all" \
"Accepts any diffs in the generated code in e2e tests. Does so by deleting current golden output and re-running e2e tests to produce new golden output."
2024-02-19 16:28:12 +03:00
print_usage_cmd "test:headless" \
"Executes headless tests, where example wasp apps are e2e tested using Playwright."
2021-11-11 21:37:56 +03:00
print_usage_cmd "wasp-cli <args>" \
2022-03-16 18:00:18 +03:00
"Runs the wasp executable while forwarding arguments. Builds the project first if needed."
2019-12-17 23:40:05 +03:00
print_usage_cmd "ghcid" \
2020-09-22 14:52:16 +03:00
"Runs ghcid, which watches source file changes and reports errors. Does not watch tests."
2020-01-28 21:41:14 +03:00
print_usage_cmd "ghcid-test" \
2020-09-22 14:52:16 +03:00
"Runs ghcid on both source and tests."
2021-11-16 22:42:49 +03:00
print_usage_cmd "code-check" \
"Checks code by running it through formatter, linter and static analysis."
print_usage_cmd "stan <args>" \
2022-03-16 18:00:18 +03:00
"Runs static code analysis on the code, generating stan.html. Builds the project first if needed."
2021-11-16 22:42:49 +03:00
print_usage_cmd "hlint <args>" \
"Runs linter on the codebase."
2022-09-08 14:56:37 +03:00
print_usage_cmd "prune-juice <args>" \
"Runs prune-juice on the codebase, which detects unused dependencies."
2021-11-16 22:42:49 +03:00
print_usage_cmd "ormolu:check" \
"Runs the code formatter and reports if code is correctly formatted or not."
print_usage_cmd "ormolu:format" \
"Runs the code formatter and formats the code in place."
2022-03-17 16:20:51 +03:00
print_usage_cmd "module-graph" \
"Creates graph of modules in the project. Needs graphmod (available on hackage) and graphviz (your os distribution) installed."
2023-06-29 00:19:06 +03:00
print_usage_cmd "wasp-packages:compile" \
"Compiles the TS projects under packages/."
2023-11-29 01:36:10 +03:00
print_usage_cmd "ghcup-set" \
"Sets the correct version of GHC and Cabal for this project (via GHCup)."
2021-11-16 22:42:49 +03:00
}
exitStatusToString () {
if (( $1 == 0 )); then echo "${GREEN}OK${RESET}"; else echo "${RED}FAIL${RESET}"; fi
2019-05-05 14:08:16 +03:00
}
case $COMMAND in
build)
echo_and_eval "$BUILD_CMD"
;;
2023-08-10 12:22:38 +03:00
install)
echo_and_eval "$INSTALL_CMD"
;;
2019-12-17 23:40:05 +03:00
ghcid)
echo_and_eval "$GHCID_CMD"
;;
2020-01-28 21:41:14 +03:00
ghcid-test)
# --color always is needed for Tasty to turn on the coloring.
# NOTE: I did not put this into variable because I was not able to put single quotes
# around :main --color always that way and it was not working.
2022-03-16 18:00:18 +03:00
ghcid -T=':main --color always' --command=cabal repl test/TastyDiscoverDriver.hs
2020-01-28 21:41:14 +03:00
;;
2019-05-05 14:08:16 +03:00
test)
echo_and_eval "$TEST_CMD"
;;
2022-03-16 18:00:18 +03:00
test:unit)
TEST_PATTERN="${ARGS[0]}"
if [[ -z "$TEST_PATTERN" ]]
then
echo_and_eval "$TEST_UNIT_CMD"
else
echo_and_eval "$TEST_UNIT_CMD --test-options \"-p \\\"$TEST_PATTERN\\\"\""
fi
2022-03-23 15:04:09 +03:00
;;
test:cli)
echo_and_eval "$TEST_CLI_CMD"
2022-03-16 18:00:18 +03:00
;;
test:e2e)
echo_and_eval "$TEST_E2E_CMD"
;;
2022-06-23 21:09:21 +03:00
test:e2e:accept-all)
echo_and_eval "$TEST_E2E_ACCEPT_ALL_CMD"
;;
2024-02-19 16:28:12 +03:00
test:headless)
echo_and_eval "$TEST_HEADLESS_CMD"
;;
2021-11-16 22:43:55 +03:00
wasp-cli)
2022-03-16 18:00:18 +03:00
echo_and_eval "$RUN_CMD"
2019-05-05 14:08:16 +03:00
;;
2021-11-16 22:43:55 +03:00
stan)
echo_and_eval "$STAN_CMD"
;;
2021-11-16 22:42:49 +03:00
hlint)
echo_and_eval "$HLINT_CMD"
;;
2022-09-08 14:56:37 +03:00
prune-juice)
echo_and_eval "$PRUNE_JUICE_CMD"
;;
2021-11-16 22:42:49 +03:00
ormolu:check)
echo_and_eval "$ORMOLU_CHECK_CMD"
;;
ormolu:format)
echo_and_eval "$ORMOLU_FORMAT_CMD"
;;
code-check)
echo_and_eval "$ORMOLU_CHECK_CMD"
ORMOLU_RESULT=$?
echo_and_eval "$HLINT_CMD"
HLINT_RESULT=$?
echo_and_eval "$STAN_CMD"
STAN_RESULT=$?
TOTAL_RESULT=$(($ORMOLU_RESULT || $HLINT_RESULT || $STAN_RESULT))
echo
echo
echo "======================================"
echo " SUMMARY"
echo "======================================"
echo
echo -e "Formatter (ormolu): $(exitStatusToString $ORMOLU_RESULT)"
echo -e "Linter (hlint): $(exitStatusToString $HLINT_RESULT)"
echo -e "Static analysis (stan): $(exitStatusToString $STAN_RESULT)"
echo "-----------------------"
echo -e "All together: $(exitStatusToString $TOTAL_RESULT)"
exit $TOTAL_RESULT
;;
2022-03-17 16:20:51 +03:00
module-graph)
2022-04-13 15:19:17 +03:00
echo_and_eval "graphmod --quiet --prune-edges $PROJECT_ROOT/src/**/*.hs | dot -Gsize=60,60! -Tpng -o module-graph.png" && echo "Printed module graph to module-graph.png."
2022-03-17 16:20:51 +03:00
;;
2023-06-22 22:37:07 +03:00
wasp-packages:compile)
echo_and_eval "$WASP_PACKAGES_COMPILE"
2023-02-07 18:29:25 +03:00
;;
2023-11-29 01:36:10 +03:00
ghcup-set)
echo_and_eval "$GHCUP_SET"
;;
2019-05-05 14:08:16 +03:00
*)
print_usage
exit 1
esac