forked from asdf-vm/asdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint.bash
executable file
·190 lines (162 loc) · 4.7 KB
/
lint.bash
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
print.info() {
printf '[INFO] %s\n' "$*"
}
print.error() {
printf '[ERROR] %s\n' "$*" >&2
}
usage() {
printf "%s\n" "Lint script for the asdf codebase. Must be executed from the"
printf "%s\n\n" "repository root directory."
printf "%s\n\n" "Usage: scripts/lint.bash [options]"
printf "%s\n" "Options:"
printf "%s\n" " -c, --check Error if any issues are found"
printf "%s\n" " -f, --fix Automatically fix issues if possible"
printf "%s\n" " -h, --help Display this help message"
}
run_shfmt_stylecheck() {
local shfmt_flag=""
if [ "$1" = "fix" ]; then
shfmt_flag="--write"
else
shfmt_flag="--diff"
fi
print.info "Checking .bash with shfmt"
shfmt --language-dialect bash --indent 2 "${shfmt_flag}" \
internal/completions/*.bash \
bin/asdf \
bin/private/asdf-exec \
lib/utils.bash \
lib/commands/*.bash \
lib/functions/*.bash \
scripts/*.bash \
test/test_helpers.bash \
test/fixtures/dummy_broken_plugin/bin/* \
test/fixtures/dummy_legacy_plugin/bin/* \
test/fixtures/dummy_plugin/bin/*
print.info "Checking .bats with shfmt"
shfmt --language-dialect bats --indent 2 "${shfmt_flag}" \
test/*.bats
}
run_shellcheck_linter() {
print.info "Checking .sh files with Shellcheck"
shellcheck --shell sh --external-sources \
asdf.sh
print.info "Checking .bash files with Shellcheck"
shellcheck --shell bash --external-sources \
internal/completions/*.bash \
bin/asdf \
bin/private/asdf-exec \
lib/utils.bash \
lib/commands/*.bash \
lib/functions/*.bash \
scripts/*.bash \
test/test_helpers.bash \
test/fixtures/dummy_broken_plugin/bin/* \
test/fixtures/dummy_legacy_plugin/bin/* \
test/fixtures/dummy_plugin/bin/*
print.info "Checking .bats files with Shellcheck"
shellcheck --shell bats --external-source \
test/*.bats
}
run_custom_python_stylecheck() {
local github_actions=${GITHUB_ACTIONS:-}
local flag=
if [ "$1" = "fix" ]; then
flag="--fix"
fi
if [ -n "$github_actions" ] && ! command -v python3 &>/dev/null; then
# fail if CI and no python3
print.error "Detected execution in GitHub Actions but python3 was not found. This is required during CI linting."
exit 1
fi
if ! command -v python3 &>/dev/null; then
# skip if local and no python3
printf "%s\n" "[WARNING] python3 not found. Skipping Custom Python Script."
else
print.info "Checking files with Custom Python Script."
"${0%/*}/checkstyle.py" "${flag}"
fi
}
# TODO: there is no elvish linter/formatter yet
# see https://github.com/elves/elvish/issues/1651
#run_elvish_linter() {
# printf "%s\n" "[WARNING] elvish linter/formatter not found, skipping for now."
#}
run_fish_linter() {
local github_actions=${GITHUB_ACTIONS:-}
local flag=
if [ "$1" = "fix" ]; then
flag="--write"
else
flag="--check"
fi
if [ -n "$github_actions" ] && ! command -v fish_indent &>/dev/null; then
# fail if CI and no fish_ident
print.error "Detected execution in GitHub Actions but fish_indent was not found. This is required during CI linting."
exit 1
fi
if ! command -v fish_indent &>/dev/null; then
# skip if local and no fish_ident
printf "%s\n" "[WARNING] fish_indent not found. Skipping .fish files."
else
print.info "Checking .fish files with fish_indent"
fish_indent "${flag}" ./internal/completions/asdf.fish
fi
}
# TODO: there is no nushell linter/formatter yet
#run_nushell_linter() {
# printf "%s\n" "[WARNING] nushell linter/formatter not found, skipping for now."
#}
# TODO: select powershell linter/formatter & setup installation in CI
#run_powershell_linter() {
# printf "%s\n" "[WARNING] powershell linter/formatter not found, skipping for now."
#}
{
repo_dir=$(git rev-parse --show-toplevel)
current_dir=$(pwd -P)
if [ "$repo_dir" != "$current_dir" ]; then
print.error "This scripts requires execution from the repository root directory."
printf "\t%s\t%s\n" "Repo root dir:" "$repo_dir"
printf "\t%s\t%s\n\n" "Current dir:" "$current_dir"
exit 1
fi
}
if [ $# -eq 0 ]; then
print.error "At least one option required."
printf "=%.0s" {1..60}
printf "\n"
usage
exit 1
fi
mode=
case "$1" in
-h | --help)
usage
exit 0
;;
-c | --check)
mode="check"
;;
-f | --fix)
mode="fix"
;;
*)
print.error "Invalid flag: $1"
printf "=%.0s" {1..60}
printf "\n"
usage
exit 1
;;
esac
printf "%s\"%s\"\n" "[INFO] Executing with mode: " "$mode"
run_shfmt_stylecheck "$mode"
run_custom_python_stylecheck "$mode"
run_shellcheck_linter "$mode"
run_fish_linter "$mode"
#run_elvish_linter "$mode"
#run_nushell_linter "$mode"
#run_powershell_linter "$mode"
print.info "Success!"