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