2019-04-10 15:22:35 +03:00
|
|
|
#!/usr/bin/env bash
|
2023-01-04 20:21:15 +03:00
|
|
|
# Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
2019-04-10 15:22:35 +03:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Run formatters and linter, anything platform-independent and quick
|
|
|
|
#
|
2022-08-17 14:10:51 +03:00
|
|
|
# Usage: ./fmt.sh [--test] [--diff]
|
2019-04-10 15:22:35 +03:00
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
cd "$(dirname "$0")"
|
|
|
|
# load the dev-env
|
|
|
|
eval "$(dev-env/bin/dade-assist)"
|
|
|
|
|
|
|
|
## Config ##
|
|
|
|
is_test=
|
|
|
|
scalafmt_args=()
|
2021-01-29 19:50:18 +03:00
|
|
|
javafmt_args=(--set-exit-if-changed --replace)
|
2021-02-03 16:00:34 +03:00
|
|
|
diff_mode=false
|
2019-04-10 15:22:35 +03:00
|
|
|
dade_copyright_arg=update
|
2019-04-12 14:10:16 +03:00
|
|
|
buildifier_target=//:buildifier-fix
|
2021-10-26 15:58:35 +03:00
|
|
|
security_update_args=()
|
2022-02-11 14:44:52 +03:00
|
|
|
prettier_args="--write"
|
2019-04-10 15:22:35 +03:00
|
|
|
|
|
|
|
## Functions ##
|
|
|
|
|
2022-02-11 14:44:52 +03:00
|
|
|
run_pprettier() {
|
|
|
|
yarn pprettier $@
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:22:35 +03:00
|
|
|
log() {
|
|
|
|
echo "fmt.sh: $*" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
run() {
|
|
|
|
echo "$ ${*%Q}"
|
|
|
|
"$@"
|
|
|
|
ret=$?
|
|
|
|
if [[ $is_test = 1 && $ret -gt 0 ]]; then
|
|
|
|
log "command failed with return $ret"
|
|
|
|
log
|
|
|
|
log "run ./fmt.sh to fix the issue"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-02-03 16:00:34 +03:00
|
|
|
# We do not run on deleted files, or files that have been added since we last rebased onto main.
|
|
|
|
check_diff() {
|
|
|
|
# $1 merge_base
|
|
|
|
# $2 regex
|
|
|
|
# "${@:3}" command
|
2023-11-23 10:54:35 +03:00
|
|
|
changed_files=$(git diff --name-only --diff-filter=ACMRT "$1" | grep $2 | grep -E -v '^canton(-3x)?/' || [[ $? == 1 ]])
|
2021-02-03 16:00:34 +03:00
|
|
|
if [[ -n "$changed_files" ]]; then
|
|
|
|
run "${@:3}" ${changed_files[@]:-}
|
|
|
|
else
|
|
|
|
echo "No changed file to check matching '$2', skipping."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:22:35 +03:00
|
|
|
## Main ##
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
-h | --help)
|
|
|
|
cat <<USAGE
|
|
|
|
Usage: ./fmt.sh [options]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help: shows this help
|
|
|
|
--test: only test for formatting changes, used by CI
|
2022-08-17 14:10:51 +03:00
|
|
|
--diff: only consider files that changed since main
|
2019-04-10 15:22:35 +03:00
|
|
|
USAGE
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
--test)
|
|
|
|
shift
|
|
|
|
is_test=1
|
|
|
|
scalafmt_args+=(--test)
|
2021-01-29 19:50:18 +03:00
|
|
|
javafmt_args=(--set-exit-if-changed --dry-run)
|
2019-04-10 15:22:35 +03:00
|
|
|
dade_copyright_arg=check
|
2019-04-12 14:10:16 +03:00
|
|
|
buildifier_target=//:buildifier
|
2021-10-26 15:58:35 +03:00
|
|
|
security_update_args+=(--test)
|
2022-02-11 14:44:52 +03:00
|
|
|
prettier_args="--check"
|
2019-04-10 15:22:35 +03:00
|
|
|
;;
|
2020-03-23 13:22:02 +03:00
|
|
|
--diff)
|
|
|
|
shift
|
2020-12-27 16:19:07 +03:00
|
|
|
merge_base="$(git merge-base origin/main HEAD)"
|
2020-10-21 15:07:11 +03:00
|
|
|
scalafmt_args+=('--mode=diff' "--diff-branch=${merge_base}")
|
2021-02-03 16:00:34 +03:00
|
|
|
diff_mode=true
|
2020-03-13 19:37:13 +03:00
|
|
|
;;
|
2019-04-10 15:22:35 +03:00
|
|
|
*)
|
|
|
|
echo "fmt.sh: unknown argument $1" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-05-24 12:11:06 +03:00
|
|
|
echo "\
|
|
|
|
─────────────────────────────▄██▄
|
|
|
|
──FORMAT ALL THE THINGS!!!───▀███
|
|
|
|
────────────────────────────────█
|
|
|
|
───────────────▄▄▄▄▄────────────█
|
|
|
|
──────────────▀▄────▀▄──────────█
|
|
|
|
──────────▄▀▀▀▄─█▄▄▄▄█▄▄─▄▀▀▀▄──█
|
|
|
|
─────────█──▄──█────────█───▄─█─█
|
|
|
|
─────────▀▄───▄▀────────▀▄───▄▀─█
|
|
|
|
──────────█▀▀▀────────────▀▀▀─█─█
|
|
|
|
──────────█───────────────────█─█
|
|
|
|
▄▀▄▄▀▄────█──▄█▀█▀█▀█▀█▀█▄────█─█
|
|
|
|
█▒▒▒▒█────█──█████████████▄───█─█
|
|
|
|
█▒▒▒▒█────█──██████████████▄──█─█
|
|
|
|
█▒▒▒▒█────█───██████████████▄─█─█
|
|
|
|
█▒▒▒▒█────█────██████████████─█─█
|
|
|
|
█▒▒▒▒█────█───██████████████▀─█─█
|
|
|
|
█▒▒▒▒█───██───██████████████──█─█
|
|
|
|
▀████▀──██▀█──█████████████▀──█▄█
|
|
|
|
──██───██──▀█──█▄█▄█▄█▄█▄█▀──▄█▀
|
|
|
|
──██──██────▀█─────────────▄▀▓█
|
|
|
|
──██─██──────▀█▀▄▄▄▄▄▄▄▄▄▀▀▓▓▓█
|
|
|
|
──████────────█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
|
|
|
|
──███─────────█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
|
|
|
|
──██──────────█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
|
|
|
|
──██──────────█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
|
|
|
|
──██─────────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
|
|
|
|
──██────────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
|
|
|
|
──██───────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▌
|
|
|
|
──██──────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▌
|
|
|
|
──██─────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▌
|
|
|
|
──██────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▌
|
|
|
|
"
|
|
|
|
|
2022-02-11 14:44:52 +03:00
|
|
|
# Make sure the current packages are installed so we can call pprettier
|
|
|
|
# via yarn because calling it via bazel results in very bad performance.
|
|
|
|
run yarn install --silent
|
|
|
|
|
2021-10-26 15:58:35 +03:00
|
|
|
# update security evidence
|
|
|
|
run security/update.sh ${security_update_args[@]:-}
|
|
|
|
|
2019-04-10 15:22:35 +03:00
|
|
|
# Check for correct copyrights
|
|
|
|
run dade-copyright-headers "$dade_copyright_arg" .
|
|
|
|
|
2020-04-24 15:04:14 +03:00
|
|
|
# We do test hlint via Bazel rules but we run it separately
|
|
|
|
# to get linting failures early.
|
2021-02-03 16:00:34 +03:00
|
|
|
if [ "$diff_mode" = "true" ]; then
|
|
|
|
check_diff $merge_base '\.hs$' hlint -j4
|
|
|
|
check_diff $merge_base '\.java$' javafmt "${javafmt_args[@]:-}"
|
2022-02-11 14:44:52 +03:00
|
|
|
check_diff $merge_base '\.\(ts\|tsx\)$' run_pprettier ${prettier_args[@]:-}
|
2020-03-23 13:22:02 +03:00
|
|
|
else
|
2021-02-03 16:00:34 +03:00
|
|
|
run hlint -j4 --git
|
2023-11-23 10:54:35 +03:00
|
|
|
java_files=$(find . -name "*.java" | grep -E -v '^\./canton(-3x)?')
|
2021-02-03 16:00:34 +03:00
|
|
|
if [[ -z "$java_files" ]]; then
|
|
|
|
echo "Unexpected: no Java file in the repository"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
run javafmt "${javafmt_args[@]:-}" ${java_files[@]:-}
|
2022-02-11 14:44:52 +03:00
|
|
|
run run_pprettier ${prettier_args[@]:-} './**/*.{ts,tsx}'
|
2020-03-23 13:22:02 +03:00
|
|
|
fi
|
2019-04-10 15:22:35 +03:00
|
|
|
|
|
|
|
# check for scala code style
|
2020-03-13 19:37:13 +03:00
|
|
|
run scalafmt "${scalafmt_args[@]:-}"
|
2019-04-10 15:22:35 +03:00
|
|
|
|
2019-04-12 14:10:16 +03:00
|
|
|
# check for Bazel build files code formatting
|
|
|
|
run bazel run "$buildifier_target"
|
2021-02-25 21:53:30 +03:00
|
|
|
|
|
|
|
# Note that we cannot use a symlink here because Windows.
|
|
|
|
if ! diff .bazelrc compatibility/.bazelrc >/dev/null; then
|
|
|
|
echo ".bazelrc and compatibility/.bazelrc are out of sync:"
|
|
|
|
diff -u .bazelrc compatibility/.bazelrc
|
|
|
|
fi
|
2023-11-15 21:22:54 +03:00
|
|
|
|
|
|
|
# check akka is not used as dependency except in navigator_maven and deprecated_maven
|
|
|
|
for f in $(ls *_install*.json | egrep -v "deprecated"); do
|
|
|
|
if grep -q akka $f; then
|
|
|
|
echo $f contains a dependency to akka
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|