Skip to content

Commit 4d34083

Browse files
authored
Use swift-format for Swift file formatting (#261)
1 parent e128658 commit 4d34083

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

.github/workflows/swiftfmt.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Swift format
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
paths:
8+
- ".github/workflows/swiftfmt.yml"
9+
- "**.swift"
10+
jobs:
11+
swiftfmt:
12+
name: Run swift format
13+
runs-on: macos-latest
14+
steps:
15+
- name: Check out code
16+
uses: actions/checkout@v4
17+
with:
18+
show-progress: false
19+
20+
# Re-enable in follow-up PR that does the formatting.
21+
# - name: Check formating
22+
# run: ./swift-format.sh check

.swiftformatignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ios/Pods/**

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ dart format lib/ test/ -l 120
4141

4242
In Android Studio, set the line length using Preferences -> Editor -> Code Style -> Dart -> Line length, set it to 120. Enable auto-format with Preferences -> Languages & Frameworks -> Flutter -> Format code on save.
4343

44+
`./swift-format.sh` can be used to format Swift code in the repo.
45+
46+
Once `swift-format` supports ignoring directories (<https://github.com/swiftlang/swift-format/issues/870>), we can move to a method of running it more like what <https://calebhearth.com/swift-format-github-action> describes.
4447

4548
# Release
4649

swift-format.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the Swift.org open source project
5+
##
6+
## Copyright (c) 2024 Apple Inc. and the Swift project authors
7+
## Licensed under Apache License v2.0 with Runtime Library Exception
8+
##
9+
## See https://swift.org/LICENSE.txt for license information
10+
## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
##
12+
##===----------------------------------------------------------------------===##
13+
14+
# Vendored from <https://github.com/swiftlang/github-workflows/blob/main/.github/workflows/scripts/check-swift-format.sh> while <https://github.com/swiftlang/swift-format/issues/870> is open.
15+
16+
# This file has been modified to only check formatting, with no linting, and to require a `check` command flag to fail when formatting was performed.
17+
18+
set -euo pipefail
19+
20+
log() { printf -- "** %s\n" "$*" >&2; }
21+
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
22+
fatal() { error "$@"; exit 1; }
23+
24+
25+
if [[ -f .swiftformatignore ]]; then
26+
log "Found swiftformatignore file..."
27+
28+
log "Running swift format format..."
29+
tr '\n' '\0' < .swiftformatignore| xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files -z '*.swift' | xargs -0 xcrun swift-format --parallel --recursive --in-place
30+
31+
# log "Running swift format lint..."
32+
33+
# tr '\n' '\0' < .swiftformatignore | xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel
34+
else
35+
log "Running swift format format..."
36+
git ls-files -z '*.swift' | xargs -0 xcrun swift-format --parallel --recursive --in-place
37+
38+
# log "Running swift format lint..."
39+
40+
# git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel
41+
fi
42+
43+
44+
if [ "${1-default}" = "check" ]; then
45+
log "Checking for modified files..."
46+
47+
GIT_PAGER='' git diff --exit-code '*.swift'
48+
49+
log "✅ Found no formatting issues."
50+
fi

0 commit comments

Comments
 (0)