OILS / devtools / format.sh View on Github | oils.pub

165 lines, 89 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,asdl/examples,benchmarks,build,builtin,core,data_lang,display,doctools,frontend,mycpp,mycpp/examples,osh,pea,spec/*,test,win32,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 test/*.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/*.{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 build/detect-*.c
113
114 # Could add pyext, but they have sort of a Python style
115 # pyext/fanos.c
116)
117
118cpp-files() {
119 shopt -s nullglob
120 for file in "${CPP_FILES[@]}"; do
121
122 echo $file
123 done
124}
125
126all-cpp() {
127 # see build/common.sh
128 if test -n "$CLANG_IS_MISSING"; then
129 log ''
130 log " *** $0: Did not find $CLANG_DIR_1"
131 log " *** Run deps/from-binary.sh to get it"
132 log ''
133 return 1
134 fi
135
136 cpp-files | egrep -v 'greatest.h' | xargs -- $0 clang-format -i
137 git diff
138}
139
140test-asdl-format() {
141 ### Test how clang-format would like our generated code
142
143 local file=${1:-_gen/asdl/hnode.asdl.h}
144
145 local tmp=_tmp/hnode
146 clang-format $file > $tmp
147 diff -u $file $tmp
148}
149
150install-npm() {
151 # ridiculous number of deps
152 sudo apt-get install npm
153}
154
155install-sql-cst-plugin() {
156 # from https://github.com/nene/prettier-plugin-sql-cst/
157 npm install --save-dev prettier prettier-plugin-sql-cst
158}
159
160all-sql() {
161 #npx prettier --plugin prettier-plugin-sql-cst --parser sqlite --write */*.sql
162 npx prettier --write */*/*.sql
163}
164
165task-five "$@"