-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathformat
More file actions
executable file
·64 lines (55 loc) · 2.34 KB
/
format
File metadata and controls
executable file
·64 lines (55 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
set -euo pipefail
function main() {
local skip_lines_backref="" prev_file_contents="" next_file_contents=""
if ! command -v shfmt &> /dev/null
then
echo "!!! Install shfmt first: brew install shfmt !!!"
exit 1
fi
for file in "$@"
do
# First, auto-format with shfmt (https://github.com/mvdan/sh):
shfmt -w -kp -ci -sr "${file}"
# Move "then" and "do" keywords onto separate lines for readability:
perl -0777 -pe 's/; then\n(\s+)(\s)(\S)/\n\1then\n\1\2\3/g;' -i "${file}"
perl -0777 -pe 's/; do\n(\s+)(\s)(\S)/\n\1do\n\1\2\3/g;' -i "${file}"
# Add surrounding spaces to $(...) and ((...)) expressions for readability:
skip_lines_backref='(?<!(?:=~|s\/|# |echo).{1,250})'
next_file_contents="$( cat "${file}" )"
while [[ "${prev_file_contents}" != "${next_file_contents}" ]]
do
prev_file_contents="$( cat "${file}" )"
perl -0777 -pe \
"s/${skip_lines_backref}"'(\$|\()\(([^\(\)\s])/\1\( \2/g' \
-i "${file}"
# ^ Add space after $( and (( when they aren't followed by space already,
# skipping lines which contain regex match, sed pattern, comments, or echos.
perl -0777 -pe \
"s/${skip_lines_backref}"'(?<=(?:\$\(|\(\().{1,250})([^\(\)\s])\)/\1 \)/g' \
-i "${file}"
# ^ Add space before ) and )) when they aren't preceeded by space already,
# skipping lines which contain regex match, sed pattern, comments, or echos.
# Only do this when $( or (( occurs before these char sequences earlier in line.
perl -0777 -pe "s/${skip_lines_backref}"'\) +/\) /g' -i "${file}"
# ^ If the above spacing changes result in multiple spaces after ), reduce to one.
perl -0777 -pe "s/${skip_lines_backref}"'\(\(\(/\(\( \(/g' -i "${file}"
perl -0777 -pe "s/${skip_lines_backref}"'\)\)\)/\) \)\)/g' -i "${file}"
# ^ Refine spacing of nested (( (...) )) and (( ((...)) )) expressions.
perl -0777 -pe "s/${skip_lines_backref}"' \( / \(/g' -i "${file}"
perl -0777 -pe \
"s/${skip_lines_backref}"'(?<!(?:\$\()[^\(\)]{1,250}) \) /\) /g' \
-i "${file}"
# ^ Remove extra spacing inside (...) expressions.
next_file_contents="$( cat "${file}" )"
done
# Remove trailing space from all lines in all files:
perl -0777 -pe 's/\s+$//g' -i "${file}"
# Ensure every file has a trailing newline:
if [[ -n "$( tail -c1 "${file}" )" ]]
then
echo >> "${file}"
fi
done
}
main "$@"