-
Notifications
You must be signed in to change notification settings - Fork 59
/
run-swift-format.sh
executable file
·70 lines (61 loc) · 2.19 KB
/
run-swift-format.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
# Get the project directory from the current script
CUR_DIR=$(dirname "$0")
PROJECT_DIR=$( cd "${CUR_DIR}/../" ; pwd -P )
# The targets that we are checking/formatting as parameters to `swift package plugin`
# Only top-level targets need to be present here
TARGETS_ARGS="--target CLI --target Driver --target FrontEnd --target Core --target IR --target CodeGenLLVM --target StandardLibrary --target Utils --target UtilsTests --target HyloTests --target DriverTests"
function usage {
echo "Usage: $(basename $0) [-h] [-d] fix|lint|fix_lint"
echo " -h Display help"
echo " -d Use Debug swift-format builds (faster to compile swift-format)"
echo " fix Run swift-format to correct formatting"
echo " lint Run swift-format to check for formatting errors"
echo " fix_lint Run swift-format to correct formatting and then check for formatting errors"
}
SWIFT_FORMAT_BUILD_MODE=release
while getopts ":hd" opt; do
case $opt in
h)
usage
exit 0
;;
d)
SWIFT_FORMAT_BUILD_MODE=debug
;;
*)
echo "Invalid parameters"
usage
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
SHOULD_LINT=0
SHOULD_FIX=0
MODE=$1
case $MODE in
fix)
SHOULD_FIX=1
;;
lint)
SHOULD_LINT=1
;;
fix_lint)
SHOULD_FIX=1
SHOULD_LINT=1
;;
*)
usage
exit 1
esac
# Check the swift-format executable.
# If we find the executable at the expected location, use it directly;
# otherwise, use it through `swift run`.
SWIFT_FORMAT="swift run -c ${SWIFT_FORMAT_BUILD_MODE} swift-format"
if [ -f "${PROJECT_DIR}/.build/${SWIFT_FORMAT_BUILD_MODE}/swift-format" ]; then
SWIFT_FORMAT="${PROJECT_DIR}/.build/${SWIFT_FORMAT_BUILD_MODE}/swift-format"
fi
COMMON_ARGS="--configuration ${PROJECT_DIR}/.swift-format.json -r -p ${PROJECT_DIR}/Sources ${PROJECT_DIR}/Tests ${PROJECT_DIR}/*.swift"
[ $SHOULD_FIX -eq 1 ] && ${SWIFT_FORMAT} -i ${COMMON_ARGS} && echo "Formatted the source code."
[ $SHOULD_LINT -eq 1 ] && ${SWIFT_FORMAT} lint -s ${COMMON_ARGS} && echo "Linted the source code."