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

277 lines, 164 significant
1#!/usr/bin/env bash
2#
3# Junk drawer for refactoring. Also see test/lint.sh
4#
5# Usage:
6# devtools/refactor.sh <function name>
7
8: ${LIB_OSH=stdlib/osh}
9source $LIB_OSH/bash-strict.sh
10source $LIB_OSH/task-five.sh
11
12change-kind() {
13 local kind=$1
14 local kind2=${2:-$kind}
15
16 # First make it all lower case
17 sed -r -i "s/${kind}_([A-Z]+)/Id.${kind2}_\\L\\1/g" */*.py
18
19 # Now make the first one upper case
20 sed -r -i "s/${kind}_([a-zA-Z]+)/${kind2}_\\u\\1/g" */*.py
21}
22
23k2() {
24 local kind=$1
25 local lower=${kind,,}
26
27 local title=${lower^}
28 local replace=${2:-$title}
29
30 sed -r -i "s/Id.${kind}_/Id.${replace}_/g" */*.py
31 sed -r -i "s/TokenKind.${kind}/TokenKind.${replace}/g" */*.py
32}
33
34# Execute a bunch of find/replace pairs in a text file.
35replace() {
36 local file=$1
37 local include_asdl=${2:-}
38
39 # NOTE: Escaping here is messed up. sed doesn't have --name like awk?
40 # To match literal parentheses I had to double-escape like this
41 # (shell-escape, then sed-escape).
42 # MakeMatcher\\(\\) MATCHER
43
44 local -a files=( */*.py )
45 if test -n "$include_asdl"; then
46 files+=( */*.asdl )
47 fi
48
49 while read pat replace; do
50 sed -r -i "s/${pat}/${replace}/g" "${files[@]}"
51
52 # word-anchored version
53 #sed -r -i "s/\b${pat}\b/${replace}/g" "${files[@]}"
54 done < $file
55}
56
57replace2() {
58 #sed -r -i "s/^from osh import parse_lib/from frontend import parse_lib/g" */*.py
59 #sed -r -i "s/^from core import libstr/from osh import string_ops/g" */*.py
60 #sed -r -i "s/^from osh import word$/from osh import word_/g" */*.py
61 #sed -r -i 's/from _devbuild.gen.syntax_asdl import word as osh_word/from _devbuild.gen.syntax_asdl import word/g' */*.py
62 #sed -r -i 's/osh_word/word/g' */*.py
63
64 if test -n ''; then
65 sed -r -i 's/bool_expr.BoolUnary/bool_expr.Unary/g' */*.py
66 sed -r -i 's/bool_expr.BoolBinary/bool_expr.Binary/g' */*.py
67 sed -r -i 's/bool_expr_e.BoolUnary/bool_expr_e.Unary/g' */*.py
68 sed -r -i 's/bool_expr_e.BoolBinary/bool_expr_e.Binary/g' */*.py
69 sed -r -i 's/bool_expr__BoolUnary/bool_expr__Unary/g' */*.py
70 sed -r -i 's/bool_expr__BoolBinary/bool_expr__Binary/g' */*.py
71 fi
72
73 sed -r -i 's/command.SimpleCommand/command.Simple/g' */*.py
74 sed -r -i 's/command_e.SimpleCommand/command_e.Simple/g' */*.py
75 sed -r -i 's/command__SimpleCommand/command__Simple/g' */*.py
76}
77
78trailing-ws() {
79 sed -r -i 's/[ ]+$//g' "$@"
80}
81
82#
83# OLD STUFF
84#
85
86# Hm all of the solutions involve grep --perl or perl itself?
87# https://stackoverflow.com/questions/3001177/how-do-i-grep-for-all-non-ascii-characters-in-unix
88
89# Found a latin-1 character in Python-2.7.13/Lib/heapq.py. Had to add LC_ALL=C.
90grep-unicode() {
91 LC_ALL=C grep --color='auto' --perl -n '[^\x00-\x7F]' "$@"
92}
93
94find-old-asdl() {
95 egrep 'import.*\bruntime\b' */*.py || true
96 echo ---
97
98 # Only tests left
99 egrep 'import.*\bast\b' */*.py || true
100}
101
102# This should be cleaned up
103grep-span-funcs() {
104 grep MostSpan {osh,core,frontend}/*.py
105}
106
107cmd-val() {
108 local file=$1
109 sed -i 's/arg_vec.strs/cmd_val.argv/g' $file
110 sed -i 's/arg_vec.spids/cmd_val.arg_spids/g' $file
111 sed -i 's/arg_vector/cmd_value__Argv/g' $file
112 sed -i 's/arg_vec/cmd_val/g' $file
113}
114
115id-kind() {
116 sed --regexp-extended -i 's/import id_kind$/import id_kind_def/' */*.py
117 sed --regexp-extended -i 's/id_kind[.]/id_kind_def./g' */*.py
118}
119
120options() {
121 #sed -i 's/simple_echo/simple_echo/g' */*.{py,md,sh}
122
123 #sed -i 's/simple_eval_builtin/simple_eval_builtin/g' */*.{py,md,sh}
124 #sed -i 's/simple_trap/simple_trap/g' */*.{py,md,sh}
125
126 sed -i 's/parse_backslash/parse_backslash/g' */*.{py,md,sh}
127 sed -i 's/parse_backticks/parse_backticks/g' */*.{py,md,sh}
128 sed -i 's/parse_dollar/parse_dollar/g' */*.{py,md,sh}
129}
130
131rename() {
132 cat cpp/cc.txt | while read name; do
133 local base=$(basename $name .cc)
134 local new=leaky_${base%_leaky}.cc
135 echo $name $new
136
137 #sed -i "s/$name/$new/g" */*.{py,sh} || true
138 git mv cpp/$name cpp/$new
139 done
140 return
141
142 cat cpp/h.txt | while read name; do
143 local base=$(basename $name .h)
144 local new=leaky_${base%_leaky}.h
145 echo $name $new
146
147 #sed -i "s/$name/$new/g" */*.{sh,py,cc,h}
148 git mv cpp/$name cpp/$new
149 done
150 return
151}
152
153revert() {
154 cat cpp/cc.txt | while read name; do
155 local new=$(basename $name .cc)_leaky.cc
156 echo $name $new
157
158 mv cpp/$new cpp/$name
159 done
160}
161
162#
163# Things we want to get rid of
164#
165
166show-usages() {
167 local out=$1
168 shift
169 "$@" | grep -v _test.py | tee $out
170 echo
171 wc -l $out
172}
173
174# 2024-02: 36 usages. Maybe use mylib.ToMachineInt() or mylib.ToInt32(). exit
175# status is a machine int?
176# Sometimes the lexer will validate, as with converting 2>&1
177
178int-convert() {
179 show-usages _tmp/int-convert \
180 egrep -n '\bint\(\b' $(metrics/source-code.sh osh-files)
181}
182
183# TokenVal() is generally bad in evaluators. However most of these are in
184# error paths, which is OK.
185#
186# 2024-02: 11 instances
187TokenVal-eval() {
188 show-usages _tmp/TokenVal-eval \
189 grep -n -w TokenVal */*_eval.py
190}
191
192TokenVal-all() {
193 show-usages _tmp/TokenVal-all \
194 grep -n -w TokenVal */*.py
195}
196
197# 2024-02: 71 left, mostly in ysh_ify which is the only thing that uses it
198spid-all() {
199 show-usages _tmp/spid-all \
200 egrep -n 'span_id|spid' */*.py
201}
202
203# 2024-04: 4 left in ysh_ify
204spid-sig() {
205 show-usages _tmp/spid-sig \
206 egrep -n 'def.*(span_id|spid)' */*.py
207}
208
209# 2024-04: 12 usages, mostly ysh_ify
210no-spid() {
211 show-usages _tmp/no-spid \
212 egrep -n 'runtime.NO_SPID' */*.py
213}
214
215# 69 instances
216loc-word() {
217 # should NOT wrap CompoundWord
218 show-usages _tmp/loc-word \
219 fgrep -n 'loc.Word(' */*.py
220}
221
222# 2023-08: 155
223loc-missing() {
224 show-usages _tmp/loc-m \
225 egrep -n 'loc.Missing' */*.py
226}
227
228mylib-python() {
229 show-usages _tmp/py \
230 egrep -n 'mylib.PYTHON' */*.py
231}
232
233asdl-create() {
234 fgrep -n 'CreateNull(alloc' */*.py */*/*.py \
235 | egrep -v '_devbuild|_test.py' | tee _tmp/asdl
236}
237
238readline() {
239 metrics/source-code.sh oils-files | xargs fgrep -n 'readline('
240}
241
242#
243# To improve code formatting
244#
245
246long-sigs() {
247 # 32 of these
248 egrep --no-filename '^[ ]*# type' */*.py \
249 | awk 'length($0) >= 80 { print length($0) $0 }' \
250 | sort -n
251}
252
253long-sigs-where() {
254 # jump to the file
255 egrep -n '^[ ]*# type' */*.py \
256 | awk 'length($0) >= 110 { print }' | tee _tmp/long
257}
258
259#
260# Refactor tests
261#
262
263print-names() {
264 egrep -o '[a-zA-Z_-]+'
265}
266
267make-sed() {
268 awk '{ print "s/" $0 "/unquoted-" $0 "/g;" }'
269}
270
271test-files() {
272 cat _tmp/r | print-names | make-sed | tee _tmp/sedr
273
274 sed -i -f _tmp/sedr test/runtime-errors.sh
275}
276
277task-five "$@"