Use rvm to force specific Ruby in Xcode Run Script build phase - ruby

Outside of Xcode I use a specific version of Ruby, using RVM to manage multiple Ruby installations.
Apple's command line dev tools install Ruby at /usr/bin/ruby and is version 1.8.7.
I use 1.9.3 through RVM.
Is there a way to force Xcode to use my 1.9.3 installation when running its Run Script build phases?
I already tried setting the Shell path to the full path of my specific Ruby, but that didn't seem to make a difference, by which I mean that the particular Gems I have installed in my 1.9.3 weren't available/visible to the script when run within Xcode.
If I run my project through xcodebuild on the command line, the Run Script phase uses my specific Ruby because it's being run from within my shell environment (even if the Shell path in the project file is set to /usr/bin/ruby, it still uses my 1.9.3).
What can I do to make the IDE use my 1.9.3 Ruby install?

I had the same (well, worse) problem, and the code that follows worked for me.
The key thing to realize is that, on the command line, you are using <something>/bin/rvm, but in a shell script, in order for rvm to change that environment, you must use a function, and you must first load that function to your shell script by calling source <something>/scripts/rvm. More on all this here.
This code is also gisted.
#!/usr/bin/env bash
# Xcode scripting does not invoke rvm. To get the correct ruby,
# we must invoke rvm manually. This requires loading the rvm
# *shell function*, which can manipulate the active shell-script
# environment.
# cf. http://rvm.io/workflow/scripting
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
# Then try to load from a root install
source "/usr/local/rvm/scripts/rvm"
else
printf "ERROR: An RVM installation was not found.\n"
exit 128
fi
# rvm will use the controlling versioning (e.g. .ruby-version) for the
# pwd using this function call.
rvm use .
As a protip, I find embedding shell code in a project.pbxproj file yucky. For all but the most trivial stuff, my actual run script step is usually just a one-line call out to an external script:

Try this at the beginning of your script in Xcode:
source "$HOME/.rvm/scripts/rvm"

Related

Ruby and Xcode - wrong ruby version [duplicate]

Outside of Xcode I use a specific version of Ruby, using RVM to manage multiple Ruby installations.
Apple's command line dev tools install Ruby at /usr/bin/ruby and is version 1.8.7.
I use 1.9.3 through RVM.
Is there a way to force Xcode to use my 1.9.3 installation when running its Run Script build phases?
I already tried setting the Shell path to the full path of my specific Ruby, but that didn't seem to make a difference, by which I mean that the particular Gems I have installed in my 1.9.3 weren't available/visible to the script when run within Xcode.
If I run my project through xcodebuild on the command line, the Run Script phase uses my specific Ruby because it's being run from within my shell environment (even if the Shell path in the project file is set to /usr/bin/ruby, it still uses my 1.9.3).
What can I do to make the IDE use my 1.9.3 Ruby install?
I had the same (well, worse) problem, and the code that follows worked for me.
The key thing to realize is that, on the command line, you are using <something>/bin/rvm, but in a shell script, in order for rvm to change that environment, you must use a function, and you must first load that function to your shell script by calling source <something>/scripts/rvm. More on all this here.
This code is also gisted.
#!/usr/bin/env bash
# Xcode scripting does not invoke rvm. To get the correct ruby,
# we must invoke rvm manually. This requires loading the rvm
# *shell function*, which can manipulate the active shell-script
# environment.
# cf. http://rvm.io/workflow/scripting
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
# Then try to load from a root install
source "/usr/local/rvm/scripts/rvm"
else
printf "ERROR: An RVM installation was not found.\n"
exit 128
fi
# rvm will use the controlling versioning (e.g. .ruby-version) for the
# pwd using this function call.
rvm use .
As a protip, I find embedding shell code in a project.pbxproj file yucky. For all but the most trivial stuff, my actual run script step is usually just a one-line call out to an external script:
Try this at the beginning of your script in Xcode:
source "$HOME/.rvm/scripts/rvm"

Switching rubies in shell script

When executing a bash shell script I am using ruby 1.9.3. Then, within the script, I want to switch to JRub (I'm using rvm). I tried switching to JRuby by doing rvm use jruby within the script, but this didn't work, it said:
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.
./run.sh: line 10: jruby: command not found
When I do type rvm | head -n1 at the command prompt, I get: rvm is a function. So I'm not sure of the problem. I thought it might be because I installed JRuby using sudo (sudo rvm install jruby). So I ran the shell script again using sudo. Again I received the error.
How can I switch rubies from within a bash shell script with rvm?
Thanks
I ended up adding this to the line before:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
as explained in this thread (sorry - though I was aware of this thread before, I didn't quite grasp it):
RVM doesn't switch Rubies
/complete/path/to/rvm your shell seems to have an 'rvm' builtin command defined.

After Installation RVM and Ruby 1.9.3 every Bash path change RVM Initialization Script is printed

I installed the latest RVM - Ruby Version Manager - and installed Ruby 1.8.7 and 1.9.3 on OSX Lion with XCode 4.3.3 using clang rvm install 1.9.3 --reconfigure --debug -C --enable-pthread --with-gcc=clang as the regular way did not work due to a GCC error. I did get one error clang: error: unsupported option '--with-libyaml, but Ruby 1.9.3 worked and I could run WPScan that needs at least 1.9.2 .
But now every time I run a command to change folder such as cd I get a long Bash script printed related to RVM - see http://pastebin.com/UAm38Vcm and:
.
How can I stop it from doing that?
Update I
Added a comment at RVM at Github as well https://github.com/wayneeseguin/rvm/issues/1039 , but as that issue is not 100% related and as I need this solved as soon as possible I opened a thread here with more data.
Update II
I realized the RVM Initialization script is the one that is being printed: https://github.com/wayneeseguin/rvm/blob/master/scripts/initialize . No idea why though..
Update IV
My .bashrc
# define aliases
alias sudo='sudo '
#alias ruby='ruby1.9'
alias apacherestart='sudo apachectl -k restart'
# define hist properties
HISTFILESIZE=1000000000
HISTSIZE=1000000
# define path to programs
PATH=/opt/local/bin:opt/local/sbin:/opt/subversion/bin:/opt/local/apache2/bin/:/opt/local/lib/python2.4/site-packages/django/bin:$PATH
# define manpath
MANPATH=/opt/local/share/man:$MANPATH
# export env vars
export HISTFILESIZE HISTSIZE PATH MANPATH
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
My .bash_profile
source ~/.bashrc
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Your cd command has somehow become aliased or a function has been created named "cd".
You can undo those with either:
unalias cd
or
unset -f cd
You should be able to revert temporarily with
unalias cd
It appears that you will need to figure out what was changed in your .bashrc and revert all those changes. If the installation script is at all sanely written, it should have made backups, or document somehow what was changed.

RVM and Jenkins setup

I am new to Jenkins CI. I'm install RVM in my remote Jenkins and when I execute below shell.
#!/bin/bash -x
source ~/.bashrc
rvm use 1.9.3#rails-3.2.3
I get following errors.
+ source /var/lib/jenkins/.bashrc
++ PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/var/lib/jenkins/.rvm/bin:/var/lib/jenkins/.rvm/bin
+ rvm use 1.9.3#rails-3.2.3
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal settings to allow shell login.
Please visit https://rvm.io/workflow/screen/ for example.
What does it mean? I don't have any idea. Please help me.
UPDATED: I'm tried below script but I still get errors:
#!/bin/bash -x
source /home/zeck/.bashrc
[[ -s ".rvmrc" ]] && source .rvmrc
export RAILS_ENV=test
bundle install
Errors:
/tmp/hudson457106939700368111.sh: line 5: bundle: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Jenkins build shell can't detect RVM, gemsets and gems. What should I do?
UPDATED 2: Therefore jenkins can't detect ruby.
+ ruby -v
/tmp/hudson2505951775163045158.sh: line 5: ruby: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILUR
I'm not using any jenkins plugn and I'm just run script from Build->Execute shell section.
As the error message suggests, RVM expects an login shell. Changing the hashbang line to #!/bin/bash -xl should resolve this.
try:
. $(/home/RVM_USER/.rvm/bin/rvm env 1.9.3#rails-3.2.3 --path)
make sure you run the stable RVM:
rvm get stable
NOTE:
Last Jenkins version does not always accept "source", but ".".
RVM_USER is the user that installed RVM.
Alternatively you can also export the RVM command in the main PATH.
Yes, apparently you miss the $HOME/.rvm/bin in your PATH. I am using rvm successfully from Hudson on Mac OS X. First thing to notice is that, unless you define BASH_ENV environment variable (ENV for sh), .bashrc is called automatically only with interactive non-login shell. Such a shell is started when you do - for example - the following from the command line:
$ /bin/bash
When you use #!/bin/bash in your script, .bashrc will not be called.
To make rvm work with Hudson, I have the following in my .bash_profile:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
Thanks to [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" I have rvm enabled every time I start new terminal window (interactive, login shell).
I do not put anything in my .bashrc, especially I am not sourcing rvm scripts there. Nothing wrong with that, but if any other scripts makes something stupid like setting `export BASH_ENV=$HOME/.bashrc' and then invoke non-interactive shell, you see what may happen - it is actually easy to forget.
Therefore, instead of loading things to your .bashrc, it is better to keep your script independent from any shell startup file and make sure that the correct environment is set up within the script. I still keep $HOME/.rvm/bin in my .bash_profile, but then I include the following at the beginning of my script:
#!/bin/bash
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
rvm use 1.9.3-head#MyGemSet
set -ex
cucumber # just an example
Notice the -e option which forces the script to exit with error code if any command following set -ex fails. This is behavior you may want when using the script with Hudson.
It is incorrect to say that RVM expects a login shell. Although using #!/bin/bash -l in your script will work, it does not seem like the best approach.
Just add this code in your shell script, i think rvm is loading from your source so it should work else need to export PATH variable
#!/bin/bash -l
source ~/.bashrc
rvm use 1.9.3#rails-3.2.3
l is for login shel, if you include x then it would be for debugging too.
adding a shebang to the build commands in jenkins fixed this for me
#!/usr/bin/env bash
rvm use 2.0.0
bundle install
rake test
...
Jenkins nodes don't load paths the same way, so it's not using the proper path to find rvm's version of ruby. You can set the path for a given agent.
Find your current PATH by doing echo $PATH
Assuming you've set up rvm properly, find where rvm's version of ruby is located by running which ruby
There's a setting in the Configuration of your agent where you can set environmental variables. Set PATH to be 1 and 2 concatenated.

TextMate, rvm and TM_RUBY

In the TextMate RVM instructions the text it says to set TM_RUBY to /Users/wayne/.rvm/bin/textmate_ruby and in the image it shows it set to rvm-auto-ruby. I decided to set it to rvm-auto-ruby thinking that it would use RVM's default Ruby version.
When running Command R in the RSpec.bundle having TM_RUBY set to rvm-auto-ruby will result in a load error. When you set it to textmate_ruby it works.
The only problem here is that TextMate doesn't always use the default version of Ruby since it's hardcoded in that file.
/Users/jspooner/.rvm/bin/textmate_ruby:
#!/usr/bin/env bash
if [[ -s "/Users/jspooner/.rvm/environments/ruby-1.9.2-head" ]] ; then
source "/Users/jspooner/.rvm/environments/ruby-1.9.2-head"
exec ruby "$#"
else
echo "ERROR: Missing RVM environment file: '/Users/jspooner/.rvm/environments/ruby-1.9.2-head'" >&2
exit 1
fi
So two questions:
What should TM_RUBY=rvm-auto-ruby actually do?
Is there a way to have TextMate use the RVM default?
Setting TM_RUBY to your-path/rvm-auto-ruby
http://rvm.io/integration/textmate/
should load whatever ruby and gemset is indicated in the .rvmrc file located in the project and if none default to rvm default. I just got this working and it is very smooth. I did need to get the latest version of rvm
rvm get head
to make it work and restart Textmate. Hope that helps.
See your other, similar, question Rspec bundle is broken in TextMate and rvm.
To help others chasing this same issue, the solution seems to be at: RVM / Textmate doesnt recognize .rvmrc Options.
Basically you replace the ~/.rvm/bin/textmate_ruby soft link with a file. This is what I did:
cd ~/.rvm/bin
mv textmate_ruby old.textmate_ruby
Create a shell script called textmate_ruby in the same directory to replace the soft-link, using the following contents:
!/usr/bin/env sh
source ~/.rvm/scripts/rvm
cd .
exec ruby "$#"
chmod +x textmate_ruby
Before doing this change I'd see my system Ruby's version (1.8.7) displayed if I did CMD+R to run the following script in TextMate:
puts RUBY_VERSION
Evaluating the script using CMD+CNTRL+SHIFT+E gave me 1.9.2.
After switching to use that script both point to Ruby 1.9.2, so at least there's some consistency now. I don't see TextMate tracking my currently set RVM Ruby version yet; Instead it's using the default version set in RVM: rvm use 1.9.2 --default. This is still a step forward because I can control which Ruby TextMate uses by adjusting my --default.
If you decide you want to revert later, just rename, or delete, the script and reverse step 2 above.

Resources