Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

ruby

rvm

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?

like image 283
Jasarien Avatar asked Mar 18 '13 10:03

Jasarien


2 Answers

Try this at the beginning of your script in Xcode:

source "$HOME/.rvm/scripts/rvm"
like image 70
shoumikhin Avatar answered Oct 24 '22 06:10

shoumikhin


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:

enter image description here

like image 26
Clay Bridges Avatar answered Oct 24 '22 05:10

Clay Bridges