forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.run-format.zsh
executable file
·271 lines (222 loc) · 8 KB
/
.run-format.zsh
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
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env zsh
builtin emulate -L zsh
setopt EXTENDED_GLOB
setopt PUSHD_SILENT
setopt ERR_EXIT
setopt ERR_RETURN
setopt NO_UNSET
setopt PIPE_FAIL
setopt NO_AUTO_PUSHD
setopt NO_PUSHD_IGNORE_DUPS
setopt FUNCTION_ARGZERO
## Enable for script debugging
# setopt WARN_CREATE_GLOBAL
# setopt WARN_NESTED_VAR
# setopt XTRACE
autoload -Uz is-at-least && if ! is-at-least 5.2; then
print -u2 -PR "%F{1}${funcstack[1]##*/}:%f Running on Zsh version %B${ZSH_VERSION}%b, but Zsh %B5.2%b is the minimum supported version. Upgrade zsh to fix this issue."
exit 1
fi
invoke_formatter() {
if (( # < 1 )) {
log_error "Usage invoke_formatter [formatter_name]"
exit 2
}
local formatter="${1}"
shift
local -a source_files=(${@})
case ${formatter} {
clang)
if (( ${+commands[clang-format-17]} )) {
local formatter=clang-format-17
} elif (( ${+commands[clang-format]} )) {
local formatter=clang-format
} else {
log_error "No viable clang-format version found (required 17.0.3)"
exit 2
}
local -a formatter_version=($(${formatter} --version))
if ! is-at-least 17.0.3 ${formatter_version[-1]}; then
log_error "clang-format is not version 17.0.3 or above (found ${formatter_version[-1]}."
exit 2
fi
if ! is-at-least ${formatter_version[-1]} 17.0.3; then
log_error "clang-format is more recent than version 17.0.3 (found ${formatter_version[-1]})."
exit 2
fi
if (( ! #source_files )) source_files=((libobs|libobs-*|UI|plugins|deps|shared)/**/*.(c|cpp|h|hpp|m|mm)(.N))
source_files=(${source_files:#*/(obs-websocket/deps|decklink/*/decklink-sdk|mac-syphon/syphon-framework|libdshowcapture)/*})
local -a format_args=(-style=file -fallback-style=none)
if (( _loglevel > 2 )) format_args+=(--verbose)
check_files() {
local -i num_failures=0
local -a source_files=($@)
local file
local -a format_args=(-style=file -fallback-style=none)
if (( _loglevel > 2 )) format_args+=(--verbose)
local -a command=(${formatter} ${format_args})
for file (${source_files}) {
if ! ${command} "${file}" | diff -q "${file}" - &> /dev/null; then
log_error "${file} requires formatting changes."
if (( fail_on_error == 2 )) return 2;
num_failures=$(( num_failures + 1 ))
fi
}
if (( num_failures && fail_on_error == 1 )) return 2
}
format_files() {
local -a source_files=($@)
if (( ${#source_files} )) {
local -a format_args=(-style=file -fallback-style=none -i)
if (( _loglevel > 2 )) format_args+=(--verbose)
"${formatter}" ${format_args} ${source_files}
}
}
;;
gersemi)
local formatter=gersemi
if (( ${+commands[gersemi]} )) {
local gersemi_version=($(gersemi --version))
if ! is-at-least 0.12.0 ${gersemi_version[2]}; then
log_error "gersemi is not version 0.12.0 or above (found ${gersemi_version[2]}."
exit 2
fi
}
if (( ! #source_files )) source_files=(CMakeLists.txt (libobs|libobs-*|UI|plugins|deps|shared|cmake|test)/**/(CMakeLists.txt|*.cmake)(.N))
source_files=(${source_files:#*/(jansson|decklink/*/decklink-sdk|obs-websocket|obs-browser|libdshowcapture)/*})
source_files=(${source_files:#(cmake/Modules/*|*/legacy.cmake)})
check_files() {
local -i num_failures=0
local -a source_files=($@)
local file
local -a command=(${formatter} -c --no-cache ${source_files})
if (( ${#source_files} )) {
while read -r line; do
local -a line_tokens=(${(z)line})
file=${line_tokens[1]//*obs-studio\//}
log_error "${file} requires formatting changes."
if (( fail_on_error == 2 )) return 2
num_failures=$(( num_failures + 1 ))
done < <(${command} 2>&1)
if (( num_failures && fail_on_error == 1 )) return 2
}
}
format_files() {
local -a source_files=($@)
if (( ${#source_files} )) {
"${formatter}" -i ${source_files}
}
}
;;
swift)
local formatter=swift-format
if (( ${+commands[swift-format]} )) {
local swift_format_version=$(swift-format --version)
if ! is-at-least 508.0.0 ${swift_format_version}; then
log_error "swift-format is not version 508.0.0 or above (found ${swift_format_version})."
exit 2
fi
} else {
log_error "No viable swift-format version found (required 508.0.0)"
exit 2
}
if (( ! #source_files )) source_files=((libobs|libobs-*|UI|plugins)/**/*.swift(.N))
check_files() {
local -i num_failures=0
local -a source_files=($@)
local file
local -a format_args=()
local -a command=(${formatter} ${format_args})
for file (${source_files}) {
if ! "${command}" "${file}" | diff -q "${file}" - &> /dev/null; then
log_error "${file} requires formatting changes."
if (( fail_on_error == 2 )) return 2;
num_failures=$(( num_failures + 1 ))
fi
}
if (( num_failures && fail_on_error == 1 )) return 2
}
format_files() {
local -a source_files=($@)
if (( ${#source_files} )) {
local -a format_args=(-i)
"${formatter}" ${format_args} ${source_files}
}
}
;;
*) log_error "Invalid formatter specified: ${1}. Valid options are clang-format, gersemi, and swift-format."; exit 2 ;;
}
local file
local -i num_failures=0
if (( check_only )) {
if (( ${+functions[check_files]} )) {
check_files ${source_files}
} else {
log_error "No format check function defined for formatter '${formatter}'"
exit 2
}
} else {
if (( ${+functions[format_files]} )) {
format_files ${source_files}
} else {
log_error "No format function defined for formatter '${formatter}'"
exit 2
}
}
}
run_format() {
if (( ! ${+SCRIPT_HOME} )) typeset -g SCRIPT_HOME=${ZSH_ARGZERO:A:h}
if (( ! ${+FORMATTER_NAME} )) typeset -g FORMATTER_NAME=${${(s:-:)ZSH_ARGZERO:t:r}[2]}
typeset -g host_os=${${(L)$(uname -s)}//darwin/macos}
local -i fail_on_error=0
local -i check_only=0
local -i verbosity=1
local -r _version='1.0.0'
fpath=("${SCRIPT_HOME}/.functions" ${fpath})
autoload -Uz set_loglevel log_info log_error log_output log_status log_warning
local -r _usage="
Usage: %B${functrace[1]%:*}%b <option>
%BOptions%b:
%F{yellow} Formatting options%f
-----------------------------------------------------------------------------
%B-c | --check%b Check only, no actual formatting takes place
%F{yellow} Output options%f
-----------------------------------------------------------------------------
%B-v | --verbose%b Verbose (more detailed output)
%B--fail-[never|error] Fail script never/on formatting change - default: %B%F{green}never%f%b
%B--debug%b Debug (very detailed and added output)
%F{yellow} General options%f
-----------------------------------------------------------------------------
%B-h | --help%b Print this usage help
%B-V | --version%b Print script version information"
local -a args
while (( # )) {
case ${1} {
-c|--check) check_only=1; shift ;;
-v|--verbose) (( verbosity += 1 )); shift ;;
-h|--help) log_output ${_usage}; exit 0 ;;
-V|--version) print -Pr "${_version}"; exit 0 ;;
--debug) verbosity=3; shift ;;
--fail-never)
fail_on_error=0
shift
;;
--fail-error)
fail_on_error=1
shift
;;
--fail-fast)
fail_on_error=2
shift
;;
*)
args+=($@)
break
;;
}
}
set -- ${(@)args}
set_loglevel ${verbosity}
invoke_formatter ${FORMATTER_NAME} ${args}
}
run_format ${@}