Skip to content

Instantly share code, notes, and snippets.

@srvanderplas
Created September 15, 2023 14:56
Show Gist options
  • Save srvanderplas/ee19b7018aaac906bad0e207cd7b829d to your computer and use it in GitHub Desktop.
Save srvanderplas/ee19b7018aaac906bad0e207cd7b829d to your computer and use it in GitHub Desktop.
Script to fix up github classroom repositories, pull changes, and compile quarto homework files
#!/bin/bash
while getopts r::pvf OPT
do
case "$OPT" in
r) FILE="$OPTARG" # RENDER files with quarto
RENDER=1;;
v) VERBOSE=1;; # VERBOSE - print everything
p) PULL=1;; # Pull new changes
f) FIX=1;; # Fix github repo
esac
done
# Attempt at default arguments
# https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
if [ $VERBOSE ];
then
echo "FILE = ${FILE:='*.qmd'}"
echo "Render=$RENDER"
echo "PULL = $PULL"
fi
for dir in */;
{
# Exit if terminated using ctrl-c or other command
# https://serverfault.com/questions/105386/bash-loop-how-to-stop-the-loop-when-i-press-control-c-inside-a-command
trap "echo Exited!; exit;" SIGINT SIGTERM
if [ $VERBOSE ]; then echo "Changing to $dir"; fi;
cd $dir
# Fix github repo origin if necessary
if [ $FIX ];
then
git remote set-url origin $(git remote -v | head -n 1 | grep -o 'github\.com.*git' | awk '{gsub("github.com/", "github.com:",$1); print "git@"$1}')
fi;
# Pull new changes
if [ $PULL ];
then
git reset --hard HEAD >> git.log
git clean -f -d >> git.log
git pull >> git.log
if [ $VERBOSE ]; then cat "git.log"; fi;
fi;
# Render quarto doc
if [ $RENDER ];
then
#echo "quarto render $FILE"
quarto render "${FILE:='*.qmd'}" &> compile.log
if [ $VERBOSE ]; then cat "compile.log"; fi;
fi;
# Change back to main directory
if [ $VERBOSE ]; then echo "Finished: $dir"; fi;
cd ../;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment