@@ -14,10 +14,16 @@ cd $BUILD_WORKSPACE_DIRECTORY
1414
1515function on_exit {
1616 code=$?
17- if [[ $code != 0 ]]; then
18- echo >&2 " FAILED: A formatter tool exited with code $code "
19- echo >&2 " Try running 'bazel run {{fix_target}}' to fix this."
20- fi
17+ case " $code " in
18+ # return code 143 is the result of SIGTERM, which isn't failure, so suppress failure suggestion
19+ 0|143)
20+ exit $code ;
21+ ;;
22+ * )
23+ echo >&2 " FAILED: A formatter tool exited with code $code "
24+ echo >&2 " Try running 'bazel run {{fix_target}}' to fix this."
25+ ;;
26+ esac
2127}
2228
2329trap on_exit EXIT
@@ -61,7 +67,7 @@ function ls-files {
6167 exit 1
6268 ;;
6369 esac
64-
70+
6571 if [ " $# " -eq 0 ]; then
6672 # When the formatter is run with no arguments, we run over "all files in the repo".
6773 # However, we want to ignore anything that is in .gitignore, is marked for delete, etc.
@@ -130,160 +136,86 @@ case "$mode" in
130136 * ) echo >&2 " unknown mode $mode " ;;
131137esac
132138
133- # Run each supplied formatter over the files it owns
134- # TODO: run them concurrently, not serial
135-
136- files=$( ls-files Starlark $@ )
137- bin=$( rlocation {{buildifier}})
138- if [ -n " $files " ] && [ -n " $bin " ]; then
139- echo " Formatting Starlark with Buildifier..."
140- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin -mode=" $mode "
141- fi
142-
143- files=$( ls-files Markdown $@ )
144- bin=$( rlocation {{prettier-md}})
145- if [ -n " $files " ] && [ -n " $bin " ]; then
146- echo " Formatting Markdown with Prettier..."
147- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $prettiermode
148- fi
149-
150- files=$( ls-files JSON $@ )
151- bin=$( rlocation {{prettier}})
152- if [ -n " $files " ] && [ -n " $bin " ]; then
153- echo " Formatting JSON with Prettier..."
154- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $prettiermode
155- fi
156-
157- files=$( ls-files JavaScript $@ )
158- bin=$( rlocation {{prettier}})
159- if [ -n " $files " ] && [ -n " $bin " ]; then
160- echo " Formatting JavaScript with Prettier..."
161- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $prettiermode
162- fi
163-
164- files=$( ls-files CSS $@ )
165- bin=$( rlocation {{prettier}})
166- if [ -n " $files " ] && [ -n " $bin " ]; then
167- echo " Formatting CSS with Prettier..."
168- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $prettiermode
169- fi
170-
171- files=$( ls-files HTML $@ )
172- bin=$( rlocation {{prettier}})
173- if [ -n " $files " ] && [ -n " $bin " ]; then
174- echo " Formatting HTML with Prettier..."
175- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $prettiermode
176- fi
177-
178- files=$( ls-files TypeScript $@ )
179- bin=$( rlocation {{prettier}})
180- if [ -n " $files " ] && [ -n " $bin " ]; then
181- echo " Formatting TypeScript with Prettier..."
182- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $prettiermode
183- fi
184-
185- files=$( ls-files TSX $@ )
186- bin=$( rlocation {{prettier}})
187- if [ -n " $files " ] && [ -n " $bin " ]; then
188- echo " Formatting TSX with Prettier..."
189- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $prettiermode
190- fi
191-
192- files=$( ls-files SQL $@ )
193- bin=$( rlocation {{prettier-sql}})
194- if [ -n " $files " ] && [ -n " $bin " ]; then
195- echo " Formatting SQL with Prettier..."
196- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $prettiermode
197- fi
198-
199- files=$( ls-files Python $@ )
200- bin=$( rlocation {{ruff}})
201- if [ -n " $files " ] && [ -n " $bin " ]; then
202- echo " Formatting Python with Ruff..."
203- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $ruffmode
204- fi
205-
206- files=$( ls-files Terraform $@ )
207- bin=$( rlocation {{terraform-fmt}})
208- if [ -n " $files " ] && [ -n " $bin " ]; then
209- echo " Formatting Terraform files with terraform fmt..."
210- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin fmt $tfmode
211- fi
212-
213- files=$( ls-files Jsonnet $@ )
214- bin=$( rlocation {{jsonnetfmt}})
215- if [ -n " $files " ] && [ -n " $bin " ]; then
216- echo " Formatting Jsonnet with jsonnetfmt..."
217- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $jsonnetmode
218- fi
139+ function time-run {
140+ local files=" $1 " && shift
141+ local bin=" $1 " && shift
142+ local lang=" $1 " && shift
143+ local silent=" $1 " && shift
144+ local tuser
145+ local tsys
219146
220- files=$( ls-files Java $@ )
221- bin=$( rlocation {{java-format}})
222- if [ -n " $files " ] && [ -n " $bin " ]; then
223- echo " Formatting Java with java-format..."
224- # Setting JAVA_RUNFILES to work around https://github.com/bazelbuild/bazel/issues/12348
225- echo " $files " | tr \\ n \\ 0 | JAVA_RUNFILES=" ${RUNFILES_MANIFEST_FILE% _manifest} " xargs -0 $bin $javamode
226- fi
147+ ( if [ $silent != 0 ] ; then 2> /dev/null ; fi ; echo " $files " | tr \\ n \\ 0 | xargs -0 " $bin " " $@ " >&2 ; times ) | ( read _ _ ; read tuser tsys; echo " Formatted ${lang} in ${tuser} " )
227148
228- files=$( ls-files Kotlin $@ )
229- bin=$( rlocation {{ktfmt}})
230- if [ -n " $files " ] && [ -n " $bin " ]; then
231- echo " Formatting Kotlin with ktfmt..."
232- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $ktmode
233- fi
234-
235- files=$( ls-files Scala $@ )
236- bin=$( rlocation {{scalafmt}})
237- if [ -n " $files " ] && [ -n " $bin " ]; then
238- echo " Formatting Scala with scalafmt..."
239- # Setting JAVA_RUNFILES to work around https://github.com/bazelbuild/bazel/issues/12348
240- echo " $files " | tr \\ n \\ 0 | JAVA_RUNFILES=" ${RUNFILES_MANIFEST_FILE% _manifest} " xargs -0 $bin $scalamode
241- fi
149+ }
242150
243- files=$( ls-files Go $@ )
244- bin=$( rlocation {{gofmt}})
245- if [ -n " $files " ] && [ -n " $bin " ]; then
246- echo " Formatting Go with gofmt..."
247- # gofmt doesn't produce non-zero exit code so we must check for non-empty output
248- # https://github.com/golang/go/issues/24230
249- if [ " $mode " == " check" ]; then
250- NEED_FMT=$( echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $gofmtmode )
251- if [ -n " $NEED_FMT " ]; then
252- echo " Go files not formatted:"
253- echo " $NEED_FMT "
254- exit 1
255- fi
256- else
257- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $gofmtmode
151+ function run-format {
152+ local lang=" $1 " && shift
153+ local fmtname=" $1 " && shift
154+ local bin=" $1 " && shift
155+ local args=" $1 " && shift
156+ local tuser
157+ local tsys
158+
159+ local files=$( ls-files " $lang " $@ )
160+ if [ -n " $files " ] && [ -n " $bin " ]; then
161+ echo " Formatting ${lang} with ${fmtname} ..."
162+ case " $lang " in
163+ ' Protocol Buffer' )
164+ ( for file in $files ; do
165+ " $bin " $args $file >&2
166+ done ; times ) | ( read _ _; read tuser tsys; echo " Formatted ${lang} in ${tuser} " )
167+ ;;
168+ Go)
169+ # gofmt doesn't produce non-zero exit code so we must check for non-empty output
170+ # https://github.com/golang/go/issues/24230
171+ if [ " $mode " == " check" ]; then
172+ GOFMT_OUT=$( mktemp)
173+ (echo " $files " | tr \\ n \\ 0 | xargs -0 " $bin " $args > " $GOFMT_OUT " ; times ) | ( read _ _; read tuser tsys; echo " Formatted ${lang} in ${tuser} " )
174+ NEED_FMT=" $( cat $GOFMT_OUT ) "
175+ rm $GOFMT_OUT
176+ if [ -n " $NEED_FMT " ]; then
177+ echo " Go files not formatted:"
178+ echo " $NEED_FMT "
179+ exit 1
180+ fi
181+ else
182+ time-run " $files " " $bin " " $lang " 0 $args
183+ fi
184+ ;;
185+ Java|Scala)
186+ # Setting JAVA_RUNFILES to work around https://github.com/bazelbuild/bazel/issues/12348
187+ ( export JAVA_RUNFILES=" ${RUNFILES_MANIFEST_FILE% _manifest} " ; time-run " $files " " $bin " " $lang " 0 $args )
188+ ;;
189+ Swift)
190+ # for any formatter that must be silenced
191+ time-run " $files " " $bin " " $lang " 1 $args
192+ ;;
193+ * )
194+ time-run " $files " " $bin " " $lang " 0 $args
195+ ;;
196+ esac
258197 fi
259- fi
260-
261- files=$( ls-files C++ $@ )
262- bin=$( rlocation {{clang-format}})
263- if [ -n " $files " ] && [ -n " $bin " ]; then
264- echo " Formatting C/C++ with clang-format..."
265- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $clangformatmode
266- fi
267-
268- files=$( ls-files Shell $@ )
269- bin=$( rlocation {{shfmt}})
270- if [ -n " $files " ] && [ -n " $bin " ]; then
271- echo " Formatting Shell with shfmt..."
272- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $shfmtmode
273- fi
198+ }
274199
275- files=$( ls-files Swift $@ )
276- bin=$( rlocation {{swiftformat}})
277- if [ -n " $files " ] && [ -n " $bin " ]; then
278- # swiftformat itself prints Running SwiftFormat...
279- echo " $files " | tr \\ n \\ 0 | xargs -0 $bin $swiftmode
280- fi
200+ # Run each supplied formatter over the files it owns
281201
282- files=$( ls-files ' Protocol Buffer' $@ )
283- bin=$( rlocation {{buf}})
284- if [ -n " $files " ] && [ -n " $bin " ]; then
285- echo " Formatting Protobuf with buf..."
286- for file in $files ; do
287- $bin $bufmode $file
288- done
289- fi
202+ run-format Starlark Buildifier " $( rlocation {{buildifier}}) " " -mode=$mode " $@
203+ run-format Markdown Prettier " $( rlocation {{prettier-md}}) " " $prettiermode " $@
204+ run-format JSON Prettier " $( rlocation {{prettier}}) " " $prettiermode " $@
205+ run-format JavaScript Prettier " $( rlocation {{prettier}}) " " $prettiermode " $@
206+ run-format CSS Prettier " $( rlocation {{prettier}}) " " $prettiermode " $@
207+ run-format HTML Prettier " $( rlocation {{prettier}}) " " $prettiermode " $@
208+ run-format TypeScript Prettier " $( rlocation {{prettier}}) " " $prettiermode " $@
209+ run-format TSX Prettier " $( rlocation {{prettier}}) " " $prettiermode " $@
210+ run-format SQL Prettier " $( rlocation {{prettier-sql}}) " " $prettiermode " $@
211+ run-format Python Ruff " $( rlocation {{ruff}}) " " $ruffmode " $@
212+ run-format Terraform " terraform fmt" " $( rlocation {{terraform-fmt}}) " " fmt $tfmode " $@
213+ run-format Jsonnet jsonnetfmt " $( rlocation {{jsonnetfmt}}) " " $jsonnetmode " $@
214+ run-format Java java-format " $( rlocation {{java-format}}) " " $javamode " $@
215+ run-format Kotlin ktfmt " $( rlocation {{ktfmt}}) " " $ktmode " $@
216+ run-format Scala scalafmt " $( rlocation {{scalafmt}}) " " $scalamode " $@
217+ run-format Go gofmt " $( rlocation {{gofmt}}) " " $gofmtmode " $@
218+ run-format C++ clang-format " $( rlocation {{clang-format}}) " " $clangformatmode " $@
219+ run-format Shell shfmt " $( rlocation {{shfmt}}) " " $shfmtmode " $@
220+ run-format Swift swiftfmt " $( rlocation {{swiftformat}}) " " $swiftmode " $@
221+ run-format ' Protocol Buffer' buf " $( rlocation {{buf}}) " " $bufmode " $@
0 commit comments