Skip to content

Instantly share code, notes, and snippets.

@oderwat
Last active July 14, 2016 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oderwat/cc0941fa64785426eee92ccdcdb1921a to your computer and use it in GitHub Desktop.
Save oderwat/cc0941fa64785426eee92ccdcdb1921a to your computer and use it in GitHub Desktop.
Little helper script for using Nim with "runner" extension in Visual Studio Code
#!/bin/bash
if [ -z "$1" ]; then
echo "No file given! You may need to save first!"
exit 5
fi
cd `dirname "$1"`
CMD="c --verbosity:0 --hints:off --parallelBuild:1 "
FULL="`basename "$1"`"
NOPLINE="`grep '^# nop' $FULL | head -n 1`"
if [ -z "$NOPLINE" ]; then
NIMLINE="`grep '^# nim' $FULL | head -n 1`"
if [ "${NIMLINE:0:5}" = "# nim" ]; then
CMD="${NIMLINE:5}"
fi
BIN="${FULL%.*}"
#nim c --verbosity:0 --hints:off -r `basename "$1"`
trap 'kill -TERM $PID' TERM INT
rm "./$BIN" &>/dev/null
rm "./nimcache/$BIN.php" &>/dev/null
rm "./nimcache/$BIN.js" &>/dev/null
nim $CMD $FULL
if [ $? -ne 0 ]; then
echo "ERROR: Could not compile!"
exit 5
fi
else
NIMLINE=" check"
fi
CMD=""
RUNLINES="`grep '^# run ' $FULL`"
while read -r LINE; do
LINE=${LINE:6}
if [ ! -z "$LINE" ]; then
echo ">" $LINE
if [ "${LINE:0:5}" = "phpr " ]; then
CMD="${LINE:5}"
php -r "$CMD" 2>&1
elif [ "${LINE:0:4}" = "php " ]; then
CMD="${LINE:4}"
php $CMD 2>&1
elif [ "${LINE:0:5}" = "node " ]; then
CMD="${LINE:5}"
node $CMD 2>&1
elif [ "${LINE:0:5}" = "nimt " ]; then
CMD="${LINE:5}"
time nim $CMD 2>&1
elif [ "${LINE:0:4}" = "nim " ]; then
CMD="${LINE:4}"
nim $CMD 2>&1
elif [ "${LINE:0:5}" = "php7 " ]; then
CMD="${LINE:5}"
/usr/local/opt/php70/bin/php $CMD 2>&1
elif [ "${LINE:0:6}" = "php7r " ]; then
CMD="${LINE:6}"
/usr/local/opt/php70/bin/php -r "$CMD" 2>&1
fi
fi
done <<< "$RUNLINES"
ECHOLINES="`grep '^# echo ' $FULL`"
while read -r LINE; do
LINE=${LINE:7}
if [ ! -z "$LINE" ]; then
echo ">" $LINE
fi
done <<< "$ECHOLINES"
if [[ $NIMLINE == *" build"* ]]; then
CMD="build"
fi
if [[ $NIMLINE == *" check"* ]]; then
CMD="check"
fi
if [ -z "$CMD" ]; then
# support for PHP Backend!
if [ -e "./nimcache/$BIN.php" ]; then
php "./nimcache/$BIN.php" &
elif [ -e "./nimcache/$BIN.js" ]; then
node "./nimcache/$BIN.js" &
else
"./$BIN" 2>&1 &
fi
PID=$!
wait $PID
trap - TERM INT
wait $PID
EXIT_STATUS=$?
rm "./$BIN" &>/dev/null
fi
#time nim c --verbosity:0 --hints:off -d:release --parallelBuild:1 -r `basename "$1"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment