Skip to content

Instantly share code, notes, and snippets.

@ammar
Last active January 3, 2016 23:49
Show Gist options
  • Save ammar/72b5ef809b671e5241f6 to your computer and use it in GitHub Desktop.
Save ammar/72b5ef809b671e5241f6 to your computer and use it in GitHub Desktop.
Run commands and tests on multiple ruby versions. (ruby-build/chruby version)
#!/bin/bash
# A bash function/script used to run commands and tests using all the
# ruby-build-installed and chruby-managed versions of ruby on the system.
# Usage: mrun [command | script name]
#
# Without arguments, mrun will look for an executable file named run and run
# it. If an executable named run is not found, it will run rake. When given
# an argument (a script or a command, like bundle), it will run it with every
# installed ruby.
# Source chruby so we can use it as a function
source /usr/local/opt/chruby/share/chruby/chruby.sh
SELF=`basename $0`
RUBIES_DIR=$HOME/.rubies
RUNFILE_EXT=run
RUNFILE_DIR=runs
LAUNCH_DIR=`pwd`
VERSIONS=`ls $RUBIES_DIR`
DIVIDER="----------------------------------------------------------------------"
function passed() { echo "$(tput setaf 2)${1}$(tput sgr 0)"; }
function failed() { echo "$(tput setaf 1)${1}$(tput sgr 0)"; }
function check_run() {
PERCENT=`egrep '^[0-9]+% passed' $2`
if [[ $PERCENT == 100* ]]; then
passed "$1: $PERCENT"
else
STATS=`egrep '^[0-9]+ tests, [0-9]+ assertions' $2`
if [ ! -z "$STATS" -a "$STATS" != " " ]; then
failed "$STATS"
else
failed 'Error'
fi
fi
}
function mrun() {
versions=($VERSIONS)
echo "Running '$@' with ${#versions[@]} ruby versions"
echo $DIVIDER
for version in ${versions[@]}
do
chruby ${version}
if [ -n "$CHECK_RUN" ]; then
OUTPUT_FILE=$RUNFILE_DIR/${version}.$RUNFILE_EXT
"$@" > $OUTPUT_FILE 2>&1
check_run $version $OUTPUT_FILE
else
"$@"
fi
echo $DIVIDER
done
}
echo "$SELF in $LAUNCH_DIR"
if [ $# -gt 0 ]; then
case "$1" in
'clean')
if [ -d $RUNFILE_DIR ]; then
echo "Cleaning version run files"
rm -f $RUNFILE_DIR/*.$RUNFILE_EXT
else
echo "No run file directory named $RUNFILE_DIR found in $LAUNCH_DIR"
fi
;;
*)
mrun "$@"
;;
esac
else
CHECK_RUN=1
if [ ! -d $RUNFILE_DIR ]; then
mkdir $RUNFILE_DIR
fi
if [ -x "$LAUNCH_DIR/run" ]; then
mrun ./run
else
mrun rake
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment