forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswift-format-wrapper.sh
More file actions
executable file
·159 lines (144 loc) · 6.05 KB
/
Copy pathswift-format-wrapper.sh
File metadata and controls
executable file
·159 lines (144 loc) · 6.05 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
# Pinned swift-format bootstrap wrapper (#9843 Ticket 02).
#
# Resolves an exact swift-format source tag, commit, and digest using the
# pinned Xcode. Caches the built binary by source identity plus toolchain
# identity, verifies the emitted version/digest, and never invokes a moving
# runner binary.
#
# Usage:
# swift-format-wrapper.sh bootstrap — build+cache from source
# swift-format-wrapper.sh version — print version after bootstrap
# swift-format-wrapper.sh digest — print the pinned commit SHA
# swift-format-wrapper.sh lint FILE… — lint --strict (exit 1 on findings)
# swift-format-wrapper.sh scope — list first-party Swift files (excludes Generated/)
# swift-format-wrapper.sh format -i FILE… — format in-place
#
# Cache: ${SWIFT_FORMAT_CACHE_DIR:-${HOME}/.cache/omi-swift-format}/<version>-<commit12>
# Override SWIFT_FORMAT_CACHE_DIR for CI (actions/cache restores the same path).
set -euo pipefail
# ── Pinned provenance ──────────────────────────────────────────────────
SWIFT_FORMAT_VERSION="602.0.0"
SWIFT_FORMAT_COMMIT="62eaad2822b865407b8cde56c36386c00800f7ec"
SWIFT_FORMAT_REPO="https://github.com/swiftlang/swift-format.git"
# ── Cache layout ───────────────────────────────────────────────────────
CACHE_DIR="${SWIFT_FORMAT_CACHE_DIR:-${HOME}/.cache/omi-swift-format}"
COMMIT12="${SWIFT_FORMAT_COMMIT:0:12}"
BUILD_DIR="${CACHE_DIR}/${SWIFT_FORMAT_VERSION}-${COMMIT12}"
# ── Project paths ─────────────────────────────────────────────────────
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
MACOS_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
CONFIG_FILE="$MACOS_DIR/Desktop/.swift-format"
GENERATED_DIR="$MACOS_DIR/Desktop/Sources/Generated"
BINARY="${BUILD_DIR}/.build/release/swift-format"
# ── Fail-closed helpers ────────────────────────────────────────────────
die() { echo "FATAL(swift-format-wrapper): $*" >&2; exit 1; }
assert_xcode() {
if ! command -v xcrun >/dev/null 2>&1; then
die "xcrun not found — run on macOS with Xcode installed"
fi
}
# ── Bootstrap ──────────────────────────────────────────────────────────
bootstrap() {
# Fast path: cached binary with verified version.
if [ -x "$BINARY" ]; then
local cached_ver
cached_ver="$("$BINARY" --version 2>&1 | head -1)"
if [ "$cached_ver" = "$SWIFT_FORMAT_VERSION" ]; then
echo "swift-format cache HIT: ${SWIFT_FORMAT_VERSION} (${COMMIT12})" >&2
return 0
fi
echo "swift-format cache stale (got '${cached_ver}'), rebuilding..." >&2
fi
assert_xcode
echo "Bootstrapping swift-format ${SWIFT_FORMAT_VERSION} from source..." >&2
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
# Clone at exact depth-1 commit for reproducibility and minimal fetch.
git clone --quiet --depth 1 --branch "$SWIFT_FORMAT_VERSION" --no-checkout "$SWIFT_FORMAT_REPO" "$BUILD_DIR"
(
cd "$BUILD_DIR"
git checkout --quiet "$SWIFT_FORMAT_COMMIT"
)
# Verify the checked-out commit matches the pin.
local actual_commit
actual_commit="$(cd "$BUILD_DIR" && git rev-parse HEAD)"
if [ "$actual_commit" != "$SWIFT_FORMAT_COMMIT" ]; then
die "commit mismatch: expected ${SWIFT_FORMAT_COMMIT}, got ${actual_commit}"
fi
# Build with the system (pinned) Xcode.
(
cd "$BUILD_DIR"
xcrun swift build -c release 2>&1
)
# Verify the built binary reports the pinned version.
if [ ! -x "$BINARY" ]; then
die "build completed but binary not found at ${BINARY}"
fi
local built_ver
built_ver="$("$BINARY" --version 2>&1 | head -1)"
if [ "$built_ver" != "$SWIFT_FORMAT_VERSION" ]; then
die "version mismatch: expected '${SWIFT_FORMAT_VERSION}', got '${built_ver}'"
fi
echo "swift-format ${SWIFT_FORMAT_VERSION} built at ${SWIFT_FORMAT_COMMIT}" >&2
}
# ── Subcommands ────────────────────────────────────────────────────────
cmd="${1:-}"
[ -n "$cmd" ] || die "usage: $0 {bootstrap|version|digest|lint|format} [args...]"
case "$cmd" in
bootstrap)
bootstrap
;;
version)
bootstrap >&2
"$BINARY" --version
;;
digest)
echo "$SWIFT_FORMAT_COMMIT"
;;
lint)
shift
bootstrap >&2
exec "$BINARY" lint --strict --configuration "$CONFIG_FILE" "$@"
;;
lint-scope)
# Verify the tree is formatter-clean: format must be a no-op on every
# file in scope. This catches true formatting drift — the lint command
# also reports advisory rules the formatter cannot auto-fix, so we use
# format idempotency as the enforcement signal.
bootstrap >&2
drift=0
while IFS= read -r f; do
if ! "$BINARY" format --configuration "$CONFIG_FILE" "$f" 2>/dev/null | diff -q "$f" - >/dev/null 2>&1; then
echo "FORMAT DRIFT: $f" >&2
drift=1
fi
done < <("$0" scope)
if [ "$drift" -eq 0 ]; then
echo "swift-format: clean (no formatting drift in scope)" >&2
fi
exit "$drift"
;;
format)
shift
bootstrap >&2
exec "$BINARY" format --configuration "$CONFIG_FILE" "$@"
;;
scope)
# List all first-party hand-written Swift files, excluding generated sources.
find "$MACOS_DIR/Desktop/Sources" "$MACOS_DIR/Desktop/Tests" \
-name '*.swift' \
-not -path "$GENERATED_DIR/*" \
| sort
;;
binary-path)
# Print the binary path without bootstrapping (for cache key computation).
echo "$BINARY"
;;
config-path)
echo "$CONFIG_FILE"
;;
*)
die "unknown subcommand: $cmd (expected bootstrap|version|digest|lint|format|scope|binary-path|config-path)"
;;
esac