It is assumed that you know what a XCode build phase is and how to create them. Create one, then follow the directions below to get it all working.
- You may find more success from creating a bash script that executes ruby code then to copy/paste the ruby code directly into XCode's script box. This way you can use XCode as the place to setup your environment and then run the code in the ruby script you want to have executed.
To do this, it's quite simple. Create a new file in the root directory of your project. script.rb
, for example.
Then, in your XCode build phase, keep the shell at it's default of /bin/sh
and have the script run the ruby script:
./script.rb
Now, you may be done already. However, you may encounter issues. That's why this whole document was created in the first place! Continue reading below to help you resolve scenarios you may encounter.
When you use rbenv on your system and you want to have rbenv run your ruby script instead of the default system's version of ruby run, you need to add this to the top of the XCode build phase script box:
export PATH=~/.rbenv/shims:$PATH
You may find that when you run the ruby script inside of XCode, you get this error thrown at you, ArgumentError: invalid byte sequence in US-ASCII
. That's a ruby output error, not XCode. To fix this, you need to add this to the top of the XCode build phase script box:
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
Thanks, I didn't quite deal with rbenv the way you suggested but basically this gist told me what I needed to know.