Tuesday, July 14, 2009

Bash Script to Freeze JRuby Gem Without Needing Rake

If you need to freeze gems in JRuby, this bash script might help. It also lists gems available to be frozen or what is frozen already when called by itself or with (some) invalid gem names.
#!/bin/bash

# jrfreeze
#
# Written by Gary S. Weaver
#
# This will freeze JRuby gems. chmod +x this script and put on your path.

set -e

if [ -z "$JRUBY_HOME" ]; then
  echo
  echo "Must set JRUBY_HOME environment variable to JRuby home."
  echo
  exit
fi

jrgems="$JRUBY_HOME/lib/ruby/gems/1.8/gems/"

if [ ! -d "$jrgems" ]; then
  echo
  echo "Could not find '$jrgems' directory."
  echo
  exit
fi

currentdir="$PWD"
vendorgems="$currentdir/vendor/gems/"

if [ ! -d "$vendorgems" ]; then
  echo
  echo "Could not find '$vendorgems' directory. Please create it if it doesn't exist."
  echo
  exit
fi

jrgemname="$1"
jrgem="$jrgems/$jrgemname/"

if [ ! -n "$jrgemname" ]; then
  echo
  echo "usage: freezegem.sh gemnameandversion"
  echo
  echo "Freezable gems:"
  echo
  ls "$jrgems"
  echo
  echo "Currently frozen gems:"
  echo
  ls "$vendorgems"
  echo
  exit
fi

if [ ! -d "$jrgem" ]; then
  echo
  echo "Cannot find '$jrgem'."
  echo
  echo "usage: freezegem.sh gemnameandversion"
  echo
  echo "Freezable gems:"
  echo
  ls "$jrgems"
  echo
  echo "Currently frozen gems:"
  echo
  ls "$vendorgems"
  echo
  exit
fi

cd "$jrgems"
tar -czvf "$jrgemname.tar.gz" "$jrgemname/"
mv "$jrgemname.tar.gz" "$vendorgems"
cd "$vendorgems"
tar -xzvf "$jrgemname.tar.gz"
rm "$jrgemname.tar.gz"
cd "$currentdir"
echo
echo "Copied $jrgemname to vendor/gems."
echo

No comments: