@@ -18,37 +18,69 @@ if [[ ! -e tools/format.sh ]]; then
1818 echo " This tool must be run from the topmost OpenCensus directory." >&2
1919 exit 1
2020fi
21+
2122set -e
22- # Correct common miscapitalizations.
23+
24+ usage ()
25+ {
26+ echo " usage: format.sh [--disable-sed-check] [--disable-tools-check]"
27+ }
28+
29+ sed_check=y
30+ tools_check=y
31+
32+ while [[ " $1 " != " " ]]; do
33+ case $1 in
34+ --disable-sed-check ) sed_check=n
35+ ;;
36+ --disable-tools-check ) tools_check=n
37+ ;;
38+ -h | --help ) usage
39+ exit
40+ ;;
41+ * ) usage
42+ exit 1
43+ esac
44+ shift
45+ done
46+
2347FIND=" find . -name .git -prune -o -name .build -prune -o"
24- sed -i ' s/Open[c]ensus/OpenCensus/g' $( $FIND -type f -print)
25- sed -i ' s/Stack[D]river/Stackdriver/g' $( $FIND -type f -print)
26- # No CRLF line endings.
27- sed -i ' s/\r$//' $( $FIND -type f -print)
28- # No trailing spaces.
29- sed -i ' s/ \+$//' $( $FIND -type f -print)
30- # For easier debugging: print the version because it affects the formatting.
31- CMD=clang-format
32- $CMD -version
33- $CMD -i -style=Google \
34- $( $FIND -name ' *.cc' -print -o -name ' *.h' -print)
35- if which buildifier > /dev/null; then
36- echo " Running buildifier."
37- buildifier $( $FIND -name WORKSPACE -print -o -name BUILD -print -o \
38- -name ' *.bzl' -print)
39- else
40- echo " Can't find buildifier. It can be installed with:"
41- echo " go get github.com/bazelbuild/buildtools/buildifier"
48+
49+ if [[ " $sed_check " == " y" ]]; then
50+ # Correct common miscapitalizations.
51+ sed -i ' s/Open[c]ensus/OpenCensus/g' $( $FIND -type f -print)
52+ sed -i ' s/Stack[D]river/Stackdriver/g' $( $FIND -type f -print)
53+ # No CRLF line endings.
54+ sed -i ' s/\r$//' $( $FIND -type f -print)
55+ # No trailing spaces.
56+ sed -i ' s/ \+$//' $( $FIND -type f -print)
4257fi
43- if which cmake-format > /dev/null; then
44- echo " Running cmake-format $( cmake-format --version 2>&1 ) ."
45- cmake-format -i $( $FIND -name FetchContent.cmake -prune -o \
46- -name ' *CMakeLists.txt' -print -o \
47- -name ' *.cmake' -print)
48- else
49- echo " Can't find cmake-format. It can be installed with:"
50- echo " pip install --user cmake_format"
58+
59+ if [[ " $tools_check " == " y" ]]; then
60+ # For easier debugging: print the version because it affects the formatting.
61+ CMD=clang-format
62+ $CMD -version
63+ $CMD -i -style=Google \
64+ $( $FIND -name ' *.cc' -print -o -name ' *.h' -print)
65+ if which buildifier > /dev/null; then
66+ echo " Running buildifier."
67+ buildifier $( $FIND -name WORKSPACE -print -o -name BUILD -print -o \
68+ -name ' *.bzl' -print)
69+ else
70+ echo " Can't find buildifier. It can be installed with:"
71+ echo " go get github.com/bazelbuild/buildtools/buildifier"
72+ fi
73+ if which cmake-format > /dev/null; then
74+ echo " Running cmake-format $( cmake-format --version 2>&1 ) ."
75+ cmake-format -i $( $FIND -name FetchContent.cmake -prune -o \
76+ -name ' *CMakeLists.txt' -print -o \
77+ -name ' *.cmake' -print)
78+ else
79+ echo " Can't find cmake-format. It can be installed with:"
80+ echo " pip install --user cmake_format"
81+ fi
5182fi
83+
5284CHANGED=" $( git ls-files --modified) "
5385if [[ ! -z " $CHANGED " ]]; then
5486 echo " The following files have changes:"
@@ -57,3 +89,8 @@ if [[ ! -z "$CHANGED" ]]; then
5789else
5890 echo " No changes."
5991fi
92+
93+ if [[ " $sed_check " != " y" || " $tools_check " != " y" ]]; then
94+ echo " All checks must run to succeed."
95+ exit 1
96+ fi
0 commit comments