Skip to content

Instantly share code, notes, and snippets.

@levibostian
Last active April 18, 2023 10:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levibostian/aac45628ff00f677888824d651cc7724 to your computer and use it in GitHub Desktop.
Save levibostian/aac45628ff00f677888824d651cc7724 to your computer and use it in GitHub Desktop.
How to run Ruby scripts from within XCode build phase script

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.

rbenv

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

Credits

ArgumentError: invalid byte sequence in US-ASCII error

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

Credits

@mattneub
Copy link

Thanks, I didn't quite deal with rbenv the way you suggested but basically this gist told me what I needed to know.

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