|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Setup environment and then do checks, must pass in Standard source directory |
| 4 | + |
| 5 | +# Where are we? |
| 6 | + |
| 7 | +ScriptPath=$(realpath "${BASH_SOURCE[0]}") |
| 8 | +ScriptDir=$(dirname "$ScriptPath") |
| 9 | + |
| 10 | +_usageDescription () |
| 11 | +{ |
| 12 | + cat <<USAGE |
| 13 | +Usage: SetupAndTest sourceDirectory [options] |
| 14 | +
|
| 15 | +Where: |
| 16 | + sourceDirectory |
| 17 | + The directory of the Standard source (.md) files |
| 18 | + from which to extract the grammar. |
| 19 | +
|
| 20 | +With no options checks grammar and then performs parsing tests. |
| 21 | +
|
| 22 | +Options: |
| 23 | + -g, --grammar |
| 24 | + Perform grammar checks |
| 25 | + -p, --parsing |
| 26 | + Perform parsing tests |
| 27 | + -d, --demo-parsing |
| 28 | + Perform demo parsing tests |
| 29 | + -h, -?, --help |
| 30 | + Displays this help and exits |
| 31 | + -v, --verbose |
| 32 | + Be (more) verbose |
| 33 | + -q, --quiet |
| 34 | + Reduce messages |
| 35 | + -wd, --working-directory |
| 36 | + Directory to store intermediate generated files in: Java source code, |
| 37 | + Java .class files, etc. Directory path follows option separated by |
| 38 | + space or an equals sign (e.g. -wd path or -wd=path) |
| 39 | + -e, --environment-directory |
| 40 | + Directory containing environment – for requirements see documentation. |
| 41 | + If omitted the working directory and directory containing this command |
| 42 | + will be searched. |
| 43 | + -t, --tests-directory |
| 44 | + Directory containing tests – for requirements see documentation. |
| 45 | + If omitted the working directory and directory containing this command |
| 46 | + will be searched. |
| 47 | +USAGE |
| 48 | +} |
| 49 | + |
| 50 | +# define usage(), we can’t use the version from our common.bash |
| 51 | +# library as its location is not yet known |
| 52 | + |
| 53 | +usage() # [optErrorMsg [optExitCode]] |
| 54 | +{ |
| 55 | + # name the parameters |
| 56 | + local _optErrorMsg="$1" |
| 57 | + local _optExitCode="${2:--1}" # default to -1 |
| 58 | + |
| 59 | + # if optErrorMsg exists send all output to stderr |
| 60 | + if [ "$_optErrorMsg" != "" ]; then |
| 61 | + echo "Error: $_optErrorMsg" 1>&2 |
| 62 | + echo 1>&2 |
| 63 | + _usageDescription 1>&2 |
| 64 | + echo 1>&2 |
| 65 | + exit $_optExitCode |
| 66 | + else |
| 67 | + echo |
| 68 | + _usageDescription |
| 69 | + echo |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +# scan all args |
| 74 | + |
| 75 | +doGrammar=false |
| 76 | +doParsing=false |
| 77 | +argSourceDirectory= |
| 78 | +optWorkingDirectory= |
| 79 | +optEnvPath= |
| 80 | +optTestPath= |
| 81 | + |
| 82 | +declare -a doChecksArgs |
| 83 | + |
| 84 | +requireArgument= |
| 85 | + |
| 86 | +for i in "$@"; do |
| 87 | + if [[ "$requireArgument" == "" ]]; then |
| 88 | + case "$i" in |
| 89 | + -g|--grammar) |
| 90 | + doChecksArgs+=($i) |
| 91 | + ;; |
| 92 | + -p|--parsing) |
| 93 | + doChecksArgs+=($i) |
| 94 | + ;; |
| 95 | + -d|--demo-parsing) |
| 96 | + doChecksArgs+=($i) |
| 97 | + ;; |
| 98 | + -h|--help|-\?) |
| 99 | + usage |
| 100 | + exit |
| 101 | + ;; |
| 102 | + -v|--verbose) |
| 103 | + beVerbose=true # so we can take note of this parameter ourselves |
| 104 | + doChecksArgs+=($i) |
| 105 | + ;; |
| 106 | + -q|--quiet) |
| 107 | + beQuiet=true # so we can take note of this parameter ourselves |
| 108 | + doChecksArgs+=($i) |
| 109 | + ;; |
| 110 | + -dbg) |
| 111 | + doChecksArgs+=($i) |
| 112 | + ;; |
| 113 | + -wd|--working-directory) |
| 114 | + if [[ "$optWorkingDirectory" == "" ]]; then |
| 115 | + requireArgument=$i |
| 116 | + else |
| 117 | + usage "Unexpected option $i" |
| 118 | + fi |
| 119 | + ;; |
| 120 | + -wd=*|--working-directory=*) |
| 121 | + if [[ "$optWorkingDirectory" == "" ]]; then |
| 122 | + optWorkingDirectory="${i#*=}" |
| 123 | + else |
| 124 | + usage "Unexpected option $i" |
| 125 | + fi |
| 126 | + ;; |
| 127 | + -e|--environment-directory) |
| 128 | + if [[ "$optEnvPath" == "" ]]; then |
| 129 | + requireArgument=$i |
| 130 | + else |
| 131 | + usage "Unexpected option $i" |
| 132 | + fi |
| 133 | + ;; |
| 134 | + -e=*|--environment-directory=*) |
| 135 | + if [[ "$optEnvPath" == "" ]]; then |
| 136 | + optEnvPath="${i#*=}" |
| 137 | + else |
| 138 | + usage "Unexpected option $i" |
| 139 | + fi |
| 140 | + ;; |
| 141 | + -t|--tests-directory) |
| 142 | + if [[ "$optTestPath" == "" ]]; then |
| 143 | + requireArgument=$i |
| 144 | + else |
| 145 | + usage "Unexpected option $i" |
| 146 | + fi |
| 147 | + ;; |
| 148 | + -t=*|--tests-directory=*) |
| 149 | + if [[ "$optTestPath" == "" ]]; then |
| 150 | + optTestPath="${i#*=}" |
| 151 | + else |
| 152 | + usage "Unexpected option $i" |
| 153 | + fi |
| 154 | + ;; |
| 155 | + -*) |
| 156 | + usage "Unrecognised option $i" |
| 157 | + ;; |
| 158 | + *) |
| 159 | + if [[ "$argSourceDirectory" == "" ]]; then |
| 160 | + argSourceDirectory="$i" |
| 161 | + elif [[ "$i" != "" ]]; then |
| 162 | + usage "Unexpected argument ‘$i’" |
| 163 | + fi |
| 164 | + ;; |
| 165 | + esac |
| 166 | + else # looking for argument after option |
| 167 | + if [[ "$i" == -* ]]; then |
| 168 | + usage "Option $requireArgument missing argument" |
| 169 | + else |
| 170 | + case $requireArgument in |
| 171 | + -wd|--working-directory) |
| 172 | + optWorkingDirectory="$i" |
| 173 | + requireArgument="" |
| 174 | + ;; |
| 175 | + -e|--environment-directory) |
| 176 | + optEnvPath="$i" |
| 177 | + requireArgument="" |
| 178 | + ;; |
| 179 | + -t|--tests-directory) |
| 180 | + optTestPath="$i" |
| 181 | + requireArgument="" |
| 182 | + ;; |
| 183 | + esac |
| 184 | + if [[ "$requireArgument" != "" ]]; then |
| 185 | + errorExit "Internal error, argument expected on none argument option, contact developer" |
| 186 | + fi |
| 187 | + fi |
| 188 | + fi |
| 189 | +done |
| 190 | + |
| 191 | +# check last command line option isn’t missing an argument |
| 192 | + |
| 193 | +if [[ "$requireArgument" != "" ]]; then |
| 194 | + usage "Option $requireArgument missing argument" |
| 195 | +fi |
| 196 | + |
| 197 | +if [[ "$argSourceDirectory" == "" ]]; then |
| 198 | + usage "Missing sourceDirectory" |
| 199 | +fi |
| 200 | + |
| 201 | +########################################################################################## |
| 202 | + |
| 203 | +# Args scanned, now locate environment directory. |
| 204 | +# Try in order: |
| 205 | +# - -e=path argument |
| 206 | +# - Is there an Environment folder in the current WD? |
| 207 | +# - Is there an Environment folder relative to the script source? |
| 208 | +# If all fail barf |
| 209 | + |
| 210 | +envDirectoryName="Environment" |
| 211 | +libRelativePath="Tools/lib/common.bash" |
| 212 | +envPath= |
| 213 | + |
| 214 | +# first locate the environment |
| 215 | +# check for script library within the folder as the (minimal) test for validity |
| 216 | +if [[ ("$optEnvPath" != "") && (-f "${optEnvPath}/${libRelativePath}") ]]; then |
| 217 | + envPath="${optEnvPath}" |
| 218 | + envSrc="ARG" |
| 219 | +elif [[ -f "${envDirectoryName}/${libRelativePath}" ]]; then |
| 220 | + envPath="${envDirectoryName}" |
| 221 | + envSrc="CWD" |
| 222 | +elif [[ -f "${ScriptDir}/${envDirectoryName}/${libRelativePath}" ]]; then |
| 223 | + envPath="${ScriptDir}/${envDirectoryName}" |
| 224 | + envSrc="ADJ" |
| 225 | +else |
| 226 | + echo "Could not locate “${envDirectoryName}” directory" 1>&2 |
| 227 | + echo "Consult the documentation" 1>&2 |
| 228 | + exit -1 |
| 229 | +fi |
| 230 | + |
| 231 | +envPath=$(realpath "$envPath") |
| 232 | + |
| 233 | +# Environment located, can now source our common library from the environment |
| 234 | +source "${envPath}/${libRelativePath}" |
| 235 | + |
| 236 | +# we now have debugEcho… |
| 237 | +debugEcho "envPath = [$envSrc] ${envPath}" |
| 238 | + |
| 239 | +########################################################################################## |
| 240 | + |
| 241 | +# Environment found, common source loaded, now locate the tests directory. |
| 242 | +# this does not validate the structure of the tests folder in any way |
| 243 | +# Try in order: |
| 244 | +# - -t=path argument |
| 245 | +# - Is there a Tests folder in the current WD? |
| 246 | +# - Is there a Tests folder relative to the script source? |
| 247 | +# If all fail barf |
| 248 | + |
| 249 | +# Much shorter than above as we can now use the common source version |
| 250 | + |
| 251 | +testPath= |
| 252 | +searchForDirectory testPath "$optTestPath" "Tests" "" "" "$ScriptDir" "" |
| 253 | + |
| 254 | +# pass on to DoChecksAndTests below – avoids DoChecksAndTests needing to search (and possibly failing) |
| 255 | +doChecksArgs+=("-t=${testPath}") |
| 256 | + |
| 257 | +########################################################################################## |
| 258 | + |
| 259 | +# Final checks and then source/call others to do the work |
| 260 | + |
| 261 | +# validate source directory and add to args |
| 262 | +validateDirectoryPath sourceDirectory "$argSourceDirectory" "Invalid source directory \"$argSourceDirectory\"" -1 |
| 263 | +doChecksArgs+=("$sourceDirectory") |
| 264 | + |
| 265 | +# create working directory if needed and add to command |
| 266 | +if [[ "$optWorkingDirectory" != "" ]]; then |
| 267 | + validateOrCreateDirectoryPath baseWorkingDirectory "$optWorkingDirectory" "Failed to create/access \"$optWorkingDirectory\"" |
| 268 | + doChecksArgs+=("-wd=$baseWorkingDirectory") |
| 269 | +fi |
| 270 | + |
| 271 | +# sanity precheck the environment |
| 272 | +if [[ ! -f "${envPath}/setup.SourceMe" ]]; then |
| 273 | + errorEcho "File ”setup.SourceMe” is missing in the environment directory." |
| 274 | + errorEcho "($envPath)" |
| 275 | + errorExit "Cannot setup the environment" |
| 276 | +fi |
| 277 | + |
| 278 | +if [[ ${beVerbose} == true ]]; then |
| 279 | + source "${envPath}/setup.SourceMe" |
| 280 | +else |
| 281 | + echo "Configuring environment..." |
| 282 | + source "${envPath}/setup.SourceMe" >/dev/null |
| 283 | +fi |
| 284 | + |
| 285 | +exitStatus=$? |
| 286 | +if [[ $exitStatus != 0 ]]; then # did setup succeed? |
| 287 | + exit $exitStatus |
| 288 | +fi |
| 289 | + |
| 290 | +# sanity postcheck the environment |
| 291 | +for i in antlr grun BuildGrammar; do |
| 292 | + which $i >/dev/null |
| 293 | + if [[ $? != 0 ]]; then |
| 294 | + errorExit "Command “${i}” not found in environment" |
| 295 | + fi |
| 296 | +done |
| 297 | + |
| 298 | +for i in f:BG_BASHLIBRARY d:BG_MODIFICATIONSETS f:BG_GRUNTREE_GRAMMAR d:BG_LIB_PARSINGTESTS; do |
| 299 | + _kind=${i:0:1} |
| 300 | + _var=${i:2} |
| 301 | + _target="${!_var}" |
| 302 | + debugEcho "Checking: ${_kind} | ${_var}=${_target}" |
| 303 | + if [[ "${_target}" == "" ]]; then |
| 304 | + errorExit "Variable ${_var} not defined" |
| 305 | + elif [[ (${_kind} == d) && (! -d "${_target}") ]]; then |
| 306 | + errorExit "${_var} must reference a directory" |
| 307 | + elif [[ (${_kind} == f) && (! -f "${_target}") ]]; then |
| 308 | + errorExit "${_var} must reference a file" |
| 309 | + fi |
| 310 | +done |
| 311 | + |
| 312 | +# we seem to have what we expect, run the tests… |
| 313 | + |
| 314 | +# use a DoChecksAndTests in testPath |
| 315 | +if [[ -x "${testPath}/DoChecksAndTests" ]]; then |
| 316 | + "${testPath}/DoChecksAndTests" "${doChecksArgs[@]}" |
| 317 | + exit $? |
| 318 | +fi |
| 319 | + |
| 320 | +# check for DoChecksAndTests in the PATH |
| 321 | +which DoChecksAndTests >/dev/null |
| 322 | +if [[ $? == 0 ]]; then |
| 323 | + DoChecksAndTests "${doChecksArgs[@]}" |
| 324 | + exit $? |
| 325 | +fi |
| 326 | + |
| 327 | +errorEcho "Script ”DoChecksAndTests” not found in the tests directory or PATH." |
| 328 | +errorEcho "Tests directory: $testPath" |
| 329 | +errorExit "Cannot run the tests" |
0 commit comments