OILS / devtools / format.sh View on Github | oilshell.org

148 lines, 78 significant
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}
9source $LIB_OSH/bash-strict.sh
10source $LIB_OSH/task-five.sh
11
12REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
13source build/dev-shell.sh # python3 in $PATH
14
15# Hack to prevent interference. TODO: Make a separate wedge for yapf.
16unset PYTHONPATH
17
18source build/common.sh # $CLANG_DIR
19
20#
21# Python
22#
23
24readonly YAPF_VENV='_tmp/yapf-venv'
25
26install-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
44yapf-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.
51yapf-files() {
52 . $YAPF_VENV/bin/activate
53 python3 -m yapf -i "$@"
54}
55
56yapf-known() {
57 ### yapf some files that have been normalized
58
59 time yapf-files \
60 {asdl,benchmarks,builtin,core,data_lang,doctools,frontend,lazylex,mycpp,mycpp/examples,osh,spec/*,yaks,ysh}/*.py \
61 */NINJA_subgraph.py
62}
63
64yapf-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
77install-docformatter() {
78 python3 -m pip install docformatter
79}
80
81docstrings() {
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 lazylex/*.py
90}
91
92#
93# C++
94#
95
96clang-format() {
97 # See //.clang-format for the style config.
98 $CLANG_DIR/bin/clang-format --style=file "$@"
99}
100
101readonly -a CPP_FILES=(
102 {asdl,core}/*.cc
103 benchmarks/*.c
104 cpp/*.{c,cc,h}
105 data_lang/*.{c,cc,h}
106 mycpp/*.{cc,h}
107 mycpp/demo/*.{cc,h}
108 demo/*.c
109 doctools/*.{h,cc}
110 yaks/*.h
111
112 # Could add pyext, but they have sort of a Python style
113 # pyext/fanos.c
114)
115
116cpp-files() {
117 shopt -s nullglob
118 for file in "${CPP_FILES[@]}"; do
119
120 echo $file
121 done
122}
123
124all-cpp() {
125 # see build/common.sh
126 if test -n "$CLANG_IS_MISSING"; then
127 log ''
128 log " *** $0: Did not find $CLANG_DIR_1"
129 log " *** Run deps/from-binary.sh to get it"
130 log ''
131 return 1
132 fi
133
134 cpp-files | egrep -v 'greatest.h' | xargs -- $0 clang-format -i
135 git diff
136}
137
138test-asdl-format() {
139 ### Test how clang-format would like our generated code
140
141 local file=${1:-_gen/asdl/hnode.asdl.h}
142
143 local tmp=_tmp/hnode
144 clang-format $file > $tmp
145 diff -u $file $tmp
146}
147
148task-five "$@"