|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +default_dirs=( "src/script/scala" ) |
| 4 | +out_root="target/script-tests" |
| 5 | +out_ext="out" |
| 6 | + |
| 7 | +error() { |
| 8 | + echo "ERROR: $@" |
| 9 | + help |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +help() { |
| 14 | + cat << EOF |
| 15 | +Checks the script files by loading them in the interpreter. Note that Scala 3 |
| 16 | +changes the way "scripts" are interpreted. They are now expected to have @main |
| 17 | +methods (or old-style main methods), while the "scripts" in this repo are really |
| 18 | +just intended for interactive sessions. Hence, ":load file" is used here. There |
| 19 | +are files with @main methods under src/main that can be interpreted as Scala 3 |
| 20 | +"scripts" by the REPL, in which case you just pass the path as a command-line |
| 21 | +argument. |
| 22 | +
|
| 23 | +So, this bash script starts the REPL (using "sbt console") for each file and then |
| 24 | +uses :load to load the file. The output is written to |
| 25 | +$out_root/path/to/file.$out_ext. |
| 26 | +
|
| 27 | +Some files DO throw errors. In some cases, you'll see a comment on the same line |
| 28 | +like "// ERROR". In other cases, you have to look at the book discussion to see |
| 29 | +if the error is expected. Unfortunately, all the output has to be inspected manually. |
| 30 | +If you see lots of errors for any one file, make sure you are using a Scala 3 REPL! |
| 31 | +
|
| 32 | +Usage: $0 [-h|--help] [-v|--verbose] [-c|--clean] [-n|--no-exec] [dir ...] |
| 33 | +Where: |
| 34 | +-h | --help Print this message and exit. |
| 35 | +-v | --verbose Print each file name to the console as it is processed. |
| 36 | +-c | --clean Delete all previous output. |
| 37 | +-n | --no-exec Don't execute the commands, just echo what would be done. |
| 38 | +dir ... Start in these directories. (default "${default_dirs[@]}") |
| 39 | +EOF |
| 40 | +} |
| 41 | + |
| 42 | +: ${VERBOSE:=false} |
| 43 | +: ${CLEAN:=false} |
| 44 | +: ${NOOP:=} |
| 45 | +dirs=() |
| 46 | + |
| 47 | +while [[ $# -gt 0 ]] |
| 48 | +do |
| 49 | + case $1 in |
| 50 | + -h|--h*) |
| 51 | + help |
| 52 | + exit 0 |
| 53 | + ;; |
| 54 | + -v|--v*) |
| 55 | + VERBOSE=true |
| 56 | + ;; |
| 57 | + -c|--c*) |
| 58 | + CLEAN=true |
| 59 | + ;; |
| 60 | + -n|--n*) |
| 61 | + NOOP=echo |
| 62 | + ;; |
| 63 | + -*) |
| 64 | + error "Unknown argument $1" |
| 65 | + ;; |
| 66 | + *) |
| 67 | + dirs+=($1) |
| 68 | + ;; |
| 69 | + esac |
| 70 | + shift |
| 71 | +done |
| 72 | + |
| 73 | +[[ ${#dirs[@]} -gt 0 ]] || dirs=( ${default_dirs[@]} ) |
| 74 | +$VERBOSE && echo "Reading directories ${dirs[@]}" |
| 75 | + |
| 76 | +if $CLEAN |
| 77 | +then |
| 78 | + $VERBOSE && echo "Cleaning old output in $out_root..." |
| 79 | + [[ -n "$out_root" ]] && rm -rf "$out_root" # safety check! |
| 80 | +fi |
| 81 | + |
| 82 | +check() { |
| 83 | + script="$1" |
| 84 | + out="$out_root/$script.$out_ext" |
| 85 | + $NOOP rm -f $out |
| 86 | + $VERBOSE && echo "$f --> $out" |
| 87 | + if [[ -z "$NOOP" ]] |
| 88 | + then |
| 89 | + mkdir -p $(dirname $out) |
| 90 | + TERM=dumb sbt console <<EOF > $out |
| 91 | +:load $script |
| 92 | +EOF |
| 93 | + else |
| 94 | + $NOOP mkdir -p $(dirname $out) |
| 95 | + $NOOP "TERM=dumb sbt console ... :load $script ... > $out" |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +for dir in "${dirs[@]}" |
| 100 | +do |
| 101 | + find "$dir" -name '*.scala' | while read f |
| 102 | + do |
| 103 | + check $f |
| 104 | + done |
| 105 | +done |
0 commit comments