|
8 | 8 |
|
9 | 9 | # Note: at the moment, there is some useless recompilation, which could be improved.
|
10 | 10 |
|
11 |
| -# --help menu |
12 |
| -for arg in $@; do |
13 |
| - if [ "$arg" == "--help" ]; then |
14 |
| - echo "Usage: check.sh [--double] [<commands>]" |
15 |
| - echo "" |
16 |
| - echo "Each specified command will be run (until one fails)." |
17 |
| - echo "If no commands are specified, all checks are run (no doc; may take several minutes)." |
18 |
| - echo "" |
19 |
| - echo "Commands:" |
20 |
| - echo " fmt format code, fail if bad" |
21 |
| - echo " clippy validate clippy lints" |
22 |
| - echo " test run unit tests (no Godot)" |
23 |
| - echo " itest run integration tests (Godot)" |
24 |
| - echo " doc generate docs for 'godot' crate" |
25 |
| - echo " dok generate docs and open in browser" |
26 |
| - echo "" |
27 |
| - echo "Options:" |
28 |
| - echo " --double run check with double-precision" |
29 |
| - echo "" |
30 |
| - echo "Examples:" |
31 |
| - echo " check.sh fmt clippy" |
32 |
| - echo " check.sh" |
33 |
| - echo " check.sh --double clippy" |
34 |
| - exit 0 |
35 |
| - fi |
36 |
| -done |
37 |
| - |
38 |
| -firstArg=1 |
39 |
| -toolchain="" |
40 |
| -extraArgs=() |
41 |
| - |
42 |
| -if [[ "$1" == "--double" ]]; then |
43 |
| - firstArg=2 |
44 |
| - extraArgs+=("--features double-precision") |
45 |
| -fi |
| 11 | +################################################################################ |
| 12 | +# Constants |
| 13 | +################################################################################ |
| 14 | + |
| 15 | +# Commands to run (in that order) if none are given on the command line. |
| 16 | +DEFAULT_COMMANDS=("fmt" "clippy" "test" "itest") |
| 17 | + |
| 18 | +# Store help text in a variable $HELP_TEXT so we don't need weird indentation later on. |
| 19 | +read -r -d '' HELP_TEXT <<EOF |
| 20 | +Usage: check.sh [OPTION|COMMAND...] |
| 21 | +
|
| 22 | +Each specified command will be run (until one fails). |
| 23 | +If no commands are specified, the following commands will be run: |
| 24 | + ${DEFAULT_COMMANDS[@]} |
| 25 | +
|
| 26 | +Commands: |
| 27 | + fmt format code, fail if bad |
| 28 | + clippy validate clippy lints |
| 29 | + test run unit tests (no Godot needed) |
| 30 | + itest run integration tests (from within Godot) |
| 31 | + doc generate docs for 'godot' crate |
| 32 | + dok generate docs and open in browser |
| 33 | +
|
| 34 | +Options: |
| 35 | + -h, --help print this help text |
| 36 | + --double run check with double-precision |
| 37 | +
|
| 38 | +Examples: |
| 39 | + check.sh fmt clippy |
| 40 | + check.sh |
| 41 | + check.sh --double clippy |
| 42 | + RUSTUP_TOOLCHAIN=nightly check.sh |
| 43 | +EOF |
| 44 | + |
| 45 | +# Terminal color codes. |
| 46 | +RED='\033[1;31m' |
| 47 | +CYAN='\033[1;36m' |
| 48 | +END='\033[0m' |
46 | 49 |
|
47 |
| -args=() |
| 50 | +################################################################################ |
| 51 | +# Helper functions |
| 52 | +################################################################################ |
48 | 53 |
|
49 |
| -for arg in "${@:$firstArg}"; do |
50 |
| - args+=("$arg") |
51 |
| -done |
| 54 | +# Drop-in replacement for `echo` that outputs to stderr and adds a newline. |
| 55 | +function log() { |
| 56 | + echo "$@" >&2 |
| 57 | +} |
52 | 58 |
|
53 |
| -# No args specified: do everything |
54 |
| -if [ ${#args[@]} -eq 0 ]; then |
55 |
| - args=("fmt" "clippy" "test" "itest") |
56 |
| -fi |
| 59 | +# Echoes the given command to stderr, then executes it. |
| 60 | +function run() { |
| 61 | + # https://stackoverflow.com/a/76153233/14637 |
| 62 | + echo -n '>' >&2 |
| 63 | + for arg in "$@"; do |
| 64 | + printf " %q" "$arg" >&2 |
| 65 | + done |
| 66 | + echo >&2 |
| 67 | + "$@" |
| 68 | +} |
57 | 69 |
|
58 |
| -# For integration tests |
| 70 | +# Finds the Godot binary and stores its path in $godotBin. Logs an error and returns with nonzero |
| 71 | +# exit status if not found. |
59 | 72 | function findGodot() {
|
60 |
| - # User-defined GODOT4_BIN |
61 |
| - if [ -n "$GODOT4_BIN" ]; then |
62 |
| - echo "Found GODOT4_BIN env var ($GODOT4_BIN)" |
| 73 | + # $godotBin previously detected. |
| 74 | + if [[ -v godotBin ]]; then |
| 75 | + return |
| 76 | + |
| 77 | + # User-defined GODOT4_BIN. |
| 78 | + elif [[ -n "$GODOT4_BIN" ]]; then |
| 79 | + log "Using environment variable GODOT4_BIN=$(printf %q "$GODOT4_BIN")" |
63 | 80 | godotBin="$GODOT4_BIN"
|
64 | 81 |
|
65 |
| - # Executable in path |
66 |
| - elif command -v godot4 &>/dev/null; then |
67 |
| - echo "Found 'godot4' executable" |
| 82 | + # Executable in path. |
| 83 | + elif command -v godot4 >/dev/null; then |
| 84 | + log "Found 'godot4' executable" |
68 | 85 | godotBin="godot4"
|
69 | 86 |
|
70 |
| - # Special case for Windows when there is a .bat file |
| 87 | + # Special case for Windows when there is a .bat file. |
71 | 88 | # Also consider that 'cmd /c' would need 'cmd //c' (https://stackoverflow.com/q/21357813)
|
72 |
| - elif |
73 |
| - godot4.bat --version |
74 |
| - [[ $? -eq 0 ]] |
75 |
| - then |
76 |
| - echo "Found 'godot4.bat' script" |
| 89 | + elif godot4.bat --version 2>/dev/null; then |
| 90 | + log "Found 'godot4.bat' script" |
77 | 91 | godotBin="godot4.bat"
|
78 | 92 |
|
79 |
| - # This should come last: only use this as a last resort as usually `godot` |
80 |
| - # refers to a Godot 3.x installation. |
81 |
| - elif command -v godot &>/dev/null; then |
| 93 | + # This should come last: only use this as a last resort as `godot` may refer to a |
| 94 | + # Godot 3.x installation. |
| 95 | + elif command -v godot >/dev/null; then |
82 | 96 | # Check if `godot` actually is Godot 4.x
|
83 |
| - if godot --version | grep -qE "^4\\."; then |
84 |
| - echo "Found 'godot' executable with version $(godot --version)" |
| 97 | + godotVersion="$(command godot --version)" |
| 98 | + if [[ "$godotVersion" =~ ^4\. ]]; then |
| 99 | + log "Found 'godot' executable with version $godotVersion" |
85 | 100 | godotBin="godot"
|
86 | 101 | else
|
87 |
| - echo "Found 'godot' executable, but it has the incompatible version $(godot --version)" |
88 |
| - exit 2 |
| 102 | + log "Found 'godot' executable, but it has incompatible version $godotVersion" |
| 103 | + return 1 |
89 | 104 | fi
|
90 | 105 |
|
91 |
| - # Error case |
| 106 | + # Error case. |
92 | 107 | else
|
93 |
| - echo "Godot executable not found" |
94 |
| - exit 2 |
| 108 | + log "Godot executable not found; try setting GODOT4_BIN to the full path to the executable" |
| 109 | + return 1 |
95 | 110 | fi
|
96 | 111 | }
|
97 | 112 |
|
| 113 | +################################################################################ |
| 114 | +# Commands |
| 115 | +################################################################################ |
| 116 | + |
| 117 | +# Surrogate namespacing: all commands are prefixed with `cmd_` to avoid confusion with shell |
| 118 | +# builtins like `test`. |
| 119 | + |
| 120 | +function cmd_fmt() { |
| 121 | + run cargo fmt --all -- --check |
| 122 | +} |
| 123 | + |
| 124 | +function cmd_clippy() { |
| 125 | + run cargo clippy "${extraCargoArgs[@]}" -- \ |
| 126 | + -D clippy::suspicious \ |
| 127 | + -D clippy::style \ |
| 128 | + -D clippy::complexity \ |
| 129 | + -D clippy::perf \ |
| 130 | + -D clippy::dbg_macro \ |
| 131 | + -D clippy::todo \ |
| 132 | + -D clippy::unimplemented \ |
| 133 | + -D warnings |
| 134 | +} |
| 135 | + |
| 136 | +function cmd_test() { |
| 137 | + run cargo test "${extraCargoArgs[@]}" |
| 138 | +} |
| 139 | + |
| 140 | +function cmd_itest() { |
| 141 | + findGodot && \ |
| 142 | + run cargo build -p itest "${extraCargoArgs[@]}" && \ |
| 143 | + run "$godotBin" --path itest/godot --headless |
| 144 | +} |
| 145 | + |
| 146 | +function cmd_doc() { |
| 147 | + run cargo doc --lib -p godot --no-deps "${extraCargoArgs[@]}" |
| 148 | +} |
| 149 | + |
| 150 | +function cmd_dok() { |
| 151 | + run cargo doc --lib -p godot --no-deps "${extraCargoArgs[@]}" --open |
| 152 | +} |
| 153 | + |
| 154 | +################################################################################ |
| 155 | +# Argument parsing |
| 156 | +################################################################################ |
| 157 | + |
98 | 158 | cmds=()
|
99 |
| -extraArgs="${extraArgs[@]}" |
| 159 | +extraCargoArgs=() |
100 | 160 |
|
101 |
| -for arg in "${args[@]}"; do |
| 161 | +for arg in "$@"; do |
102 | 162 | case "$arg" in
|
103 |
| - fmt) |
104 |
| - cmds+=("cargo $toolchain fmt --all -- --check") |
105 |
| - ;; |
106 |
| - clippy) |
107 |
| - cmds+=("cargo $toolchain clippy $extraArgs -- -D clippy::suspicious -D clippy::style -D clippy::complexity -D clippy::perf -D clippy::dbg_macro -D clippy::todo -D clippy::unimplemented -D warnings") |
108 |
| - ;; |
109 |
| - test) |
110 |
| - cmds+=("cargo $toolchain test $extraArgs") |
111 |
| - ;; |
112 |
| - itest) |
113 |
| - findGodot |
114 |
| - |
115 |
| - cmds+=("cargo $toolchain build -p itest $extraArgs") |
116 |
| - cmds+=("$godotBin --path itest/godot --headless") |
117 |
| - ;; |
118 |
| - doc) |
119 |
| - cmds+=("cargo $toolchain doc --lib -p godot --no-deps $extraArgs") |
120 |
| - ;; |
121 |
| - dok) |
122 |
| - cmds+=("cargo $toolchain doc --lib -p godot --no-deps $extraArgs --open") |
123 |
| - ;; |
124 |
| - *) |
125 |
| - echo "Unrecognized command '$arg'" |
126 |
| - exit 2 |
127 |
| - ;; |
| 163 | + -h | --help | help) |
| 164 | + echo "$HELP_TEXT" |
| 165 | + exit 0 |
| 166 | + ;; |
| 167 | + --double) |
| 168 | + extraCargoArgs+=("--features" "double-precision") |
| 169 | + ;; |
| 170 | + fmt | clippy | test | itest | doc | dok) |
| 171 | + cmds+=("$arg") |
| 172 | + ;; |
| 173 | + *) |
| 174 | + log "Unrecognized argument '$arg'. Use '$0 --help' to see what's available." |
| 175 | + exit 2 |
| 176 | + ;; |
128 | 177 | esac
|
129 | 178 | done
|
130 | 179 |
|
131 |
| -RED='\033[1;31m' |
132 |
| -GREEN='\033[1;36m' |
133 |
| -END='\033[0m' |
| 180 | +# Default if no commands are explicitly given. |
| 181 | +if [[ ${#cmds[@]} -eq 0 ]]; then |
| 182 | + cmds=("${DEFAULT_COMMANDS[@]}") |
| 183 | +fi |
| 184 | + |
| 185 | +################################################################################ |
| 186 | +# Execution |
| 187 | +################################################################################ |
| 188 | + |
134 | 189 | for cmd in "${cmds[@]}"; do
|
135 |
| - echo "> $cmd" |
136 |
| - $cmd || { |
137 |
| - printf "$RED\n=====================" |
138 |
| - printf "\ngdext: checks FAILED." |
139 |
| - printf "\n=====================\n$END" |
| 190 | + "cmd_${cmd}" || { |
| 191 | + log -ne "$RED\n=====================" |
| 192 | + log -ne "\ngdext: checks FAILED." |
| 193 | + log -ne "\n=====================\n$END" |
140 | 194 | exit 1
|
141 | 195 | }
|
142 | 196 | done
|
143 | 197 |
|
144 |
| -printf "$GREEN\n=========================" |
145 |
| -printf "\ngdext: checks SUCCESSFUL." |
146 |
| -printf "\n=========================\n$END" |
| 198 | +log -ne "$CYAN\n=========================" |
| 199 | +log -ne "\ngdext: checks SUCCESSFUL." |
| 200 | +log -ne "\n=========================\n$END" |
0 commit comments