-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-swift-format.sh
More file actions
executable file
·88 lines (79 loc) · 2.68 KB
/
Copy pathrun-swift-format.sh
File metadata and controls
executable file
·88 lines (79 loc) · 2.68 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bash
# Swift Format Check / Fix Script
#
# This script runs `swift-format` on all Swift source files in the repository.
#
# Default behavior:
# - Runs `swift format lint --strict`
# - Fails if formatting violations are found
#
# Optional behavior:
# - If `--fix` is passed, runs `swift format format --in-place`
# to automatically fix formatting issues
#
# The script ensures that required configuration files
# (.swift-format and .swiftformatignore) are present by downloading
# them from the central workflow repository if missing.
#
# Intended usage:
# - CI: enforce consistent Swift formatting
# - Local: optionally auto-fix formatting before committing
set -euo pipefail
# Logging helpers
# All output is written to stderr for consistent CI and local logs
log() { printf -- "** %s\n" "$*" >&2; }
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
fatal() {
error "$@"
exit 1
}
# Determine swift-format command mode
#
# By default, run in strict lint mode.
# If --fix is provided, switch to in-place formatting.
FORMAT_COMMAND=(lint --strict)
for arg in "$@"; do
if [ "$arg" == "--fix" ]; then
FORMAT_COMMAND=(format --in-place)
fi
done
# Base URL for shared swift-format configuration files
URL="https://raw.githubusercontent.com/BinaryBirds/github-workflows/refs/heads/main"
# Ensure .swift-format configuration exists
#
# This file defines formatting rules used by swift-format.
# If missing, it is downloaded to keep formatting consistent
# across repositories.
if [ ! -f ".swift-format" ]; then
log ".swift-format does not exist. Downloading..."
curl -o ".swift-format" "$URL/.swift-format"
fi
# Ensure .swiftformatignore exists
#
# This file defines paths that should be excluded from formatting.
# If missing, it is downloaded from the shared configuration.
if [ ! -f ".swiftformatignore" ]; then
log ".swiftformatignore does not exist. Downloading..."
curl -o ".swiftformatignore" "$URL/.swiftformatignore"
fi
# Run swift-format
#
# - Uses git to list tracked and untracked Swift files
# - Applies exclusions defined in .swiftformatignore
# - Runs formatting in parallel for performance
#
# The exit code is captured explicitly to allow controlled error handling.
tr '\n' '\0' <.swiftformatignore |
xargs -0 -I% printf '":(exclude)%" ' |
xargs git ls-files -z --cached --others --exclude-standard '*.swift' |
xargs -0 swift format "${FORMAT_COMMAND[@]}" --parallel &&
SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
# If swift-format failed, print a helpful error message
if [ "${SWIFT_FORMAT_RC}" -ne 0 ]; then
fatal "❌ Running swift-format produced errors.
To fix:
% run make format
"
fi
# Success
log "✅ Ran swift-format with no errors."