Skip to content

Commit f5859f8

Browse files
committed
add ci jobs2
1 parent cbd9e85 commit f5859f8

3 files changed

Lines changed: 97 additions & 24 deletions

File tree

.githooks/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ set -euo pipefail
33

44
repo_root="$(git rev-parse --show-toplevel)"
55
"$repo_root/scripts/update-readme.sh"
6+
"$repo_root/scripts/fix-and-lint-staged-swift.sh"
67
git add "$repo_root/README.md"

.github/workflows/ci.yml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,6 @@ jobs:
4747
chmod +x scripts/check-duplicate-problems.sh
4848
./scripts/check-duplicate-problems.sh
4949
50-
swift-build-test:
51-
name: Swift Build And Test
52-
runs-on: macos-latest
53-
steps:
54-
- uses: actions/checkout@v4
55-
- name: Build and test project
56-
shell: bash
57-
run: |
58-
set -euo pipefail
59-
project="Algorithm Solutions In Swift.xcodeproj"
60-
list_output="$(xcodebuild -list -project "$project")"
61-
echo "$list_output"
62-
63-
if echo "$list_output" | sed -n '/Schemes:/,$p' | grep -Fq "Algorithm Solutions In Swift"; then
64-
xcodebuild \
65-
-project "$project" \
66-
-scheme "Algorithm Solutions In Swift" \
67-
-destination "platform=macOS" \
68-
build test
69-
else
70-
xcodebuild -project "$project" -target "Algorithm Solutions In Swift" build
71-
xcodebuild -project "$project" -target "Algorithm Solutions In Swift Tests" build
72-
fi
73-
7450
swift-format-lint-check:
7551
name: Swift Format And Lint Check
7652
runs-on: macos-latest
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
5+
cd "$repo_root"
6+
7+
staged_swift_files="$(mktemp)"
8+
tabs_report="$(mktemp)"
9+
trailing_ws_report="$(mktemp)"
10+
tabs_tmp="$(mktemp)"
11+
trailing_tmp="$(mktemp)"
12+
trap 'rm -f "$staged_swift_files" "$tabs_report" "$trailing_ws_report" "$tabs_tmp" "$trailing_tmp"' EXIT
13+
14+
git diff --cached --name-only --diff-filter=ACMR -- '*.swift' > "$staged_swift_files" || true
15+
16+
if [[ ! -s "$staged_swift_files" ]]; then
17+
echo "No staged Swift files to format/lint."
18+
exit 0
19+
fi
20+
21+
swift_format_available=0
22+
if command -v swift-format >/dev/null 2>&1; then
23+
swift_format_available=1
24+
elif xcrun --find swift-format >/dev/null 2>&1; then
25+
swift_format_available=2
26+
fi
27+
28+
run_swift_format() {
29+
local mode="$1"
30+
local file="$2"
31+
32+
if [[ "$swift_format_available" -eq 1 ]]; then
33+
swift-format "$mode" "$file"
34+
elif [[ "$swift_format_available" -eq 2 ]]; then
35+
xcrun swift-format "$mode" "$file"
36+
fi
37+
}
38+
39+
# Auto-fix pass for staged Swift files.
40+
while IFS= read -r file; do
41+
[[ -z "$file" ]] && continue
42+
[[ -f "$file" ]] || continue
43+
44+
perl -i -pe 's/[ \t]+$//' "$file"
45+
if [[ "$swift_format_available" -ne 0 ]]; then
46+
run_swift_format format "$file"
47+
fi
48+
49+
git add "$file"
50+
done < "$staged_swift_files"
51+
52+
# Lint pass for staged Swift files.
53+
errors=0
54+
while IFS= read -r file; do
55+
[[ -z "$file" ]] && continue
56+
[[ -f "$file" ]] || continue
57+
58+
if grep -n $'\t' "$file" > "$tabs_tmp" 2>/dev/null; then
59+
sed "s#^#$file:#" "$tabs_tmp" >> "$tabs_report"
60+
fi
61+
62+
if grep -n -E "[[:blank:]]$" "$file" > "$trailing_tmp" 2>/dev/null; then
63+
sed "s#^#$file:#" "$trailing_tmp" >> "$trailing_ws_report"
64+
fi
65+
done < "$staged_swift_files"
66+
67+
if [[ -s "$tabs_report" ]]; then
68+
head -n 30 "$tabs_report" >&2
69+
echo "Swift lint failed: tabs detected in staged files." >&2
70+
errors=1
71+
fi
72+
73+
if [[ -s "$trailing_ws_report" ]]; then
74+
head -n 30 "$trailing_ws_report" >&2
75+
echo "Swift lint failed: trailing whitespace detected in staged files." >&2
76+
errors=1
77+
fi
78+
79+
if [[ "$swift_format_available" -ne 0 ]]; then
80+
while IFS= read -r file; do
81+
[[ -z "$file" ]] && continue
82+
[[ -f "$file" ]] || continue
83+
if ! run_swift_format lint "$file"; then
84+
echo "Swift-format lint failed for $file." >&2
85+
errors=1
86+
fi
87+
done < "$staged_swift_files"
88+
else
89+
echo "swift-format not found; format/lint step skipped for swift-format."
90+
fi
91+
92+
if [[ "$errors" -ne 0 ]]; then
93+
exit 1
94+
fi
95+
96+
echo "Staged Swift format/lint checks passed."

0 commit comments

Comments
 (0)