RVM, onde estou!?!?!

Posted on November 24, 2009, under Configuração, Desenvolvimento, Ruby/Rails.

Comecei a utilizar o RVM em sua versão 0.0.22, na época ele não passava de um script shell que permitia a instalação e troca entre as vms, mas ele vem avançado muito, e funcionalidade com set de gemas é só a ponta do iceberg.

Motivado pelo post anterior acabei lendo um pouco mais a documentação e encontrei uns scriptizinho bem interessante. Como saber rapidamente qual a versão atual da minha VM? Para isso podemos contar com o script ~/.rvm/bin/rvm-prompt, ele pode ser usando adicionando a seguinte linha ao seu .bash_profile:

PS1="\$(~/.rvm/bin/rvm-prompt) $PS1"

Com isso ele mostra qual maquina virtual do Ruby você esta usando, mas apenas quando essa for diferente da default. Só isso já seria o suficiente, mas o que me incomoda nesta solução, é que eu já tenho uma linha de comando grande, e nem sempre eu preciso saber qual versão eu estou usando, isso é particularmente interessante quando estou em um projeto Ruby. Como normalmente um projeto Ruby temos um arquivo Rakefile podemos procurar por este arquivo e exibir a versão da máquina apenas quando estivermos no diretório de um projeto Ruby. Vamos adicionar as seguintes linhas ao nosso .bash_profile:

function rvm_version {
    RVM_VERSION=`~/.rvm/bin/rvm-prompt`
    if [[ -f "$(pwd)/Rakefile" ]] && [[ ! -z "$RVM_VERSION" ]]; then
        echo "${RVM_VERSION} "
    fi
}

Agora podemos mudar a linha anterior para:

PS1="$(rvm_version) $PS1"

Espero que ajude alguém e qualquer dica que possa melhor é só comentar a baixo.

8 Replies to "RVM, onde estou!?!?!"

gravatar

Wayne E. Seguin  on November 24, 2009

If you are using the latest version of rvm (rvm update –head) then you can take advantage of a few new features, namely hooks and the new project specific ‘.rvmrc’ file.

Let’s say I have my project directory as ~/projects/ and in this directory I have two projects ‘projecta’ and ‘projectb’. Project a is supposed to run on jruby all the time and projectb is supposed to run on ree 1.8.7. The setup:

echo “rvm jruby%projecta” > ~/projects/projecta/.rvmrc

echo “rvm ree%projectb” > ~/projects/projectb/.rvmrc

# notice the ‘ so that the “$” does not get interpreted by the shell:
echo ‘echo “Now using $rvm_ruby_string”‘ ~/.rvm/hooks/after_use

Then if I type this:

“cd ~/projects/projecta” I will then be running jruby with ‘projecta’ gem set and see the output ‘Now using jruby-1.4.0′. If I then “cd ~/projects/projectb” I will then be running ree with a ‘projectb’ gem set and see the output ‘Now using ree-1.8.7-2009.10′

By using the ~/.rvm/hooks/after_use hook every time I use a new ruby with rvm the echo statement gets executed in that context so for example:

∴ rvm 1.9.1

Now using ruby-1.9.1-p243

Enjoy, and thank you for using rvm!

~Wayne

gravatar

Lucas Fais  on November 24, 2009

Show!!

Bom, eu coloquei deixei aparecendo sempre porque a maior parte do tempo eu estou trabalhando com ruby mesmo. Dei uma modificada no meu PS1 para fica de uma forma que mais me agrada. Ta aí completo, com branch do git, cores e tudo mais:

PS1=’\n[\u] \[33[1;33m\]\w\a\[33[0m\]$(__git_ps1 ” \[33[1;32m\](%s)\[33[0m\]“) \[33[0;31m\]$(~/.rvm/bin/rvm-prompt)33[0m\]\n\$ ‘

gravatar

admin  on November 24, 2009

Hi Wayne,
Very nice tip, I’m using version 0.0.82, and worked perfectly here is.
Thank you for creating rvm, surely one of the best tools I’ve ever used.

gravatar

Wayne E. Seguin  on November 24, 2009

That is great to hear, thank you :)

Additionally I should mention that rvm-prompt also takes command line arguments so you can do things like this:

∴ ~/.rvm/bin/rvm-prompt i v

[ruby-1.9.1]

∴ ~/.rvm/bin/rvm-prompt i p

[ruby-p243]

∴ ~/.rvm/bin/rvm-prompt v p

[1.9.1-p243]

~Wayne

gravatar

ericfer  on November 26, 2009

@Everton, ótimo complemento para o post anterior. Show de bola !!!

@Wayne, really great tip. I’ve already incorporated in my prompt ;)

gravatar

Wayne E. Seguin  on November 29, 2009

I have updated the documentation for:

rvmrc files: http://rvm.beginrescueend.com/rvmrc/

rvm-prompt: http://rvm.beginrescueend.com/prompt/

rvm hooks: http://rvm.beginrescueend.com/hooks/

Enjoy!

~Wayne

Leave a Comment