1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Run yapf formatter; it's installed in ~/wedge/ by build/deps.sh
|
4 | #
|
5 | # Usage:
|
6 | # test/format.sh <function name>
|
7 |
|
8 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
9 |
|
10 | source build/dev-shell.sh # python3 in $PATH
|
11 | source devtools/run-task.sh
|
12 |
|
13 | # Hack to prevent interference. TODO: Make a separate wedge for yapf.
|
14 | unset PYTHONPATH
|
15 |
|
16 | . build/common.sh # $CLANG_DIR
|
17 |
|
18 | set -o nounset
|
19 | set -o pipefail
|
20 | set -o errexit
|
21 | shopt -s strict:all 2>/dev/null || true # dogfood for OSH
|
22 |
|
23 | #
|
24 | # Python
|
25 | #
|
26 |
|
27 | readonly YAPF_VENV='_tmp/yapf-venv'
|
28 |
|
29 | install-yapf() {
|
30 | local venv=$YAPF_VENV
|
31 |
|
32 | rm -r -f -v $venv
|
33 |
|
34 | python3 -m venv $venv
|
35 |
|
36 | . $venv/bin/activate
|
37 |
|
38 | # 0.40.1 is the 2023-06-20 release
|
39 | #
|
40 | # Pin the version so formatting is stable!
|
41 |
|
42 | python3 -m pip install 'yapf == 0.40.1'
|
43 |
|
44 | yapf-version
|
45 | }
|
46 |
|
47 | yapf-version() {
|
48 | . $YAPF_VENV/bin/activate
|
49 | python3 -m yapf --version
|
50 | }
|
51 |
|
52 | # For now, run yapf on specific files. TODO: could query git for the files
|
53 | # that are are different from master branch, and run it on those.
|
54 | yapf-files() {
|
55 | . $YAPF_VENV/bin/activate
|
56 | python3 -m yapf -i "$@"
|
57 | }
|
58 |
|
59 | yapf-known() {
|
60 | ### yapf some files that have been normalized
|
61 |
|
62 | time yapf-files \
|
63 | {asdl,benchmarks,builtin,core,data_lang,frontend,mycpp,mycpp/examples,osh,spec/*,yaks,ysh}/*.py \
|
64 | */NINJA_subgraph.py
|
65 | }
|
66 |
|
67 | yapf-changed() {
|
68 | branch="${1:-master}"
|
69 |
|
70 | #git diff --name-only .."$branch" '*.py'
|
71 |
|
72 | git diff --name-only .."$branch" '*.py' \
|
73 | | xargs --no-run-if-empty -- $0 yapf-files
|
74 | }
|
75 |
|
76 | #
|
77 | # C++
|
78 | #
|
79 |
|
80 | clang-format() {
|
81 | # See //.clang-format for the style config.
|
82 | $CLANG_DIR/bin/clang-format --style=file "$@"
|
83 | }
|
84 |
|
85 | readonly -a CPP_FILES=(
|
86 | {asdl,core}/*.cc
|
87 | benchmarks/*.c
|
88 | cpp/*.{c,cc,h}
|
89 | data_lang/*.{c,cc,h}
|
90 | mycpp/*.{cc,h}
|
91 | mycpp/demo/*.{cc,h}
|
92 | demo/*.c
|
93 | doctools/*.{h,cc}
|
94 | yaks/*.h
|
95 |
|
96 | # Could add pyext, but they have sort of a Python style
|
97 | # pyext/fanos.c
|
98 | )
|
99 |
|
100 | cpp-files() {
|
101 | shopt -s nullglob
|
102 | for file in "${CPP_FILES[@]}"; do
|
103 |
|
104 | echo $file
|
105 | done
|
106 | }
|
107 |
|
108 | all-cpp() {
|
109 | # see build/common.sh
|
110 | if test -n "$CLANG_IS_MISSING"; then
|
111 | log ''
|
112 | log " *** $0: Did not find $CLANG_DIR_1"
|
113 | log " *** Run deps/from-binary.sh to get it"
|
114 | log ''
|
115 | return 1
|
116 | fi
|
117 |
|
118 | cpp-files | egrep -v 'greatest.h' | xargs -- $0 clang-format -i
|
119 | git diff
|
120 | }
|
121 |
|
122 | test-asdl-format() {
|
123 | ### Test how clang-format would like our generated code
|
124 |
|
125 | local file=${1:-_gen/asdl/hnode.asdl.h}
|
126 |
|
127 | local tmp=_tmp/hnode
|
128 | clang-format $file > $tmp
|
129 | diff -u $file $tmp
|
130 | }
|
131 |
|
132 | run-task "$@"
|