#!/bin/bash rvm_author="Wayne E. Seguin" rvm_version="0.0.3" rvm_updated="2009.08.23" # # License: See LICENSE # # # Functions # function usage { cat <<-Usage Usage: rvm Action [Implementation] [Options] Action: * usage - Show this usage information use - Switch to using a specific ruby versio (new login shell) info - Show information for current ruby gemdir - Switch to gem directory for installation (new login shell) srcdir - Switch to src directory for the current ruby installation gemdup - Clone source implementation version gems to currently used version install - Install a ruby version, default is from source debug - Emit environment and configuration information for debugging Implementation: * ruby - MRI/YARV Ruby (The Standard), defaults to 1.8.6 jruby - jRuby ree - Ruby Enterprise Edition rubinius - Rubinius (NIY) default - Resets to the default system ruby all - Used with install, installs all latest known versions Options: -v|--version - Ruby Package Version, defaults to 'latest' -l|--level - Patch level for the specified Ruby version -p|--prefix - Package and source directory prefix, with trailing slash! Default is a users home directory and /usr/local/ for root -c|--configure - Options for source compile (default: --enable-shared) -d|--debug - Toggle debug mode on for extra messages (NIY) Notes: * Defaults above are denoted with a '*' prefix. * rvm is intended to be run as an individual user (not root, yet) * All ruby installation, configuration and source files are in ~/.rvm * To manually reset to defaults: "rm -f ~/.rvm/current", then open new shell * To preserve previous gem installations for a particular ruby version copy, move, symlink or copy the old gem directory to (1.8 for 1.8.X): ~/.gem/\$interpreter/\$version Examples: $ gem install rvm # Install the rvm gem $ rvm-install # Install rvm, adds source hooks to ~/.bash_profile $ rvm install jruby # Install jRuby, default 1.3.1 $ rvm use ruby -v 1.9.1 # Use Ruby 1.9, install first if not installed $ rvm use -v 1.9 # Equivalent to above, because of defaults $ rvm use default # Use the system default (as if no rvm) TODO: (in order) * Documentation * Change gemdup to simply allow for a path to base gem dir to clone from * rvm gemdir default * Debug level messages * Rubinius (not in first revisions) * Shell detection instead of simply assuming bash (not in first revisions) Usage } # Logging functions based on level function log { echo -e "\n $* \e[0m\n" ; } function debug { log "\e[4;34m $*" ; } function info { log "\e[0;32m $*" ; } function warn { log "\e[0;33m $*" ; } function error { log "\e[0;31m $*" ; } function fail { log "\e[0;31m $*" ; return 1 ; } function ruby-gi { gem install -q --no-rdoc --no-ri $* ; } function ruby-info { full_version=$(ruby -v) ruby_implementation=$(echo "$full_version" | awk '{print $1}') ruby_version=$(echo "$full_version" | awk '{print $2}') ruby_patchlevel=$(echo "$full_version" | sed 's/^.*(//' | sed 's/).*$//') ruby_date=$(echo "$full_version" | sed 's/^.*(\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*$/\1/') ruby_platform=$(echo "$full_version" | sed 's/^.*\[//' | sed 's/\].*$//') cat <<-Info ruby: implementation: "$ruby_implementation" version: "$ruby_version" date: "$ruby_date" platform: "$ruby_platform" patchlevel: "$ruby_patchlevel" gem_home: "${GEM_HOME-not set}" ruby_home: "${ruby_home-not set}" full_version: "$full_version" Info } function ruby-install-source { case "${version-$1}" in 1.9|1.9.1) major="1.9" ; minor="1" ; patchlevel="${2-243}" ;; 1.8|1.8.6) major="1.8" ; minor="6" ; patchlevel="${2-369}" ;; 1.8.7) major="1.8" ; minor="7" ; patchlevel="${2-174}" ;; *) fail "Ruby version '$version' is unknown." esac package_name="ruby-$major.$minor-p$patchlevel" url="ftp://ftp.ruby-lang.org/pub/ruby/$major/$package_name.tar.gz" pushd $source_path if [ -d $package_name ] ; then cd $package_name else if [ ! -f "$package_name.tar.gz" ] ; then $curl $url ; fi tar xzf $package_name.tar.gz && cd $package_name/ fi ./configure --prefix=$install_path/$package_name ${configure-'--enable-shared'} && make && make install chmod +x $install_path/$package_name/bin/* popd } function ruby-install { case "$implementation" in ree) version=${version-1.8.6} patchlevel=${patchlevel-20090610} package_name="ruby-enterprise-$version-$patchlevel" url="http://rubyforge.org/frs/download.php/58677/$package_name.tar.gz" pushd $source_path if [ -d $package_name ] ; then cd $package_name else if [ ! -f "$package_name.tar.gz" ] ; then $curl $url ; fi tar xzf $package_name.tar.gz && cd $package_name/ fi cd ruby-enterprise-$version-$patchlevel && ./installer -a $install_path/ruby-enterprise-$version-$patchlevel --dont-install-useful-gems popd chmod +x $install_path/$package_name/bin/* ;; jruby) version=${version-1.3.1} # 1.2.0, 1.3.1 patchlevel="" # No patchlevel for jRuby package_name="$implementation-bin-$version" package_dir="$implementation-$version" url="http://dist.codehaus.org/$implementation/$version/$package_name.zip" pushd $source_path if [ -d $package_name ] ; then cd $package_name else if [ ! -f "$package_name.zip" ] ; then $curl $url ; fi jar xf $package_name.zip && cd $package_name/ fi mkdir -p $install_path/$package_dir/bin/ for binary in jruby jgem jirb ; do ln -sf $source_path/$package_dir/bin/$binary $source_path/$package_dir/bin/${binary#j} done rsync -ag $source_path/$package_dir/ $install_path/$package_dir/ cd $source_path/$package_dir/tool/nailgun && make popd chmod +x $install_path/$package_dir/bin/* ruby-gi jruby-openssl ;; ruby) version=${version-1.8.6} ruby-install-source $version ${patchlevel-$3} ;; *) fail "Ruby implementation '$implementation' is not known." esac ruby-gi rake } function ruby-use { case "$implementation" in default) rm -f ~/.rvm/current unset MY_RUBY_HOME unset GEM_HOME ;; #leopard) MY_RUBY_HOME="/System/Library/Frameworks/Ruby.framework/Versions/Current/usr"; GEM_HOME="$HOME/.gem/ruby/1.8" ;; jruby) version="${version-1.3.1}" if [ "$version" = "1.2.0" -o "$version" = "1.3.1" ] ; then MY_RUBY_HOME="$install_path/jruby-$version" GEM_HOME="$HOME/.gem/jruby/1.8" alias ruby_ng="jruby --ng" alias ruby_ng_server="jruby --ng-server" else fail "Unknown jRuby version: $version" fi ;; ree) version=${version-1.8.6} if [ "$version" = "1.8.6" ] ; then patchlevel="${3-20090610}" MY_RUBY_HOME="$install_path/ruby-enterprise-$version-$patchlevel" GEM_HOME="$HOME/.gem/ruby-enterprise/1.8" else fail "Unknown Ruby Enterprise Edition version: $version" fi ;; ruby) if [ "$version" = "1.8.6" ] ; then patchlevel="${patchlevel-369}" MY_RUBY_HOME="$install_path/ruby-1.8.6-p$patchlevel" GEM_HOME="$HOME/.gem/ruby/1.8" elif [ "$version" = "1.8.7" ] ; then patchlevel="${patchlevel-174}" MY_RUBY_HOME="$install_path/ruby-1.8.7-p$patchlevel" GEM_HOME="$HOME/.gem/ruby/1.8" elif [ "$version" = "1.9.1" -o "$version" = "1.9" ] ; then patchlevel="${patchlevel-243}" MY_RUBY_HOME="$install_path/ruby-1.9.1-p$patchlevel" GEM_HOME="$HOME/.gem/ruby/1.9.1" else fail "Unknown ruby version: $version" fi ;; *) fail "Ruby implementation '$implementation' is not known." esac if [ ! "$implementation" = "default" ] ; then RUBY_VERSION="$($MY_RUBY_HOME/bin/ruby -v | sed 's/^\(.*\) (.*$/\1/')" export GEM_HOME MY_RUBY_HOME RUBY_VERSION if [ ! -d $MY_RUBY_HOME ] ; then warn "$implementation $version is not installed, installing." ruby-install $implementation $version $patchlevel fi # Setup ~/.rvm/current echo "PATH=$MY_RUBY_HOME/bin:$GEM_HOME/bin:\$PATH ; export PATH" > ~/.rvm/current for variable in RUBY_VERSION GEM_HOME MY_RUBY_HOME ; do eval "export $variable" eval value=\$${variable} echo "${variable}='$value' ; export ${variable}" >> ~/.rvm/current done else PATH=$original_path ; export PATH fi info "Switching to $implementation $version $patchlevel ..." exec bash --login } function ruby-gem-dir { case "$implementation" in jruby) GEM_HOME="$HOME/.gem/jruby/1.8" ;; ree) GEM_HOME="$HOME/.gem/ruby-enterprise/1.8" ;; ruby) if [ "$version" = "1.8.6" -o "$version" = "1.8" ] ; then GEM_HOME="$HOME/.gem/ruby/1.8" elif [ "$version" = "1.8.7" ] ; then GEM_HOME="$HOME/.gem/ruby/1.8" elif [ "$version" = "1.9.1" -o "$version" = "1.9" ] ; then GEM_HOME="$HOME/.gem/ruby/1.9.1" else fail "Unknown ruby version: $version" fi ;; *) fail "Ruby implementation '$implementation' is not known." esac if [ -d $GEM_HOME ] ; then cd $GEM_HOME else fail "$implementation $version GEM directory does not exist." fi } function ruby-src-dir { case "$implementation" in jruby) version="${version-1.3.1}" if [ "$version" = "1.2.0" -o "$version" = "1.3.1" ] ; then src_dir="$source_path/$implementation-$version" else fail "Unknown jRuby version: $version" fi ;; ree) version=${version-1.8.6} if [ "$version" = "1.8.6" ] ; then patchlevel="${3-20090610}" src_dir="$source_path/ruby-enterprise-$version-$patchlevel" else fail "Unknown Ruby Enterprise Edition version: $version" fi ;; ruby) if [ "$version" = "1.8.6" -o "$version" = "1.8" ] ; then patchlevel="${patchlevel-369}" src_dir="$source_path/ruby-1.8.6-p$patchlevel" elif [ "$version" = "1.8.7" ] ; then patchlevel="${patchlevel-174}" src_dir="$source_path/ruby-1.8.7-p$patchlevel" elif [ "$version" = "1.9.1" -o "$version" = "1.9" ] ; then patchlevel="${patchlevel-243}" src_dir="$source_path/ruby-1.9.1-p$patchlevel" else fail "Unknown ruby version: $version" fi ;; *) fail "Ruby implementation '$implementation' is not known." return 1 esac if [ -d $src_dir ] ; then cd $src_dir else fail "$implementation $version source directory does not exist." fi } # clones from source implementation/version to current function ruby-gem-dup { gem_dir=$1 # TODO: check for and remove trailing /gems if [ ! -z "$gem_dir" ] ; then for gem_name_version in `ls $gem_dir/gems` ; do gem_name=${gem_name_version%-*} gem_version=${gem_name_version##*-} if [ -d $GEM_HOME/gems/$gem_name_version ] ; then echo "$gem_name_version already installed." else ruby-gi $gem_dir/cache/$gem_name-$gem_version.gem fi done else fail "Unknown $implementation version: $version" fi } function rvm-version { info "rvm $rvm_version" ; } function rvm { # Cleanup, aisle 3 for variable in action implementation patchlevel version source_path install_path manager debug prefix_path ; do eval "unset $variable" done while [ $# -gt 0 ] ; do token="$1" ; shift case "$token" in install|use|path|info|gemdir|gemdup|setup|version|debug|srcdir) action=$token ;; ruby|jruby|ree|default|all) implementation="$token" ;; 1.8|1.9|1.8.6|1.8.7|1.9.1|1.2.0|1.3.1) version="$token" ;; -v|--version) version="$1" ; shift ;; -l|--level) patchlevel="$1" ; shift ;; -p|--prefix) install_path="$1" ; shift ;; -s|--source) source_path="$1" ; shift ;; # Undocumented / untested "feature" -c|--configure) configure="$1" ; shift ;; -m|--manager) manager="$1" ; shift ;; -d|--debug) debug=true ;; *) usage ; return 1 esac done curl="curl -O -L -s " implementation=${implementation-'ruby'} username=`whoami` # TODO: Sanitize user input, ensure that there is a / a the end... if [ "$username" = "root" ] ; then prefix_path=${prefix-/usr/local/} else prefix_path=${prefix-$HOME/.} fi source_path="${source_path-${prefix_path}rvm/src}" install_path="${prefix_path}rvm" mkdir -p $source_path $install_path original_path=`cat $install_path/.original_path` if [ -z "$original_path" ] ; then echo $PATH > $install_path/.original_path original_path=$PATH fi case "$action" in install) if [ "$implementation" = "all" ] ; then for implementation in ruby jruby ree ; do if [ "$implementation" = "ruby" ] ; then for version in 1.8.6 1.8.7 1.9.1 ; do ruby-install $implementation $version $patchlevel done else ruby-install $implementation $version $patchlevel fi done else ruby-install $implementation $version $patchlevel fi ;; use) ruby-use $implementation $version $patchlevel ;; gemdir) ruby-gem-dir $implementation $version $patchlevel ;; srcdir) ruby-src-dir $implementation $version $patchlevel ;; gemdup) ruby-gem-dup $implementation $version $patchlevel ;; info) ruby-info $implementation $version $patchlevel ;; version) rvm-version ;; debug) rvm-version info "GEM_HOME: $GEM_HOME\nMY_RUBY_HOME: $MY_RUBY_HOME" info "ruby: `which ruby`\ngem: `which gem`\nirb: `which irb`" info "PATH:$(echo $PATH | awk -F":" '{print $1":"$2":"$3":"$4":"%5}')" info "\n.bash_profile: \n$(cat ~/.bash_profile | tail -n 5)\n" info "\n.rvm/current: \n$(cat ~/.rvm/current)\n" return 0 ;; *) usage if [ ! -z "$action" ] ; then fail "unknown action '$action'" fi return 1 esac }