|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Small utility to compile and run examples |
| 4 | + |
| 5 | +# No args specified: do everything |
| 6 | +if [ "$#" -eq 0 ]; then |
| 7 | + args=("--help") |
| 8 | +else |
| 9 | + args=("$@") |
| 10 | +fi |
| 11 | + |
| 12 | +# --help menu |
| 13 | +for arg in "${args[@]}"; do |
| 14 | + if [ "$arg" == "--help" ]; then |
| 15 | + echo "Usage: example.sh <command> <example-name>" |
| 16 | + echo "" |
| 17 | + echo "Commands:" |
| 18 | + echo " run run the specified example" |
| 19 | + echo " edit open the specified example in the editor" |
| 20 | + echo "" |
| 21 | + echo "Examples:" |
| 22 | + echo " example.sh run hello-world" |
| 23 | + exit 0 |
| 24 | + fi |
| 25 | +done |
| 26 | + |
| 27 | +if [ "$#" -ne 2 ]; then |
| 28 | + echo "Both the command and the name of the example are required." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +function findGodot() { |
| 33 | + # User-defined GODOT_BIN |
| 34 | + if [ -n "$GODOT_BIN" ]; then |
| 35 | + echo "Found GODOT_BIN env var ($GODOT_BIN)" |
| 36 | + godotBin="$GODOT_BIN" |
| 37 | + |
| 38 | + # Executable in path |
| 39 | + elif command -v godot &>/dev/null; then |
| 40 | + echo "Found 'godot' executable" |
| 41 | + godotBin="godot" |
| 42 | + |
| 43 | + # Special case for Windows when there is a .bat file |
| 44 | + # Also consider that 'cmd /c' would need 'cmd //c' (https://stackoverflow.com/q/21357813) |
| 45 | + elif |
| 46 | + # Godot returns 255 for older versions, but 0 for newer ones |
| 47 | + godot.bat --version |
| 48 | + [[ $? -eq 255 || $? -eq 0 ]] |
| 49 | + then |
| 50 | + echo "Found 'godot.bat' script" |
| 51 | + godotBin="godot.bat" |
| 52 | + |
| 53 | + # Error case |
| 54 | + else |
| 55 | + echo "Godot executable not found" |
| 56 | + exit 2 |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +example_path="${BASH_SOURCE%/*}/examples/${args[1]}" |
| 61 | + |
| 62 | +if ! [[ -d "$example_path" ]]; then |
| 63 | + echo "The example ${args[1]} is not found." |
| 64 | + exit 2 |
| 65 | +fi |
| 66 | + |
| 67 | +findGodot |
| 68 | + |
| 69 | +cmds=() |
| 70 | + |
| 71 | +case "${args[0]}" in |
| 72 | +run) |
| 73 | + cmds+=("cargo build --manifest-path $example_path/Cargo.toml") |
| 74 | + cmds+=("$godotBin --path $example_path") |
| 75 | + ;; |
| 76 | +edit) |
| 77 | + cmds+=("cargo build --manifest-path $example_path/Cargo.toml") |
| 78 | + cmds+=("$godotBin -e --path $example_path") |
| 79 | + ;; |
| 80 | +*) |
| 81 | + echo "Unrecognized command '$arg'" |
| 82 | + exit 2 |
| 83 | + ;; |
| 84 | +esac |
| 85 | + |
| 86 | +for cmd in "${cmds[@]}"; do |
| 87 | + echo "> $cmd" |
| 88 | + $cmd || { |
| 89 | + exit 1 |
| 90 | + } |
| 91 | +done |
0 commit comments