forked from randomidiot13/speedrunbot-plusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.sh
More file actions
executable file
·64 lines (57 loc) · 1.51 KB
/
Copy pathformat.sh
File metadata and controls
executable file
·64 lines (57 loc) · 1.51 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
# Format all the source files.
prompt_user() {
echo Error: $1 was not found. This script will break when formatting $2 files.
printf "Do you want to install $1? [y/N]: "
read -r C
if test "$C" = "y" || test "$C" = "Y"; then
return 0
fi
return 1
}
if ! command -v black >/dev/null; then
prompt_user black python && python3 -m pip install black
fi
if ! command -v isort >/dev/null; then
prompt_user isort python && python3 -m pip install isort
fi
if ! command -v clang-format >/dev/null; then
if prompt_user clang-format C; then
if command -v sudo >/dev/null 2>&1; then
SU="sudo"
elif command -v doas >/dev/null 2>&1; then
SU="doas"
else
printf "Command to gain superuser privileges (typically sudo or doas): "
read -r SU
fi
yes | $SU apt install clang-format
fi
fi
# TODO: Find a way to make this POSIX compliant.
shopt -s globstar nullglob
SCR_PATH=$(cd "$(dirname "$0")" && pwd)
for FILE in "$SCR_PATH"/**/*; do
case $FILE in
*.[ch])
clang-format -i --verbose --sort-includes -style=file "$FILE"
;;
*.py)
echo Formatting "$FILE"
isort "$FILE" &>/dev/null
python3.9 -m black -l 100 "$FILE" &>/dev/null
unexpand -t 4 --first-only "$FILE" >temp
test -x "$FILE" && EFLAG=1 || EFLAG=0
mv temp "$FILE"
test $EFLAG -eq 1 && chmod +x "$FILE"
# '...' in docstrings causes some formatting issues.
sed -i 's/^\t\(\t*\.\.\.\)/\1/' $FILE
;;
*.sh)
echo Formatting "$FILE"
unexpand -t 8 --first-only "$FILE" >temp
mv temp "$FILE"
chmod +x "$FILE"
;;
esac
done