2020-06-12 18:32:13 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-06-18 13:44:13 +00:00
|
|
|
|
2022-10-20 22:23:14 +00:00
|
|
|
. $(dirname $0)/format-version.sh
|
2021-03-10 00:16:36 +00:00
|
|
|
|
2021-05-12 11:36:00 +00:00
|
|
|
cd "$(dirname $0)/../"
|
2022-10-20 22:23:14 +00:00
|
|
|
|
2023-10-24 17:54:20 +00:00
|
|
|
sources=($(find jni daemon llarp include pybind | grep -E '\.([hc](pp)?|m(m)?)$' | grep -v '#'))
|
|
|
|
|
|
|
|
incl_pat='^(#include +)"(llarp|libntrup|oxen|oxenc|oxenmq|quic|CLI|cpr|nlohmann|ghc|fmt|spdlog|uvw?)([/.][^"]*)"'
|
|
|
|
|
2021-05-12 11:36:00 +00:00
|
|
|
if [ "$1" = "verify" ] ; then
|
2023-10-24 17:54:20 +00:00
|
|
|
if [ $($CLANG_FORMAT --output-replacements-xml "${sources[@]}" | grep '</replacement>' | wc -l) -ne 0 ] ; then
|
2021-10-15 00:02:23 +00:00
|
|
|
exit 2
|
2021-05-14 17:07:44 +00:00
|
|
|
fi
|
2023-10-24 17:54:20 +00:00
|
|
|
|
|
|
|
if grep --color -E "$incl_pat" "${sources[@]}"; then
|
|
|
|
exit 5
|
|
|
|
fi
|
2021-05-12 11:36:00 +00:00
|
|
|
else
|
2023-10-24 17:54:20 +00:00
|
|
|
$CLANG_FORMAT -i "${sources[@]}" &> /dev/null
|
|
|
|
|
|
|
|
perl -pi -e "s{$incl_pat}"'{$1<$2$3>}' "${sources[@]}" &> /dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Some includes just shouldn't exist anywhere, but need to be fixed manually:
|
|
|
|
if grep --color -E '^#include ([<"]external/|<bits/|<.*/impl)' "${sources[@]}"; then
|
|
|
|
echo "Format failed: bad includes detected that can't be auto-corrected"
|
|
|
|
exit 5
|
2021-05-12 11:36:00 +00:00
|
|
|
fi
|
2021-05-14 17:07:44 +00:00
|
|
|
|
2021-10-15 00:02:23 +00:00
|
|
|
swift_format=$(command -v swiftformat 2>/dev/null)
|
2021-05-14 17:07:44 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
if [ "$1" = "verify" ] ; then
|
2022-09-19 15:22:28 +00:00
|
|
|
for f in $(find daemon | grep -E '\.swift$' | grep -v '#') ; do
|
2021-05-14 17:07:44 +00:00
|
|
|
if [ $($swift_format --quiet --dryrun < "$f" | diff "$f" - | wc -l) -ne 0 ] ; then
|
2021-10-15 00:02:23 +00:00
|
|
|
exit 3
|
2021-05-14 17:07:44 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
2022-09-19 15:22:28 +00:00
|
|
|
$swift_format --quiet $(find daemon | grep -E '\.swift$' | grep -v '#')
|
2021-05-14 17:07:44 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
2021-10-15 00:01:33 +00:00
|
|
|
|
|
|
|
jsonnet_format=$(command -v jsonnetfmt 2>/dev/null)
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
if [ "$1" = "verify" ]; then
|
|
|
|
if ! $jsonnet_format --test .drone.jsonnet; then
|
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
$jsonnet_format --in-place .drone.jsonnet
|
|
|
|
fi
|
|
|
|
fi
|