forked from godot-rust/gdext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.sh
executable file
·223 lines (186 loc) · 6.61 KB
/
check.sh
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Small utility to run tests locally
# Similar to minimal-ci
# Note: at the moment, there is some useless recompilation, which could be improved.
################################################################################
# Constants
################################################################################
# Commands to run (in that order) if none are given on the command line.
DEFAULT_COMMANDS=("fmt" "clippy" "test" "itest")
# Store help text in a variable $HELP_TEXT so we don't need weird indentation later on.
read -r -d '' HELP_TEXT <<EOF
Usage: check.sh [OPTION|COMMAND...]
Each specified command will be run (until one fails).
If no commands are specified, the following commands will be run:
${DEFAULT_COMMANDS[@]}
Commands:
fmt format code, fail if bad
clippy validate clippy lints
test run unit tests (no Godot needed)
itest run integration tests (from within Godot)
doc generate docs for 'godot' crate
dok generate docs and open in browser
Options:
-h, --help print this help text
--double run check with double-precision
Examples:
check.sh fmt clippy
check.sh
check.sh --double clippy
RUSTUP_TOOLCHAIN=nightly check.sh
EOF
# Terminal color codes.
RED='\033[1;31m'
CYAN='\033[1;36m'
END='\033[0m'
################################################################################
# Helper functions
################################################################################
# Drop-in replacement for `echo` that outputs to stderr and adds a newline.
function log() {
echo "$@" >&2
}
# Echoes the given command to stderr, then executes it.
function run() {
# https://stackoverflow.com/a/76153233/14637
echo -n '>' >&2
for arg in "$@"; do
printf " %q" "$arg" >&2
done
echo >&2
"$@"
}
# Finds the Godot binary and stores its path in $godotBin. Logs an error and returns with nonzero
# exit status if not found.
function findGodot() {
# $godotBin previously detected.
if [[ -v godotBin ]]; then
return
# User-defined GODOT4_BIN.
elif [[ -n "$GODOT4_BIN" ]]; then
log "Using environment variable GODOT4_BIN=$(printf %q "$GODOT4_BIN")"
godotBin="$GODOT4_BIN"
# Executable in path.
elif command -v godot4 >/dev/null; then
log "Found 'godot4' executable"
godotBin="godot4"
# Special case for Windows when there is a .bat file.
# Also consider that 'cmd /c' would need 'cmd //c' (https://stackoverflow.com/q/21357813)
elif godot4.bat --version 2>/dev/null; then
log "Found 'godot4.bat' script"
godotBin="godot4.bat"
# This should come last: only use this as a last resort as `godot` may refer to a
# Godot 3.x installation.
elif command -v godot >/dev/null; then
# Check if `godot` actually is Godot 4.x
godotVersion="$(command godot --version)"
if [[ "$godotVersion" =~ ^4\. ]]; then
log "Found 'godot' executable with version $godotVersion"
godotBin="godot"
else
log "Found 'godot' executable, but it has incompatible version $godotVersion"
return 1
fi
# Error case.
else
log "Godot executable not found; try setting GODOT4_BIN to the full path to the executable"
return 1
fi
}
################################################################################
# Commands
################################################################################
# Surrogate namespacing: all commands are prefixed with `cmd_` to avoid confusion with shell
# builtins like `test`.
function cmd_fmt() {
run cargo fmt --all -- --check
}
function cmd_clippy() {
run cargo clippy "${extraCargoArgs[@]}" -- \
-D clippy::suspicious \
-D clippy::style \
-D clippy::complexity \
-D clippy::perf \
-D clippy::dbg_macro \
-D clippy::todo \
-D clippy::unimplemented \
-D warnings
}
function cmd_test() {
run cargo test "${extraCargoArgs[@]}"
}
function cmd_itest() {
findGodot && \
run cargo build -p itest "${extraCargoArgs[@]}" && \
run "$godotBin" --path itest/godot --headless
}
function cmd_doc() {
run cargo doc --lib -p godot --no-deps "${extraCargoArgs[@]}"
}
function cmd_dok() {
run cargo doc --lib -p godot --no-deps "${extraCargoArgs[@]}" --open
}
################################################################################
# Argument parsing
################################################################################
# By default, disable `codegen-full` to reduce compile times and prevent flip-flopping
# between `itest` compilations and `check.sh` runs.
extraCargoArgs=("--no-default-features")
cmds=()
for arg in "$@"; do
case "$arg" in
-h | --help | help)
echo "$HELP_TEXT"
exit 0
;;
--double)
extraCargoArgs+=("--features" "godot/double-precision")
;;
fmt | clippy | test | itest | doc | dok)
cmds+=("$arg")
;;
*)
log "Unrecognized argument '$arg'. Use '$0 --help' to see what's available."
exit 2
;;
esac
done
# Default if no commands are explicitly given.
if [[ ${#cmds[@]} -eq 0 ]]; then
cmds=("${DEFAULT_COMMANDS[@]}")
fi
################################################################################
# Execution and summary
################################################################################
function compute_elapsed() {
local total=$SECONDS
local min=$(("$total" / 60))
if [[ "$min" -gt 0 ]]; then
min="${min}min "
else
min=""
fi
local sec=$(("$total" % 60))
# Don't use echo and call it with $(compute_elapsed), it messes with stdout
elapsed="${min}${sec}s"
}
for cmd in "${cmds[@]}"; do
"cmd_${cmd}" || {
compute_elapsed
log -ne "$RED\n====================="
log -ne "\ngdext: checks FAILED."
log -ne "\n=====================\n$END"
log -ne "\nTotal duration: $elapsed.\n"
exit 1
}
done
compute_elapsed
log -ne "$CYAN\n========================="
log -ne "\ngdext: checks SUCCESSFUL."
log -ne "\n=========================\n$END"
log -ne "\nTotal duration: $elapsed.\n"
# If invoked with sh instead of bash, pressing Up arrow after executing `sh check.sh` may cause a `[A` to appear.
# See https://unix.stackexchange.com/q/103608.