Debian Codegraph @

bin/ci

raw
1 #!/usr/bin/env bash
2
3
4 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
5 THIS_PROG="$0"
6
7 set -o pipefail
8
9 # load utility functions
10 . "${DIR}/_utils.sh"
11
12 # -----------------------------------------------------------------
13 # Main Functions --------------------------------------------------
14 # -----------------------------------------------------------------
15
16 SUBCOMMAND="all"
17 AUTOFIX=false
18 COLOR=false
19 RUN_PREFIX="run_boxed"
20 FORWARDED_OPTS=()
21
22 CODE_DIRS=(
23 lookatme
24 tests
25 )
26
27 function run_flake8 {
28 if [ "$AUTOFIX" == "true" ] ; then
29 log "No autofix for flake8"
30 else
31 local exit_code=0
32 local color_opt=""
33 [ "$COLOR" == true ] && color_opt="--color always"
34
35 $RUN_PREFIX flake8 "${CODE_DIRS[@]}" $color_opt \
36 --count \
37 --select=E9,F63,F7,F82 \
38 --show-source \
39 --statistics
40 [ $? -ne 0 ] && exit_code=1
41
42 $RUN_PREFIX flake8 "${CODE_DIRS[@]}" $color_opt \
43 --count \
44 --exit-zero \
45 --max-complexity=10 \
46 --max-line-length=127 \
47 --statistics
48 [ $? -ne 0 ] && exit_code=1
49
50 return $exit_code
51 fi
52 }
53
54 function run_autopep8 {
55 if [ "$AUTOFIX" == "true" ] ; then
56 $RUN_PREFIX autopep8 -r --in-place "${CODE_DIRS[@]}"
57 else
58 log "autopep8 is only used with --autofix"
59 fi
60 }
61
62 function run_pytest {
63 if [ "$AUTOFIX" == "true" ] ; then
64 log "🤣 LOL, no autofix for pytest!"
65 else
66 local color_opt=""
67 [ "$COLOR" == true ] && color_opt="--color=yes"
68
69 $RUN_PREFIX pytest $color_opt "$@"
70 fi
71 }
72
73 function run_isort {
74 if [ "$AUTOFIX" == "true" ] ; then
75 $RUN_PREFIX isort "${CODE_DIRS[@]}"
76 else
77 $RUN_PREFIX isort --check --diff "${CODE_DIRS[@]}"
78 fi
79 }
80
81 function run_pyright {
82 if [ "$AUTOFIX" == "true" ] ; then
83 log "🤣 LOL, no autofix for pyright!"
84 else
85 if [ "$COLOR" == true ] ; then
86 pyright "${CODE_DIRS[@]}"
87 else
88 $RUN_PREFIX pyright "${CODE_DIRS[@]}"
89 fi
90 fi
91 }
92
93 function run_all {
94 check_deps --log \
95 isort \
96 flake8 \
97 pytest \
98 autopep8 \
99 pyright
100 if [ $? -ne 0 ] ; then
101 log "Can't run all, did you forget to use a virtual environment?"
102 exit 1
103 fi
104
105 run_with_summary \
106 run_isort \
107 run_flake8 \
108 run_autopep8 \
109 run_pyright \
110 run_pytest
111 }
112
113 function show_help {
114 cat <<-EOF
115 USAGE: ${THIS_PROG} [SUBCOMMAND] [--auto|--autofix] [--color] [-- fwd opts]
116
117 This is a basic way to run CI locally for lookatme. This is the same script
118 that lookatme's CI uses in GitHub actions. Running this script with zero
119 arguments (or the subcommand "all") will run all linting/analysis/tests that
120 CI runs.
121
122 SUBCOMMAND may be one of the below values. The default is "all":
123 all - Run all commands
124 isort - Run isort
125 pytest - Run pytest tests
126 flake8 - Run flake8
127 autopep8 - Run autopep8
128
129 --autofix Run the command's autofix functionality
130 --color Run the command with color
131 --plain Do not box the output of the command, let it be printed plain
132 -- Forward all remaining options to the subcommand
133 EOF
134 exit 1
135 }
136
137 function parse_args {
138 while [ $# -ne 0 ] ; do
139 param="$1"
140 shift
141
142 case "$param" in
143 --help|-h)
144 show_help
145 exit 1
146 ;;
147 all|pytest|flake8|test|isort|autopep8|pyright)
148 SUBCOMMAND="$param"
149 ;;
150 --plain)
151 RUN_PREFIX=""
152 ;;
153 --color)
154 COLOR=true
155 ;;
156 --autofix|--auto)
157 AUTOFIX=true
158 ;;
159 --)
160 while [ $# -ne 0 ] ; do
161 FORWARDED_OPTS+=("$1")
162 shift
163 done
164 ;;
165 *)
166 echo "[!] Unrecognized parameter $param"
167 echo
168 show_help
169 exit 1
170 ;;
171 esac
172 done
173 }
174
175 parse_args "$@"
176
177 case "$SUBCOMMAND" in
178 all)
179 run_all
180 ;;
181 isort)
182 run_isort "${FORWARDED_OPTS[@]}"
183 ;;
184 flake8)
185 run_flake8 "${FORWARDED_OPTS[@]}"
186 ;;
187 autopep8)
188 run_autopep8 "${FORWARDED_OPTS[@]}"
189 ;;
190 pyright)
191 run_pyright "${FORWARDED_OPTS[@]}"
192 ;;
193 test|pytest)
194 run_pytest "${FORWARDED_OPTS[@]}"
195 ;;
196 esac