| 1 |
#!/usr/bin/env zsh |
| 2 |
|
| 3 |
builtin emulate -L zsh |
| 4 |
setopt EXTENDED_GLOB |
| 5 |
setopt PUSHD_SILENT |
| 6 |
setopt ERR_EXIT |
| 7 |
setopt ERR_RETURN |
| 8 |
setopt NO_UNSET |
| 9 |
setopt PIPE_FAIL |
| 10 |
setopt NO_AUTO_PUSHD |
| 11 |
setopt NO_PUSHD_IGNORE_DUPS |
| 12 |
setopt FUNCTION_ARGZERO |
| 13 |
|
| 14 |
## Enable for script debugging |
| 15 |
# setopt WARN_CREATE_GLOBAL |
| 16 |
# setopt WARN_NESTED_VAR |
| 17 |
# setopt XTRACE |
| 18 |
|
| 19 |
autoload -Uz is-at-least && if ! is-at-least 5.8; then |
| 20 |
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." |
| 21 |
exit 1 |
| 22 |
fi |
| 23 |
|
| 24 |
invoke_formatter() { |
| 25 |
if (( # < 1 )) { |
| 26 |
log_error "Usage invoke_formatter [formatter_name]" |
| 27 |
exit 2 |
| 28 |
} |
| 29 |
|
| 30 |
local formatter="${1}" |
| 31 |
shift |
| 32 |
local -a source_files=(${@}) |
| 33 |
|
| 34 |
case ${formatter} { |
| 35 |
clang) |
| 36 |
if (( ${+commands[clang-format-17]} )) { |
| 37 |
local formatter=clang-format-17 |
| 38 |
} elif (( ${+commands[clang-format]} )) { |
| 39 |
local formatter=clang-format |
| 40 |
} else { |
| 41 |
log_error "No viable clang-format version found (required 17.0.3)" |
| 42 |
exit 2 |
| 43 |
} |
| 44 |
|
| 45 |
local -a formatter_version=($(${formatter} --version)) |
| 46 |
|
| 47 |
if ! is-at-least 17.0.3 ${formatter_version[-1]}; then |
| 48 |
log_error "clang-format is not version 17.0.3 or above (found ${formatter_version[-1]}." |
| 49 |
exit 2 |
| 50 |
fi |
| 51 |
|
| 52 |
if ! is-at-least ${formatter_version[-1]} 17.0.3; then |
| 53 |
log_error "clang-format is more recent than version 17.0.3 (found ${formatter_version[-1]})." |
| 54 |
exit 2 |
| 55 |
fi |
| 56 |
|
| 57 |
if (( ! #source_files )) source_files=(src/**/*.(c|cpp|h|hpp|m|mm)(.N)) |
| 58 |
|
| 59 |
local -a format_args=(-style=file -fallback-style=none) |
| 60 |
if (( _loglevel > 2 )) format_args+=(--verbose) |
| 61 |
|
| 62 |
check_files() { |
| 63 |
local -i num_failures=0 |
| 64 |
local -a source_files=($@) |
| 65 |
local file |
| 66 |
local -a format_args=(-style=file -fallback-style=none) |
| 67 |
if (( _loglevel > 2 )) format_args+=(--verbose) |
| 68 |
|
| 69 |
local -a command=(${formatter} ${format_args}) |
| 70 |
|
| 71 |
for file (${source_files}) { |
| 72 |
if ! ${command} "${file}" | diff -q "${file}" - &> /dev/null; then |
| 73 |
log_error "${file} requires formatting changes." |
| 74 |
if (( fail_on_error == 2 )) return 2; |
| 75 |
num_failures=$(( num_failures + 1 )) |
| 76 |
fi |
| 77 |
} |
| 78 |
if (( num_failures && fail_on_error == 1 )) return 2 |
| 79 |
} |
| 80 |
|
| 81 |
format_files() { |
| 82 |
local -a source_files=($@) |
| 83 |
|
| 84 |
if (( ${#source_files} )) { |
| 85 |
local -a format_args=(-style=file -fallback-style=none -i) |
| 86 |
if (( _loglevel > 2 )) format_args+=(--verbose) |
| 87 |
|
| 88 |
"${formatter}" ${format_args} ${source_files} |
| 89 |
} |
| 90 |
} |
| 91 |
;; |
| 92 |
gersemi) |
| 93 |
local formatter=gersemi |
| 94 |
if (( ${+commands[gersemi]} )) { |
| 95 |
local gersemi_version=($(gersemi --version)) |
| 96 |
|
| 97 |
if ! is-at-least 0.12.0 ${gersemi_version[2]}; then |
| 98 |
log_error "gersemi is not version 0.12.0 or above (found ${gersemi_version[2]}." |
| 99 |
exit 2 |
| 100 |
fi |
| 101 |
} |
| 102 |
|
| 103 |
if (( ! #source_files )) source_files=(CMakeLists.txt (cmake)/**/(CMakeLists.txt|*.cmake)(.N)) |
| 104 |
|
| 105 |
check_files() { |
| 106 |
local -i num_failures=0 |
| 107 |
local -a source_files=($@) |
| 108 |
local file |
| 109 |
local -a command=(${formatter} -c --no-cache ${source_files}) |
| 110 |
|
| 111 |
if (( ${#source_files} )) { |
| 112 |
while read -r line; do |
| 113 |
local -a line_tokens=(${(z)line}) |
| 114 |
if (( #line_tokens )) { |
| 115 |
file=${line_tokens[1]//*${project_root}\//} |
| 116 |
|
| 117 |
log_error "${file} requires formatting changes." |
| 118 |
} else { |
| 119 |
log_error "${line}" |
| 120 |
} |
| 121 |
|
| 122 |
if (( fail_on_error == 2 )) return 2 |
| 123 |
num_failures=$(( num_failures + 1 )) |
| 124 |
done < <(${command} 2>&1) |
| 125 |
|
| 126 |
if (( num_failures && fail_on_error == 1 )) return 2 |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
format_files() { |
| 131 |
local -a source_files=($@) |
| 132 |
|
| 133 |
if (( ${#source_files} )) { |
| 134 |
"${formatter}" -i ${source_files} |
| 135 |
} |
| 136 |
} |
| 137 |
;; |
| 138 |
swift) |
| 139 |
local formatter=swift-format |
| 140 |
if (( ${+commands[swift-format]} )) { |
| 141 |
local swift_format_version=$(swift-format --version) |
| 142 |
|
| 143 |
if ! is-at-least 508.0.0 ${swift_format_version}; then |
| 144 |
log_error "swift-format is not version 508.0.0 or above (found ${swift_format_version})." |
| 145 |
exit 2 |
| 146 |
fi |
| 147 |
} else { |
| 148 |
log_error "No viable swift-format version found (required 508.0.0)" |
| 149 |
exit 2 |
| 150 |
} |
| 151 |
|
| 152 |
if (( ! #source_files )) source_files=(src/**/*.swift(.N)) |
| 153 |
|
| 154 |
check_files() { |
| 155 |
local -i num_failures=0 |
| 156 |
local -a source_files=($@) |
| 157 |
local file |
| 158 |
local -a format_args=() |
| 159 |
|
| 160 |
local -a command=(${formatter} ${format_args}) |
| 161 |
|
| 162 |
for file (${source_files}) { |
| 163 |
if ! "${command}" "${file}" | diff -q "${file}" - &> /dev/null; then |
| 164 |
log_error "${file} requires formatting changes." |
| 165 |
if (( fail_on_error == 2 )) return 2; |
| 166 |
num_failures=$(( num_failures + 1 )) |
| 167 |
fi |
| 168 |
} |
| 169 |
if (( num_failures && fail_on_error == 1 )) return 2 |
| 170 |
} |
| 171 |
|
| 172 |
format_files() { |
| 173 |
local -a source_files=($@) |
| 174 |
|
| 175 |
if (( ${#source_files} )) { |
| 176 |
local -a format_args=(-i) |
| 177 |
|
| 178 |
"${formatter}" ${format_args} ${source_files} |
| 179 |
} |
| 180 |
} |
| 181 |
;; |
| 182 |
*) log_error "Invalid formatter specified: ${1}. Valid options are clang-format, gersemi, and swift-format."; exit 2 ;; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
local file |
| 187 |
local -i num_failures=0 |
| 188 |
if (( check_only )) { |
| 189 |
if (( ${+functions[check_files]} )) { |
| 190 |
check_files ${source_files} |
| 191 |
} else { |
| 192 |
log_error "No format check function defined for formatter '${formatter}'" |
| 193 |
exit 2 |
| 194 |
} |
| 195 |
} else { |
| 196 |
if (( ${+functions[format_files]} )) { |
| 197 |
format_files ${source_files} |
| 198 |
} else { |
| 199 |
log_error "No format function defined for formatter '${formatter}'" |
| 200 |
exit 2 |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
run_format() { |
| 206 |
if (( ! ${+SCRIPT_HOME} )) typeset -g SCRIPT_HOME=${ZSH_ARGZERO:A:h} |
| 207 |
if (( ! ${+FORMATTER_NAME} )) typeset -g FORMATTER_NAME=${${(s:-:)ZSH_ARGZERO:t:r}[2]} |
| 208 |
local project_root=${SCRIPT_HOME:A:h} |
| 209 |
|
| 210 |
typeset -g host_os=${${(L)$(uname -s)}//darwin/macos} |
| 211 |
local -i fail_on_error=0 |
| 212 |
local -i check_only=0 |
| 213 |
local -i verbosity=1 |
| 214 |
local -r _version='2.0.0' |
| 215 |
|
| 216 |
fpath=("${SCRIPT_HOME}/.functions" ${fpath}) |
| 217 |
autoload -Uz log_info log_error log_output set_loglevel log_status log_warning |
| 218 |
|
| 219 |
local -r _usage=" |
| 220 |
Usage: %B${functrace[1]%:*}%b <option> [SOURCE_FILES] |
| 221 |
|
| 222 |
%BOptions%b: |
| 223 |
|
| 224 |
%F{yellow} Formatting options%f |
| 225 |
----------------------------------------------------------------------------- |
| 226 |
%B-c | --check%b Check only, no actual formatting takes place |
| 227 |
|
| 228 |
%F{yellow} Output options%f |
| 229 |
----------------------------------------------------------------------------- |
| 230 |
%B-v | --verbose%b Verbose (more detailed output) |
| 231 |
%B--fail-[never|error] Fail script never/on formatting change - default: %B%F{green}never%f%b |
| 232 |
%B--debug%b Debug (very detailed and added output) |
| 233 |
|
| 234 |
%F{yellow} General options%f |
| 235 |
----------------------------------------------------------------------------- |
| 236 |
%B-h | --help%b Print this usage help |
| 237 |
%B-V | --version%b Print script version information" |
| 238 |
|
| 239 |
local -a args |
| 240 |
while (( # )) { |
| 241 |
case ${1} { |
| 242 |
-c|--check) check_only=1; shift ;; |
| 243 |
-v|--verbose) (( verbosity += 1 )); shift ;; |
| 244 |
-h|--help) log_output ${_usage}; exit 0 ;; |
| 245 |
-V|--version) print -Pr "${_version}"; exit 0 ;; |
| 246 |
--debug) verbosity=3; shift ;; |
| 247 |
--fail-never) |
| 248 |
fail_on_error=0 |
| 249 |
shift |
| 250 |
;; |
| 251 |
--fail-error) |
| 252 |
fail_on_error=1 |
| 253 |
shift |
| 254 |
;; |
| 255 |
--fail-fast) |
| 256 |
fail_on_error=2 |
| 257 |
shift |
| 258 |
;; |
| 259 |
*) |
| 260 |
args+=($@) |
| 261 |
break |
| 262 |
;; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
set -- ${(@)args} |
| 267 |
set_loglevel ${verbosity} |
| 268 |
|
| 269 |
invoke_formatter ${FORMATTER_NAME} ${args} |
| 270 |
} |
| 271 |
|
| 272 |
run_format ${@} |