-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlint
More file actions
executable file
·104 lines (92 loc) · 2.94 KB
/
lint
File metadata and controls
executable file
·104 lines (92 loc) · 2.94 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
# wrapper around linter/formatters
# pauses at each step if there are errors
set -o pipefail
# get the name of this script
declare script_name
script_name="$(basename "${BASH_SOURCE[0]}")"
# function to verify an external command is installed
havecmd() {
local BINARY ERRMSG
# error if first argument isn't provided
BINARY="${1:?Must provide command to check}"
# the commend exists, exit with 0 (success!)
if command -v "${BINARY}" >/dev/null 2>&1; then
return 0
else
# construct error message
ERRMSG="'${script_name}' requires '${BINARY}', could not find that on your \$PATH"
if [[ -n "$2" ]]; then
ERRMSG="${ERRMSG}. $2"
fi
printf '%s\n' "${ERRMSG}" 1>&2
return 1
fi
}
set -e
havecmd shellcheck
havecmd exists 'See https://github.com/seanbreckenridge/exists'
havecmd rifleman 'See https://github.com/seanbreckenridge/rifleman'
havecmd pytest
havecmd jq
havecmd tput
havecmd mypy
havecmd flake8
havecmd black
havecmd shfmt
set +e
cd "$(realpath "$(dirname "${BASH_SOURCE[0]}")")" || {
echo "Couldn't cd to current dir"
exit 1
}
# E225,E501,E302,E402,E305,E261,E265,E116,E252,E702,E266,E741,E306,E203,E731,W503
readonly FLAKE8_IGNORE='E402,E501,E741,W503,E266,E302,E305,E203'
readonly FLAKE8_FILES_IGNORE=''
# call shellcheck on all the scripts
shellcheck_scripts() {
git ls-files | exists | rifleman - -a lint -j | jq -r 'to_entries[] | select(.key|startswith("shellcheck")) | .value | .[]' | xargs shellcheck
}
prompt() {
local MESSAGE
MESSAGE='Hit enter to continue > '
[[ -n "$1" ]] && MESSAGE="$1"
echo -en "$(tput setaf 1)${MESSAGE}$(tput sgr0)"
read -r # if no variable is specified, sets the REPLY environment variable
}
custom_flake8() {
local FLAKE8_OUTPUT FLAKE8_OUTPUT_LENGTH
printf "Running flake8 with:\nignored rules: %s\nIgnored file patterns:\n%s\n" "${FLAKE8_IGNORE}" "$(tr ',' '\n' <<<"${FLAKE8_FILES_IGNORE}")"
# capture flake8 output so I can determine whether or not there were lines returned
# if there were return 1, so it prompts to wait while I look at the error
FLAKE8_OUTPUT="$(find my -iname '*.py' -exec flake8 --ignore="${FLAKE8_IGNORE}" --extend-exclude="${FLAKE8_FILES_IGNORE}" {} +)"
FLAKE8_OUTPUT_LENGTH="$(wc -c <<<"${FLAKE8_OUTPUT}")"
# ignore newlines/spaces for some reason, all errors will have string length > 5
if ((FLAKE8_OUTPUT_LENGTH > 5)); then
printf '%s\n' "${FLAKE8_OUTPUT}"
return 1
else
return 0
fi
}
update_fork() {
local FORK_LOCATION
FORK_LOCATION="$(realpath ../HPI-fork/)"
cd "${FORK_LOCATION}" || return $?
git checkout master
git pull upstream master
}
main() {
(update_fork) # cd in subshell
custom_flake8 || prompt ''
pytest "$@" || prompt ''
echo "Running mypy..."
mypy --color-output --ignore-missing-imports -p my || prompt ''
mypy ~/.config/my/my/config/ || prompt ''
echo "Running shellcheck..."
shellcheck_scripts
# format everything in the repo
git ls-files | exists | rifleman -
echo -e "$(tput setaf 2)Done!$(tput sgr0)"
git status
}
main "$@"