Skip to content

Instantly share code, notes, and snippets.

@nihilismus
Last active January 30, 2017 01:30
Show Gist options
  • Save nihilismus/5698450 to your computer and use it in GitHub Desktop.
Save nihilismus/5698450 to your computer and use it in GitHub Desktop.
chruby + ruby-build / mksh

chruby + ruby-build / mksh

  1. Install chruby and ruby-build with git. I keep their repo at ~/.chrb, their installation inside ~/local and my rubies at ~/.chrb/rubies.
  2. Copy this in your ~/.mkshrc file
# chruby + ruby-build
chrb() {
    source ~/local/share/chruby/chruby.sh
    unset RUBIES
    RUBIES=$(ls -d ~/.chrb/rubies/* 2> /dev/null)
    CHRUBY_SOURCE_DIR=~/.chrb/chruby/
    RUBY_BUILD_SOURCE_DIR=~/.chrb/ruby-build
    case "$1" in
        -h|--help)
            chruby -h
            echo
            ruby-build -h
            ;;
        -v|--version)
            chruby -v
            echo
            ruby-build 2>&1 | head -1
            ;;
        -l|--list-definitions)
            # Get the current names of rubies to get a more usefull output
            # Installed ruby will be prefixed with "[*]", no installed ruby with "[ ]"
            _INSTALLED_RUBIES="$(ls ~/.chrb/rubies | sed 's:$:\$:' | xargs | sed 's: :\\|:g')"
            if [ -n "$_INSTALLED_RUBIES" ]; then
                ruby-build --definitions | \
                    sed "s:\($_INSTALLED_RUBIES\):[*] \1:" | \
                    sed '/\[/!s/^/[ ] /g' | COLUMNS=100 column
            else
                ruby-build --definitions | sed 's/^/[ ] /' | COLUMNS=100 column
            fi
            ;;
        -i|--install-definition)
            (
                # Temporary directory to build ruby
                export TMPDIR=/tmp/rubies
                # Use the same directory to keep the source code
                export RUBY_BUILD_CACHE_PATH=$TMPDIR
                # For a CPU with 4 cores
                export MAKE_OPTS="-j 5"
                # Rubies will be installed inside ~/.chrb/rubies/
                ruby-build -v $2 ~/.chrb/rubies/$2
            )
            ;;
        -u|--update-definitions)
            (
                # Updates ruby-build and chruby from git because ... YOLO
                cd $RUBY_BUILD_SOURCE_DIR
                echo "Updating " $(pwd)
                git pull
                # Delete all ruby-build definitions
                rm -rf ~/local/share/ruby-build/*
                PREFIX=~/local ./install.sh

                cd $CHRUBY_SOURCE_DIR
                echo "Updating " $(pwd)
                git pull
                PREFIX=~/local make install
            )
            ;;
        -d|--delete-definition)
            (
                # Actually i just move it to other place... not so YOLO
                mkdir -p /tmp/rubies
                mv ~/.rubies/$2 /tmp/rubies/$2
            )
            ;;
        -c|--changelog-definitions)
            (
                # To see what i've just updated
                _NUMBER_OF_COMMITS=10
                cd $RUBY_BUILD_SOURCE_DIR
                echo "ruby-build: last $_NUMBER_OF_COMMITS commits:"
                git log --oneline -$_NUMBER_OF_COMMITS --parents
                echo
                cd $CHRUBY_SOURCE_DIR
                echo "chruby last $_NUMBER_OF_COMMITS commits:"
                git log --oneline -$_NUMBER_OF_COMMITS --parents
            )
            ;;
        *)
            if [ -z $@ ]; then
                # If there is a .ruby-version file in the current working
                # directory check if there is a ruby installed with *exactly* the
                # same name. This will disable the "Fuzzy matching of Rubies by name."
                # feature in chruby... that's the way, aha, i like it.
                _CWD=$(pwd)
                _RUBY_VERSION_FILE="$_CWD/.ruby-version"
                _SELECTED_RUBY=""
                if [ -f "$_RUBY_VERSION_FILE" ]; then
                    echo "Detected $_RUBY_VERSION_FILE"
                    _RUBY_VERSION=$(head -1 "$_RUBY_VERSION_FILE" | cut -d ' ' -f 1)
                    for ruby in $(ls ~/.chrb/rubies); do
                        if [ "$_RUBY_VERSION" == "$ruby" ]; then
                            echo "Recognized $ruby"
                            _SELECTED_RUBY="$ruby";
                            break
                        fi
                    done
                    if [ -z "$_SELECTED_RUBY" ]; then
                        echo "$_RUBY_VERSION is not installed at ~/.chrb/rubies"
                        echo "Installed rubies:"
                    fi
                fi
                chruby $_SELECTED_RUBY
            else
                chruby $@
            fi
            ;;
    esac
}
  1. Use chrb, rather than chruby or ruby-build.
 → chrb
   1.9.3-p429
   2.0.0-p195
   jruby-1.7.4
 → chrb -l
[ ] 1.8.6-p383			[ ] 1.9.3-p385			[ ] jruby-1.7.0-rc1
[ ] 1.8.6-p420			[ ] 1.9.3-p392			[ ] jruby-1.7.0-rc2
[ ] 1.8.7-p249			[*] 1.9.3-p429			[ ] jruby-1.7.1
[ ] 1.8.7-p302			[ ] 1.9.3-preview1		[ ] jruby-1.7.2
[ ] 1.8.7-p334			[ ] 1.9.3-rc1			[ ] jruby-1.7.3
[ ] 1.8.7-p352			[ ] 2.0.0-dev			[*] jruby-1.7.4
[ ] 1.8.7-p357			[ ] 2.0.0-p0			[ ] maglev-1.0.0
[ ] 1.8.7-p358			[*] 2.0.0-p195			[ ] maglev-1.1.0-dev
[ ] 1.8.7-p370			[ ] 2.0.0-preview1		[ ] maglev-2.0.0-dev
[ ] 1.8.7-p371			[ ] 2.0.0-preview2		[ ] mruby-dev
[ ] 1.9.1-p378			[ ] 2.0.0-rc1			[ ] rbx-1.2.4
[ ] 1.9.1-p430			[ ] 2.0.0-rc2			[ ] rbx-2.0.0-dev
[ ] 1.9.2-p0			[ ] 2.1.0-dev			[ ] rbx-2.0.0-rc1
[ ] 1.9.2-p180			[ ] jruby-1.5.6			[ ] ree-1.8.6-2009.06
[ ] 1.9.2-p290			[ ] jruby-1.6.3			[ ] ree-1.8.7-2009.09
[ ] 1.9.2-p318			[ ] jruby-1.6.4			[ ] ree-1.8.7-2009.10
[ ] 1.9.2-p320			[ ] jruby-1.6.5			[ ] ree-1.8.7-2010.01
[ ] 1.9.3-dev			[ ] jruby-1.6.5.1		[ ] ree-1.8.7-2010.02
[ ] 1.9.3-p0			[ ] jruby-1.6.6			[ ] ree-1.8.7-2011.03
[ ] 1.9.3-p125			[ ] jruby-1.6.7			[ ] ree-1.8.7-2011.12
[ ] 1.9.3-p194			[ ] jruby-1.6.7.2		[ ] ree-1.8.7-2012.01
[ ] 1.9.3-p286			[ ] jruby-1.6.8			[ ] ree-1.8.7-2012.02
[ ] 1.9.3-p327			[ ] jruby-1.7.0			[ ] topaz-dev
[ ] 1.9.3-p362			[ ] jruby-1.7.0-preview1
[ ] 1.9.3-p374			[ ] jruby-1.7.0-preview2
 → which ruby
/usr/bin/ruby
 → ruby -v
ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-linux]
 → cd /tmp/foo/
 → chrb
Detected /tmp/foo/.ruby-version
Recognized jruby-1.7.4
 → which ruby
~/.rubies/jruby-1.7.4/bin/ruby
 → ruby -v
jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_21-b11 [linux-amd64]
 → chrb
   1.9.3-p429
   2.0.0-p195
   jruby-1.7.4
 → chrb -i jruby-1.7.0
...
Installed jruby-1.7.0 to ~/.rubies/jruby-1.7.0
 → chrb
   1.9.3-p429
   2.0.0-p195
   jruby-1.7.0
   jruby-1.7.4

problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment