Skip to content

Commit 9cdcd2d

Browse files
committed
Switch to swift-format
1 parent 62882cc commit 9cdcd2d

File tree

6 files changed

+129
-80
lines changed

6 files changed

+129
-80
lines changed

.github/workflows/ci.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ jobs:
1414
- uses: actions/checkout@v3
1515
- name: "Formatting and License Headers check"
1616
run: |
17-
SWIFTFORMAT_VERSION=0.52.0
18-
git clone --depth 1 --branch "$SWIFTFORMAT_VERSION" "https://github.com/nicklockwood/SwiftFormat" "$HOME/SwiftFormat"
19-
swift build -c release --package-path "$HOME/SwiftFormat" --product swiftformat
20-
export PATH=$PATH:"$(swift build -c release --package-path "$HOME/SwiftFormat" --show-bin-path)"
2117
./scripts/sanity.sh
2218
unit-tests:
2319
strategy:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ Examples/EchoWeb/node_modules
1919
Examples/EchoWeb/package-lock.json
2020
dev/codegen-tests/**/generated/*
2121
/scripts/.swiftformat-source/
22+
/scripts/.swift-format-source/
2223
Package.resolved
2324
*.out.*

.swift-format

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"fileScopedDeclarationPrivacy" : {
3+
"accessLevel" : "private"
4+
},
5+
"indentation" : {
6+
"spaces" : 2
7+
},
8+
"indentConditionalCompilationBlocks" : false,
9+
"indentSwitchCaseLabels" : false,
10+
"lineBreakAroundMultilineExpressionChainComponents" : false,
11+
"lineBreakBeforeControlFlowKeywords" : false,
12+
"lineBreakBeforeEachArgument" : true,
13+
"lineBreakBeforeEachGenericRequirement" : false,
14+
"lineLength" : 100,
15+
"maximumBlankLines" : 1,
16+
"prioritizeKeepingFunctionOutputTogether" : true,
17+
"respectsExistingLineBreaks" : true,
18+
"rules" : {
19+
"AllPublicDeclarationsHaveDocumentation" : false,
20+
"AlwaysUseLowerCamelCase" : false,
21+
"AmbiguousTrailingClosureOverload" : true,
22+
"BeginDocumentationCommentWithOneLineSummary" : false,
23+
"DoNotUseSemicolons" : true,
24+
"DontRepeatTypeInStaticProperties" : true,
25+
"FileScopedDeclarationPrivacy" : true,
26+
"FullyIndirectEnum" : true,
27+
"GroupNumericLiterals" : true,
28+
"IdentifiersMustBeASCII" : true,
29+
"NeverForceUnwrap" : false,
30+
"NeverUseForceTry" : false,
31+
"NeverUseImplicitlyUnwrappedOptionals" : false,
32+
"NoAccessLevelOnExtensionDeclaration" : true,
33+
"NoAssignmentInExpressions" : true,
34+
"NoBlockComments" : false,
35+
"NoCasesWithOnlyFallthrough" : true,
36+
"NoEmptyTrailingClosureParentheses" : true,
37+
"NoLabelsInCasePatterns" : false,
38+
"NoLeadingUnderscores" : false,
39+
"NoParensAroundConditions" : true,
40+
"NoVoidReturnOnFunctionSignature" : true,
41+
"OneCasePerLine" : true,
42+
"OneVariableDeclarationPerLine" : true,
43+
"OnlyOneTrailingClosureArgument" : true,
44+
"OrderedImports" : true,
45+
"ReturnVoidInsteadOfEmptyTuple" : true,
46+
"UseEarlyExits" : false,
47+
"UseLetInEveryBoundCaseVariable" : false,
48+
"UseShorthandTypeNames" : true,
49+
"UseSingleLinePropertyGetter" : false,
50+
"UseSynthesizedInitializer" : false,
51+
"UseTripleSlashForDocumentationComments" : true,
52+
"UseWhereClausesInForLoops" : false,
53+
"ValidateDocumentationComments" : false
54+
},
55+
"spacesAroundRangeFormationOperators" : true,
56+
"tabWidth" : 2,
57+
"version" : 1
58+
}

.swiftformat

Lines changed: 0 additions & 60 deletions
This file was deleted.

scripts/format.sh

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,98 @@
1616

1717
set -eu
1818

19+
function log() { printf -- "** %s\n" "$*" >&2; }
20+
function error() { printf -- "** ERROR: %s\n" "$*" >&2; }
21+
function fatal() { error "$*"; exit 1; }
22+
23+
function usage() {
24+
echo >&2 "Usage:"
25+
echo >&2 " $0 -[f|l]"
26+
echo >&2 ""
27+
echo >&2 "Options:"
28+
echo >&2 " -f Format source code in place"
29+
echo >&2 " -l Lint check without formatting the source code"
30+
}
31+
32+
lint=false
33+
while getopts ":lh" opt; do
34+
case "$opt" in
35+
l)
36+
lint=true
37+
;;
38+
h)
39+
usage
40+
exit 1
41+
;;
42+
\?)
43+
usage
44+
exit 1
45+
;;
46+
esac
47+
done
48+
49+
THIS_SCRIPT=$0
1950
HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2051
REPO="$HERE/.."
21-
SWIFTFORMAT_DIR="$HERE/.swiftformat-source"
22-
23-
# Important: if this is changed then make sure to update the version
24-
# in the .github/workflows/ci.yaml as well!
25-
SWIFTFORMAT_VERSION=0.52.0
52+
SWIFTFORMAT_DIR="$HERE/.swift-format-source"
53+
SWIFTFORMAT_VERSION=509.0.0
2654

2755
# Clone SwiftFormat if we don't already have it.
2856
if [ ! -d "$SWIFTFORMAT_DIR" ]; then
29-
echo "- Cloning SwiftFormat @ $SWIFTFORMAT_VERSION"
57+
echo "- Cloning swift-format @ $SWIFTFORMAT_VERSION"
3058
git clone \
3159
--depth 1 \
3260
--branch "$SWIFTFORMAT_VERSION" \
33-
https://github.com/nicklockwood/SwiftFormat.git \
61+
https://github.com/apple/swift-format.git \
3462
"$SWIFTFORMAT_DIR"
3563
fi
3664

3765
cd "$SWIFTFORMAT_DIR"
3866

3967
# Figure out the path for the binary.
40-
SWIFTFORMAT_BIN="$(swift build --show-bin-path -c release)/swiftformat-$SWIFTFORMAT_VERSION"
68+
SWIFTFORMAT_BIN="$(swift build --show-bin-path -c release)/swift-format-$SWIFTFORMAT_VERSION"
4169

4270
# Build it if we don't already have it.
4371
if [ ! -f "$SWIFTFORMAT_BIN" ]; then
4472
# We're not on the right tag, fetch and checkout the right one.
45-
echo "- Fetching SwiftFormat @ $SWIFTFORMAT_VERSION"
73+
echo "- Fetching swift-format @ $SWIFTFORMAT_VERSION"
4674
git fetch --depth 1 origin "refs/tags/$SWIFTFORMAT_VERSION:refs/tags/$SWIFTFORMAT_VERSION"
4775
git checkout "$SWIFTFORMAT_VERSION"
4876

4977
# Now build and name the bin appropriately.
50-
echo "- Building SwiftFormat @ $SWIFTFORMAT_VERSION"
51-
swift build -c release --product swiftformat
52-
mv "$(swift build --show-bin-path -c release)/swiftformat" "$SWIFTFORMAT_BIN"
78+
echo "- Building swift-format @ $SWIFTFORMAT_VERSION"
79+
swift build -c release --product swift-format
80+
mv "$(swift build --show-bin-path -c release)/swift-format" "$SWIFTFORMAT_BIN"
5381

5482
echo "- OK"
5583
fi
5684

57-
# Now run it.
58-
$SWIFTFORMAT_BIN "$REPO"
85+
if "$lint"; then
86+
"${SWIFTFORMAT_BIN}" lint \
87+
--parallel --recursive --strict \
88+
"${REPO}/Sources" "${REPO}/Tests" \
89+
&& SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
90+
91+
if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
92+
fatal "Running swift-format produced errors.
93+
94+
To fix, run the following command:
95+
96+
% $THIS_SCRIPT -f
97+
"
98+
exit "${SWIFT_FORMAT_RC}"
99+
fi
100+
101+
log "Ran swift-format lint with no errors."
102+
else
103+
"${SWIFTFORMAT_BIN}" format \
104+
--parallel --recursive --in-place \
105+
"${REPO}/Sources" "${REPO}/Tests" \
106+
&& SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
107+
108+
if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
109+
fatal "Running swift-format produced errors." "${SWIFT_FORMAT_RC}"
110+
fi
111+
112+
log "Ran swift-format with no errors."
113+
fi

scripts/sanity.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ function check_license_headers() {
4242
}
4343

4444
function check_formatting() {
45-
hash swiftformat 2> /dev/null || { printf "\033[0;31mERROR\033[0m swiftformat must be installed (see: https://github.com/nicklockwood/SwiftFormat)\n"; exit 1; }
46-
run_logged "Checking formatting (swiftformat $(swiftformat --version))" "swiftformat --lint --verbose $HERE/.."
45+
run_logged "Checking formatting" "$HERE/format.sh lint"
4746
}
4847

4948
errors=0

0 commit comments

Comments
 (0)