-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-swift-headers.sh
More file actions
executable file
·260 lines (227 loc) · 6.7 KB
/
Copy pathcheck-swift-headers.sh
File metadata and controls
executable file
·260 lines (227 loc) · 6.7 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
set -euo pipefail
# Swift Header Checker / Fixer
#
# - Verifies that Swift source files contain a standardized header
# - Can optionally fix or insert headers in-place (--fix)
# - Uses git to determine file creation dates where possible
# - Supports excluding files via .swiftheaderignore
#
# Intended usage:
# - CI: fail if headers are missing or malformed
# - Local: optionally auto-fix headers
# Logging helpers (stderr, consistent formatting)
log() { printf -- "** %s\n" "$*" >&2; }
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
fatal() {
error "$@"
exit 1
}
# Configuration / state
DEFAULT_AUTHOR="Binary Birds"
FIX_MODE=0
HAS_ERRORS=0
# Argument parsing
while [ $# -gt 0 ]; do
case "$1" in
--fix)
FIX_MODE=1
;;
--author)
shift
[ -z "${1:-}" ] && fatal "--author requires a value"
DEFAULT_AUTHOR="$1"
;;
*)
fatal "Unknown argument: $1"
;;
esac
shift
done
if [ "$FIX_MODE" -eq 1 ]; then
log "Fix mode enabled — headers will be inserted or corrected."
else
log "Checking Swift file headers..."
fi
# Project name
PROJECT_NAME="$(basename "$PWD")"
# Date helpers
normalize_date() {
local raw="$1"
if command -v gdate >/dev/null 2>&1; then
gdate -d "$raw" +"%Y. %m. %d" 2>/dev/null
else
date -d "$raw" +"%Y. %m. %d" 2>/dev/null
fi
}
get_file_creation_date() {
local file="$1"
if git ls-files --error-unmatch "$file" >/dev/null 2>&1; then
git log -1 --format="%ad" --date=format:"%Y. %m. %d" -- "$file"
else
date +"%Y. %m. %d"
fi
}
# Header detection helpers
# Find the line number of the "Created by" line (header anchor)
find_header_line() {
grep -nE '^// Created by .+ on [0-9]{4}\. [0-9]{2}\. [0-9]{2}\.{1,2}$' "$1" |
head -n1 |
cut -d: -f1
}
# Validate header structure relative to the anchor line
is_header_valid_at_line() {
local file="$1"
local line="$2"
local filename
filename=$(basename "$file")
sed -n "$((line - 4)),${line}p" "$file" | awk -v f="$filename" -v p="$PROJECT_NAME" '
NR==1 && $0=="//" {next}
NR==2 && $0=="// " f {next}
NR==3 && $0=="// " p {next}
NR==4 && $0=="//" {next}
NR==5 && $0 ~ "^// Created by .+ on [0-9]{4}\\. [0-9]{2}\\. [0-9]{2}\\.\\.{0,1}$" {exit 0}
{exit 1}
'
}
extract_author_from_header_line() {
sed -E 's|^// Created by (.+) on [0-9]{4}\. [0-9]{2}\. [0-9]{2}\.{1,2}$|\1|'
}
extract_date_from_header_line() {
sed -E 's|^// Created by .+ on ([0-9]{4}\. [0-9]{2}\. [0-9]{2})\.{1,2}$|\1|'
}
# Returns 0 when the header line uses legacy double-dot suffix.
is_legacy_double_dot_header_line() {
[[ "$1" =~ \.\.$ ]]
}
# Replace an invalid header block in place
replace_header_at_line() {
local file="$1"
local line="$2"
local tmpfile
tmpfile=$(mktemp)
# Extract original author and date
local header_line
header_line=$(sed -n "${line}p" "$file")
local author
author=$(printf '%s\n' "$header_line" | extract_author_from_header_line)
local date
date=$(printf '%s\n' "$header_line" | extract_date_from_header_line)
# Safety fallback (should never happen)
[ -z "$author" ] && author="$DEFAULT_AUTHOR"
[ -z "$date" ] && date="$(get_file_creation_date "$file")"
# Remove the old header block
local start=$((line - 4))
[ "$start" -lt 1 ] && start=1
sed "${start},${line}d" "$file" >"$tmpfile"
# Rebuild file WITHOUT adding extra blank lines
{
echo "//"
echo "// $(basename "$file")"
echo "// $PROJECT_NAME"
echo "//"
echo "// Created by $author on $date."
cat "$tmpfile"
} >"$tmpfile.fixed"
mv "$tmpfile.fixed" "$file"
}
# Header validation / fixing
check_or_fix_header() {
local file="$1"
local filename
filename=$(basename "$file")
# Explicitly ignore Package.swift
[ "$filename" = "Package.swift" ] && return 0
local header_line
header_line=$(find_header_line "$file" || true)
if [ -n "$header_line" ]; then
if is_header_valid_at_line "$file" "$header_line"; then
if [ "$FIX_MODE" -eq 1 ]; then
local header_text
header_text=$(sed -n "${header_line}p" "$file")
if is_legacy_double_dot_header_line "$header_text"; then
replace_header_at_line "$file" "$header_line"
log "Normalized legacy header: $file"
fi
fi
return 0
fi
if [ "$FIX_MODE" -eq 1 ]; then
replace_header_at_line "$file" "$header_line"
log "Fixed header: $file"
else
error "❌ $file - Header is invalid"
return 1
fi
else
# No header anywhere → insert at top
if [ "$FIX_MODE" -eq 1 ]; then
local tmpfile
tmpfile=$(mktemp)
{
echo "//"
echo "// $filename"
echo "// $PROJECT_NAME"
echo "//"
echo "// Created by $DEFAULT_AUTHOR on $(get_file_creation_date "$file")."
echo ""
cat "$file"
} >"$tmpfile"
mv "$tmpfile" "$file"
log "Header added: $file"
else
error "❌ $file - Header missing"
return 1
fi
fi
}
# Exclusions
IGNORE_FILE=".swiftheaderignore"
EXCLUDE_PATTERNS=()
if [ -f "$IGNORE_FILE" ]; then
log "Using exclusion list from $IGNORE_FILE"
while IFS= read -r line || [ -n "$line" ]; do
[[ -n "$line" && ! "$line" =~ ^# ]] && EXCLUDE_PATTERNS+=(":(exclude)$line")
done <"$IGNORE_FILE"
else
EXCLUDE_PATTERNS+=(
":(exclude).*"
":(exclude)*.txt"
":(exclude)*.png"
":(exclude)*.jpeg"
":(exclude)*.jpg"
":(exclude)*.sh"
":(exclude)*.html"
":(exclude)*.yaml"
":(exclude)README.md"
":(exclude)Package.resolved"
":(exclude)Makefile"
":(exclude)LICENSE"
":(exclude)Package*.swift"
":(exclude)docker/**"
)
fi
# File processing
FILES=()
while IFS= read -r -d '' file; do
FILES+=("$file")
done < <(git ls-files -z --cached --others --exclude-standard "${EXCLUDE_PATTERNS[@]}")
for file in "${FILES[@]}"; do
if ! check_or_fix_header "$file"; then
HAS_ERRORS=1
fi
done
# Final result
if [ "$HAS_ERRORS" -eq 1 ]; then
if [ "$FIX_MODE" -eq 1 ]; then
log "⚠️ Some headers were fixed."
else
fatal "Some files have header issues."
fi
else
if [ "$FIX_MODE" -eq 1 ]; then
log "✅ Headers updated successfully."
else
log "✅ All headers are valid."
fi
fi