Refactor and tidy BOSL's shell scripts

* Fixed ShellCheck.net warnings, including
  * exit -1: https://github.com/koalaman/shellcheck/wiki/SC2242
  * cd || exit: https://github.com/koalaman/shellcheck/wiki/SC2164
  * "$foo": https://github.com/koalaman/shellcheck/wiki/SC2086
  * $(( $foo )): https://github.com/koalaman/shellcheck/wiki/SC2004
  * grep | wc -l: https://github.com/koalaman/shellcheck/wiki/SC2126
* Use arrays instead of unquoted strings for loops and command arguments
* Use (( ... )) arithmetic context instead of string comparisons
* Use bash's built-in regex support instead of grep+sed+awk
* Write errors to stderr
This commit is contained in:
Michael Diamond 2021-05-21 00:46:17 -07:00
parent 57b1d0412c
commit 0666f6482c
5 changed files with 58 additions and 69 deletions

View file

@ -1,15 +1,16 @@
#!/bin/sh #!/bin/bash
VERFILE="version.scad" VERFILE="version.scad"
vernums=$(grep ^BOSL_VERSION "$VERFILE" | sed 's/^.*[[]\([0-9,]*\)[]].*$/\1/') if [[ "$(cat "$VERFILE")" =~ BOSL_VERSION.*=.*\[([0-9]+),\ *([0-9]+),\ *([0-9]+)\]\; ]]; then
major=$(echo "$vernums" | awk -F, '{print $1}') major=${BASH_REMATCH[1]} minor=${BASH_REMATCH[2]} revision=${BASH_REMATCH[3]}
minor=$(echo "$vernums" | awk -F, '{print $2}') new_revision=$(( revision+1 ))
revision=$(echo "$vernums" | awk -F, '{print $3}')
newrev=$(($revision+1)) echo "Current Version: $major.$minor.$revision"
echo "Current Version: $major.$minor.$revision" echo "New Version: $major.$minor.$new_revision"
echo "New Version: $major.$minor.$newrev"
sed -i '' 's/^BOSL_VERSION = .*$/BOSL_VERSION = ['"$major,$minor,$newrev];/g" $VERFILE
sed -i 's/^BOSL_VERSION = .*$/BOSL_VERSION = ['"$major,$minor,$new_revision];/g" "$VERFILE"
else
echo "Could not extract version number from $VERFILE" >&2
exit 1
fi

View file

@ -1,17 +1,12 @@
#!/bin/bash #!/bin/bash
lib_comment_lines=$(grep '^// ' *.scad | wc -l) lib_comment_lines=$(cat -- *.scad | grep -c '^// ')
lib_code_lines=$(grep '^ *[^ /]' *.scad | wc -l) tutorial_lines=$(cat tutorials/*.md | grep -c '^ *[^ /]')
script_code_lines=$(grep '^ *[^ /]' scripts/*.sh scripts/*.py | wc -l)
example_code_lines=$(grep '^ *[^ /]' examples/*.scad | wc -l)
test_code_lines=$(grep '^ *[^ /]' tests/*.scad | wc -l)
tutorial_lines=$(grep '^ *[^ /]' tutorials/*.md | wc -l)
y=$(printf "%06d" 13) printf '%-20s: %6d\n' \
'Documentation Lines' "$(( lib_comment_lines + tutorial_lines ))" \
printf "Documentation Lines : %6d\n" $(($lib_comment_lines+$tutorial_lines)) 'Example Code Lines' "$(cat examples/*.scad | grep -c '^ *[^ /]')" \
printf "Example Code Lines : %6d\n" $example_code_lines 'Library Code Lines' "$(cat -- *.scad | grep -c '^ *[^ /]')" \
printf "Library Code Lines : %6d\n" $lib_code_lines 'Support Script Lines' "$(cat scripts/*.sh scripts/*.py | grep -c '^ *[^ /]')" \
printf "Support Script Lines: %6d\n" $script_code_lines 'Test Code Lines' "$(cat tests/*.scad | grep -c '^ *[^ /]')"
printf "Test Code Lines : %6d\n" $test_code_lines

View file

@ -1,40 +1,38 @@
#!/bin/bash #!/bin/bash
FORCED="" DISPMD=0
FILES="" GEN_ARGS=()
DISPMD="" FILES=()
for opt in "$@" ; do for opt in "$@" ; do
case $opt in case "$opt" in
-f ) FORCED=$opt ;; -f ) GEN_ARGS+=(-f) ;;
-d ) DISPMD=$opt ;; -d ) DISPMD=1 ;;
-* ) echo "Unknown option $opt"; exit -1 ;; -* ) echo "Unknown option $opt" >&2; exit 1 ;;
* ) FILES="$FILES $opt" ;; * ) FILES+=("$opt") ;;
esac esac
done done
if [[ "$FILES" != "" ]]; then if (( ${#FILES[@]} == 0 )); then
PREVIEW_LIBS="$FILES" FILES=(Shapes2d Shapes3d Transforms Distributors Mutators Attachments Paths FractalTree)
else
PREVIEW_LIBS="Shapes2d Shapes3d Transforms Distributors Mutators Attachments Paths FractalTree"
fi fi
dir="$(basename $PWD)" # Try to cd to the BOSL2.wiki directory if run from the BOSL2 root
if [ "$dir" = "BOSL2" ]; then if [[ "$(basename "$PWD")" != "BOSL2.wiki" ]]; then
cd BOSL2.wiki if ! cd BOSL2.wiki; then
elif [ "$dir" != "BOSL2.wiki" ]; then echo "BOSL2.wiki directory not found, try running from the BOSL2 or BOSL2/BOSL2.wiki directory" >&2
echo "Must run this script from the BOSL2 or BOSL2/BOSL2.wiki directories."
exit 1 exit 1
fi
fi fi
rm -f tmp_*.scad rm -f tmp_*.scad
for base in $PREVIEW_LIBS; do for base in "${FILES[@]}"; do
base="$(basename $base .md)" base="$(basename "$base" .md)"
mkdir -p images/tutorials mkdir -p images/tutorials
rm -f images/tutorials/${base}_*.png images/tutorials/${base}_*.gif rm -f "images/tutorials/${base}"_*.png "images/tutorials/${base}"_*.gif
echo "$base.md" echo "${base}.md"
../scripts/tutorial_gen.py ../tutorials/$base.md -o Tutorial-$base.md $FORCED -I images/tutorials/ || exit 1 ../scripts/tutorial_gen.py "../tutorials/${base}.md" -o "Tutorial-${base}.md" "${GEN_ARGS[@]}" -I images/tutorials/ || exit 1
if [ "$DISPMD" != "" ]; then if (( DISPMD )); then
open -a Typora Tutorial-$base.md open -a Typora "Tutorial-${base}.md"
fi fi
done done

View file

@ -1,10 +1,11 @@
#!/bin/bash #!/bin/bash
if [[ ! -d BOSL2.wiki/.git ]] ; then if [[ ! -d BOSL2.wiki/.git ]] ; then
echo "Must be run from the BOSL2 directory, with the BOSL2.wiki repo inside." echo "Must be run from above the BOSL2.wiki repo." >&2
exit -1 exit 1
fi fi
set -e # short-circuit if any command fails
cd BOSL2.wiki cd BOSL2.wiki
rm -rf .git rm -rf .git
git init git init
@ -12,5 +13,3 @@ git add .
git commit -m "Purged wiki history." git commit -m "Purged wiki history."
git remote add origin git@github.com:revarbat/BOSL2.wiki.git git remote add origin git@github.com:revarbat/BOSL2.wiki.git
git push -u --force origin master git push -u --force origin master
cd ..

View file

@ -1,35 +1,31 @@
#!/bin/bash #!/bin/bash
if [ "$(uname -s)" != "Darwin" ]; then OPENSCAD=openscad
OPENSCAD=openscad if [ "$(uname -s)" == "Darwin" ]; then
else
OPENSCAD=/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD OPENSCAD=/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD
fi fi
if [ "$*" != "" ] ; then INFILES=("$@")
INFILES="$*" if (( ${#INFILES[@]} == 0 )); then
else INFILES=(tests/test_*.scad)
INFILES="tests/test_*.scad"
fi fi
OUTCODE=0 OUTCODE=0
for testscript in $INFILES ; do for testfile in "${INFILES[@]}"; do
repname="$(basename $testscript | sed 's/^test_//')" if [[ -f "$testfile" ]] ; then
testfile="tests/test_$repname" repname="$(basename "$testfile" | sed 's/^test_//')"
if [ -f "$testfile" ] ; then "${OPENSCAD}" -o out.echo --hardwarnings --check-parameters true --check-parameter-ranges true "$testfile" 2>&1
${OPENSCAD} -o out.echo --hardwarnings --check-parameters true --check-parameter-ranges true $testfile 2>&1
retcode=$? retcode=$?
res=$(cat out.echo) output=$(cat out.echo)
if [ $retcode -eq 0 ] && [ "$res" = "" ] ; then if (( retcode == 0 )) && [[ "$output" = "" ]]; then
echo "$repname: PASS" echo "$repname: PASS"
else else
echo "$repname: FAIL!" echo "$repname: FAIL!"
cat out.echo echo "$output"
echo OUTCODE="$retcode"
OUTCODE=-1
fi fi
rm -f out.echo rm -f out.echo
fi fi
done done
exit $OUTCODE exit "$OUTCODE"