bash/rvm in rvm-0.0.4 vs bash/rvm in rvm-0.0.5

- old
+ new

@@ -1,22 +1,30 @@ #!/bin/bash rvm_author="Wayne E. Seguin" -rvm_version="0.0.4" +rvm_author_email="wayneeseguin@gmail.com" +rvm_website="http://github.com/wayneeseguin/rvm" +rvm_version="0.0.5" rvm_updated="2009.08.23" # # License: See LICENSE # # # Functions # -function usage { +function rvm-usage { cat <<-Usage + About: + + rvm ${rvm_version} ${rvm_website} + + by ${rvm_author} (${rvm_author_email}) + Usage: rvm Action [Implementation] [Options] Action: @@ -34,22 +42,21 @@ 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) + -d|--debug - Toggle debug mode on for extra messages (NYI) Notes: * Defaults above are denoted with a '*' prefix. * rvm is intended to be run as an individual user (not root, yet) @@ -60,26 +67,36 @@ ~/.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.1, install first if not installed - $ rvm use 1.9 # Equivalent to above, due to dof efaults - $ rvm use 1.8 # Use Ruby 1.8.6, install first if not installed + $ rvm-install # Install rvm, adds source hooks for bash & zsh + $ rvm install jruby # Install jRuby (default version is 1.3.1) + $ rvm use ruby -v 1.9.1 # Use Ruby 1.9.1, installs if necessary + $ rvm use 1.9 # Equivalent to above, due to defaults + $ rvm use 1.8 # Use Ruby 1.8.6, installs if necessary $ 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) + Credits: + + Bash Support Testing + + - Daniel Neighman (dneighman@gmail.com) + irc: hassox ; github: http://github.com/hassox + + - John Mettraux (jmettraux@openwfe.org) + irc: jmettraux ; github: http://github.com/jmettraux + + ZSH Support Testing + + - Franco Lazzarino (flazzarino@gmail.com) + irc: flazz ; github: http://github.com/flazz + Usage } # Logging functions based on level @@ -88,13 +105,13 @@ function info { log "\e[0;32m <i> $*" ; } function warn { log "\e[0;33m <w> $*" ; } function error { log "\e[0;31m <e> $*" ; } function fail { log "\e[0;31m <e> $*" ; return 1 ; } -function ruby-gi { gem install -q --no-rdoc --no-ri $* ; } +function rvm-gi { gem install -q --no-rdoc --no-ri $* ; } -function ruby-info { +function rvm-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/).*$//') @@ -113,15 +130,16 @@ full_version: "$full_version" Info } -function ruby-install-source { +function rvm-install-source { - case "${version-$1}" in + version="${1:-1.8.6}" + case "$version" in 1.9|1.9.1) major="1.9" ; minor="1" ; level="${2-243}" ;; - 1.9.2) major="1.9" ; minor="2" ; level="review1" ;; + 1.9.2) major="1.9" ; minor="2" ; level="${2-review1}" ;; 1.8|1.8.6) major="1.8" ; minor="6" ; level="${2-369}" ;; 1.8.7) major="1.8" ; minor="7" ; level="${2-174}" ;; *) fail "Ruby version '$version' is unknown." esac @@ -130,11 +148,11 @@ pushd $source_path if [ -d $package_name ] ; then cd $package_name else - if [ ! -f "$package_name.tar.gz" ] ; then $curl $url ; fi + if [ ! -f "$package_name.tar.gz" ] ; then curl -O -L -s $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/* @@ -142,21 +160,21 @@ gem_package_name="rubygems-1.3.5" gem_url="http://rubyforge.org/frs/download.php/60718/$gem_package_name.tgz" if [ -d $gem_package_name ] ; then cd $gem_package_name else - if [ ! -f $gem_package_name.tgz ] ; then $curl $gem_url ; fi + if [ ! -f $gem_package_name.tgz ] ; then curl -O -L -s $gem_url ; fi tar zxf $gem_package_name.tgz && cd $gem_package_name fi # Well this is fun... fix nil error on require_paths: sed -i '' "s/require_paths\.join/require_paths.to_a.join/" $source_path/$package_name/$gem_package_name/lib/rubygems/gem_path_searcher.rb $install_path/$package_name/bin/ruby ./setup.rb popd } -function ruby-install { +function rvm-install-ruby { case "$implementation" in ree) version=${version-1.8.6} @@ -165,56 +183,58 @@ 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 + if [ ! -f "$package_name.tar.gz" ] ; then curl -O -L -s $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 + unset 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 + if [ ! -f "$package_name.zip" ] ; then curl -O -L -s $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 + rvm-gi jruby-openssl ;; ruby) - version=${version-1.8.6} - ruby-install-source $version ${patchlevel-$3} + rvm-install-source ${version-1.8.6} ${patchlevel-$3} ;; + default) + fail "please specify a ruby implementation to install." + ;; *) fail "Ruby implementation '$implementation' is not known." esac - ruby-gi rake + rvm-gi rake } -function ruby-use { +function rvm-use { case "$implementation" in default) rm -f ~/.rvm/current @@ -245,26 +265,30 @@ fail "Unknown Ruby Enterprise Edition version: $version" fi ;; ruby) - if [ "$version" = "1.8.6" ] ; then - level="${patchlevel-369}" - MY_RUBY_HOME="$install_path/ruby-1.8.6-p$level" - GEM_HOME="$HOME/.gem/ruby/1.8" - elif [ "$version" = "1.8.7" ] ; then + if [ "$version" = "1.8.7" ] ; then level="${patchlevel-174}" MY_RUBY_HOME="$install_path/ruby-1.8.7-p$level" GEM_HOME="$HOME/.gem/ruby/1.8" - elif [ "$version" = "1.9.1" -o "$version" = "1.9" ] ; then - level="${patchlevel-243}" - MY_RUBY_HOME="$install_path/ruby-1.9.1-p$level" - GEM_HOME="$HOME/.gem/ruby/1.9" + + elif [ "$version" = "1.8.6" -o "$version" = "1.8" ] ; then + level="${patchlevel-369}" + MY_RUBY_HOME="$install_path/ruby-1.8.6-p$level" + GEM_HOME="$HOME/.gem/ruby/1.8" + elif [ "$version" = "1.9.2" ] ; then level="${patchlevel-review1}" MY_RUBY_HOME="$install_path/ruby-1.9.2-p$level" - GEM_HOME="$HOME/.gem/ruby/1.9" + GEM_HOME="$HOME/.gem/ruby/1.9.2" + + elif [ "$version" = "1.9.1" -o "$version" = "1.9" ] ; then + level="${patchlevel-243}" + MY_RUBY_HOME="$install_path/ruby-1.9.1-p$level" + GEM_HOME="$HOME/.gem/ruby/1.9.1" + else fail "Unknown ruby version: $version" fi ;; @@ -277,11 +301,11 @@ 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 $level + rvm-install-ruby $implementation $version $level 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 @@ -292,41 +316,51 @@ else PATH=$original_path ; export PATH fi info "Switching to $implementation $version $patchlevel ..." - exec bash --login - + if [ ! -z "$BASH_VERSION" ] ; then + exec bash -l + elif [ ! -z "$ZSH_VERSION" ] ; then + exec zsh -l + else + fail "Your shell '$0' is not supported." + fi } -function ruby-gem-dir { +function rvm-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" ] ; then + if [ "$version" = "1.8" -o "$version" = "1.8.6" ] ; then GEM_HOME="$HOME/.gem/ruby/1.8" - elif [ "${version%\.*}" = "1.9" ] ; then - GEM_HOME="$HOME/.gem/ruby/1.9" + + elif [ "$version" = "1.9.2" ] ; then + GEM_HOME="$HOME/.gem/ruby/1.9.2" + + elif [ "$version" = "1.9" -o "$version" = "1.9.1" ] ; 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 + echo $GEM_HOME && cd $GEM_HOME else fail "$implementation $version GEM directory does not exist." fi } -function ruby-src-dir { +function rvm-src-dir { case "$implementation" in jruby) version="${version-1.3.1}" if [ "$version" = "1.2.0" -o "$version" = "1.3.1" ] ; then @@ -337,30 +371,29 @@ ;; ree) version=${version-1.8.6} if [ "$version" = "1.8.6" ] ; then - patchlevel="${3-20090610}" - src_dir="$source_path/ruby-enterprise-$version-$patchlevel" + src_dir="$source_path/ruby-enterprise-$version-"${3-20090610}"" else fail "Unknown Ruby Enterprise Edition version: $version" fi ;; ruby) - if [ "$version" = "1.8.6" -o "$version" = "1.8" ] ; then - level="${patchlevel-369}" - src_dir="$source_path/ruby-1.8.6-p$level" - elif [ "$version" = "1.8.7" ] ; then - level="${patchlevel-174}" - src_dir="$source_path/ruby-1.8.7-p$level" - elif [ "$version" = "1.9.1" -o "$version" = "1.9" ] ; then - level="${patchlevel-243}" - src_dir="$source_path/ruby-1.9.1-p$level" + if [ "$version" = "1.8.7" ] ; then + src_dir="$source_path/ruby-1.8.7-p${patchlevel-174}" + + elif [ "${version%\.*}" = "1.8" ] ; then + src_dir="$source_path/ruby-1.8.6-p${patchlevel-369}" + elif [ "$version" = "1.9.2" ] ; then - level="${patchlevel-review1}" - src_dir="$source_path/ruby-1.9.2-p$level" + src_dir="$source_path/ruby-1.9.2-p${patchlevel-review1}" + + elif [ "${version%\.*}" = "1.9" ] ; then + src_dir="$source_path/ruby-1.9.1-p${patchlevel-243}" + else fail "Unknown ruby version: $version" fi ;; @@ -376,21 +409,21 @@ fi } # clones from source implementation/version to current -function ruby-gem-dup { +function rvm-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 + rvm-gi $gem_dir/cache/$gem_name-$gem_version.gem fi done else fail "Unknown $implementation version: $version" fi @@ -406,29 +439,33 @@ 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 ;; + install|use|path|info|gemdir|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.9.2|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 + implementation="$token" ;; + 1.8|1.8.6|1.8.7|1.9|1.9.1|1.9.2|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 ;; + -u|--gemdup) action="gemdup" ; gem_dir="$1" ; shift ;; + -d|--debug) debug=true ;; + *) rvm-usage ; return 1 esac done - curl="curl -O -L -s " - implementation=${implementation-'ruby'} + if [ -z "$implementation" -a -z "$version" ] ; then + implementation="default" + else + implementation=${implementation-'ruby'} + fi username=`whoami` # TODO: Sanitize user input, ensure that there is a / a the end... if [ "$username" = "root" ] ; then prefix_path=${prefix-/usr/local/} @@ -438,11 +475,13 @@ 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 [ -f ~/.rvm/original_path ] ; then + original_path=`cat ~/.rvm/original_path` + fi if [ -z "$original_path" ] ; then echo $PATH > $install_path/.original_path original_path=$PATH fi @@ -450,25 +489,25 @@ 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 1.9.2 ; do - ruby-install $implementation $version $patchlevel + rvm-install-ruby $implementation $version $patchlevel done else - ruby-install $implementation $version $patchlevel + rvm-install-ruby $implementation $version $patchlevel fi done else - ruby-install $implementation $version $patchlevel + rvm-install-ruby $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 ;; + use) rvm-use $implementation $version $patchlevel ;; + gemdir) rvm-gem-dir $implementation $version $patchlevel ;; + srcdir) rvm-src-dir $implementation $version $patchlevel ;; + gemdup) rvm-gem-dup $implementation $version $patchlevel ;; + info) rvm-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`" @@ -476,12 +515,13 @@ 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'" + else + rvm-usage fi return 1 esac }